How to traverse a nested dict structure using Ansible?

I have the following dict structure variable in an unoccupied play:

apache_vhosts: - name: foo server_name: foo.com server_aliases: - a.foo.com - b.foo.com - c.foo.com - name: bar server_name: bar.com server_aliases: - d.bar.com - e.bar.com - f.bar.com 

I need to create a symbolic link for each of the server_name and server_aliases , for example:

 /tmp/foo.com -> /var/www/foo /tmp/a.foo.com -> /var/www/foo /tmp/b.foo.com -> /var/www/foo /tmp/c.foo.com -> /var/www/foo /tmp/bar.com -> /var/www/bar /tmp/d.bar.com -> /var/www/bar /tmp/e.bar.com -> /var/www/bar /tmp/f.bar.com -> /var/www/bar 

I have the following task that works for server_name :

 - name: Add a domain symlinks /tmp for server_name. file: src: "/var/www/{{ item.name }}" dest: "/tmp/{{ item.server_name }}" state: link with_items: apache_vhosts 

But I'm not sure how I can do the same for the server_aliases array.

I am happy to use two separate tasks, if necessary, but I try not to add a separate domains variable, which duplicates the list of domains in a flat structure.

This Google Groups post is close, but I can’t decide how to make it work for multiple virtual hosts.

Is it possible? Or is this a fundamentally wrong approach?

+5
source share
1 answer

You can use with_subelements to loop through server_aliases. Next snippet

  - name: Add a domain symlinks /tmp for server_name. debug: msg="{{ item.server_name }}" with_items: apache_vhosts - name: Add a domain symlinks /tmp for server_aliases. debug: msg="name - {{ item.0.name }} and serverAlias - {{ item.1 }}" with_subelements: - apache_vhosts - server_aliases 
+12
source

All Articles