Nested looping with dict/sub-list

657 views
Skip to first unread message

Duncan Hutty

unread,
Feb 5, 2015, 11:08:59 AM2/5/15
to ansible...@googlegroups.com
I'm having a bit of difficulty with getting the behaviour that I want in
as concise a way as I think should be possible.

Here's the point:
I'm having trouble with iterating over a hash where the value for one of
the keys is itself a list. Is this possible with Ansible? Or would I
need to write a custom iterator?

I think that, having read the docs on how Ansible does looping
(http://docs.ansible.com/playbooks_loops.html) that I must be
misunderstanding something, so I'm asking for illumination concerning
the general case - this repository problem is just an example.


I want to configure /etc/apt/sources.list to contain:

deb http://mirrors.us.kernel.org/ubuntu/ precise main
deb http://mirrors.us.kernel.org/ubuntu/ precise universe
deb http://mirrors.us.kernel.org/ubuntu/ precise-updates main
deb http://mirrors.us.kernel.org/ubuntu/ precise-updates universe
deb http://mirrors.us.kernel.org/ubuntu/ precise-security main
deb http://mirrors.us.kernel.org/ubuntu/ precise-security universe

My desired approach is to have a role called 'repositories', because I
will need to take care of multiple platform versions/releases and
non-distro/vendor repositories.

roles/repositories/defaults/main.yml:
---
# defaults file for repositories
apt_repositories:
vendor_base:
uri: "http://mirrors.us.kernel.org/ubuntu/"
suffix: ""
components:
- main
- universe
vendor_security:
uri: "http://mirrors.us.kernel.org/ubuntu/"
suffix: "-security"
components:
- main
- universe
vendor_updates:
uri: "http://mirrors.us.kernel.org/ubuntu/"
suffix: "-updates"
components:
- main
- universe


roles/repositories/tasks/main.yml
---
# tasks file for apt repository config
- name: "install standard apt repositories"
apt_repository: repo="deb {{item.value.uri}} {{
ansible_distribution_release }}{{ item.value.suffix }}" state=present
with_dict: apt_repositories


With the task as above, I get:

deb http://mirrors.us.kernel.org/ubuntu/ precise
deb http://mirrors.us.kernel.org/ubuntu/ precise-updates
deb http://mirrors.us.kernel.org/ubuntu/ precise-security

which is nearly there, but is missing the iteration over the list that
is the subelement 'components'. I have attempted to use
'with_subelements' and I think perhaps I just haven't managed the
correct incantation because I see in the debug output that the 'item'
dict _does_ contain my components list.

I've already managed to solve the immediate problem of getting the
desired repos configured on my machines by using 'with_nested' with
hard-coded _lists_ of suffixes and components and I realise that I could
iterate over my entire dict with sublist data structure with a jinja2
template (instead of using the apt_repository module), but I'm posting
for the general theory of iteration.

Thank you for reading this far,
Duncan Hutty

Matt Martz

unread,
Feb 5, 2015, 11:18:56 AM2/5/15
to ansible...@googlegroups.com
I'm guessing what you want is:

apt_repository: repo="deb {{item.value.uri}} {{ ansible_distribution_release }}{{ item.value.suffix }} {{ item.components|join(" ") }}" state=present

This just uses the 'join' filter to join that components list into a space separated string.



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/54D3874A.1050302%40allgoodbits.org.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Duncan Hutty

unread,
Feb 6, 2015, 9:28:33 AM2/6/15
to ansible...@googlegroups.com

That's an interesting strategy for getting the sources.list.d
configured, but it relies on the fact that that line defining a deb
repository can take multiple components 'deb
http://mirrors.us.kernel.org/ubuntu/ precise main universe', so I'm no
clearer on the real question about iterating over a dict with a sub-list.


(although it also made me notice that I want to sort the dict and it's
not immediately obvious how to do so: "with_dict: apt_repositories |
dictsort" doesn't work - I think because dictsort yields a list of
key,value pairs not a real dict).

Thanks for the helpful reply!

Duncan Hutty
Reply all
Reply to author
Forward
0 new messages