Using with_items with a dictionary

49 views
Skip to first unread message

Michael Bushey

unread,
Aug 28, 2017, 3:51:19 PM8/28/17
to Ansible Project
Hello,

I have a dict that contains a list of sites. For each of these sites I would like to provide a list of symlinks to create provided by with_items. Is this possible? I have Ansible 2.3.1.0.


Thanks!
Michael

Uwe Sauter

unread,
Aug 28, 2017, 3:53:04 PM8/28/17
to ansible...@googlegroups.com
I'm not quiet sure I understand your data model but instead of with_items have a look at with_dict.
> --
> 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-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/13eb2739-7e21-4503-8e75-7f3db8803966%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/13eb2739-7e21-4503-8e75-7f3db8803966%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Michael Bushey

unread,
Aug 28, 2017, 4:42:40 PM8/28/17
to ansible-project
Thanks for responding Uwe! I appreciate your time. :)

I'm already using a with_dict to get the list of sites, but now for each site I would like to provide a list of symlinks to generate. The list of symlinks does not change.

This creates the docroots:

- name: Create docroot
  file: path=/var/www/html/{{ item.value.url }} state=directory owner=www-data group=www-data
  with_dict: "{{ sites }}"
  when: item.value.type == "grav"
  tags: grav, grav-sites

I would like to create the same symlinks for each of these sites, but I don't know how to get {{ site from dict }}:

- name: Create symlinks
  file: dest=/var/www/html/{{ site from dict }}/{{ item }} src=/var/www/grav/grav/{{ item }} state=link owner=www-data group=www-data
  with_items:
    - bin
    - CHANGELOG.md
    - composer.json
    - .editorconfig
    - .gitignore
    - index.php
    - LICENSE.txt
    - system
    - vendor
  tags: grav, grav-sites


On Mon, Aug 28, 2017 at 12:52 PM, Uwe Sauter <uwe.sa...@gmail.com> wrote:
I'm not quiet sure I understand your data model but instead of with_items have a look at with_dict.

Am 28.08.2017 um 21:51 schrieb Michael Bushey:
> Hello,
>
> I have a dict that contains a list of sites. For each of these sites I would like to provide a list of symlinks to
> create provided by with_items. Is this possible? I have Ansible 2.3.1.0.
>
>
> Thanks!
> Michael
>
> --
> 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
--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/gns-XRtt9aE/unsubscribe.
To unsubscribe from this group and all its topics, 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/d8d85ea0-addc-6d99-c3ad-be9bfc62f83d%40gmail.com.

Uwe Sauter

unread,
Aug 29, 2017, 3:05:06 AM8/29/17
to ansible...@googlegroups.com
My first thought was to put those two tasks into a block, modify task 1 to register a new variable that contains the current site
and use it in task 2. Then I remembered that you cannot loop over a block (currently…
https://github.com/ansible/ansible/issues/13262).


You could try to create a new list variable out of your dict which only contains all {{ site from dict }} and then use
with_nested. E.g.

- name: 'create docroot'
...

- name: 'extract sites'
set_fact: sitenames='{{ sitenames | default([]) }} + {{ item.name }}'
with_dict: '{{ sites }}'

- name: 'create symlinks'
file:
dest: '/var/www/html/{{ item[0] }}/{{ item[1] }}'
src: '/var/www/grav/grav/{{ item[1] }}'
state: link
owner: www-data
group: www-data
with_nested:
- '{{ sitenames }}'
- [ 'bin', 'CHANGELOG.md', ...]
> > ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>>.
> > To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>
> <mailto:ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>>.
> <https://groups.google.com/d/msgid/ansible-project/13eb2739-7e21-4503-8e75-7f3db8803966%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/13eb2739-7e21-4503-8e75-7f3db8803966%40googlegroups.com?utm_medium=email&utm_source=footer>>.
> > For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/gns-XRtt9aE/unsubscribe
> <https://groups.google.com/d/topic/ansible-project/gns-XRtt9aE/unsubscribe>.
> To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com
> <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/d8d85ea0-addc-6d99-c3ad-be9bfc62f83d%40gmail.com
> <https://groups.google.com/d/msgid/ansible-project/d8d85ea0-addc-6d99-c3ad-be9bfc62f83d%40gmail.com>.
> For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
>
>
> --
> 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-proje...@googlegroups.com
> <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAPJTyAXqsGF7HMoF5fNjaacL%2Bfsxz%3D9dZ%3DhHR6K7THF%2B74QhTg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAPJTyAXqsGF7HMoF5fNjaacL%2Bfsxz%3D9dZ%3DhHR6K7THF%2B74QhTg%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Arbab Nazar

unread,
Aug 29, 2017, 6:29:48 AM8/29/17
to Ansible Project
Hi Michael, This simple example might help you that how we can use the with_items with dictionary:

---
- hosts: all
  become
: no
  gather_facts
: no
  connection
: local
  vars
:
    USERS
:
      foo
:
        state
: present
        company
: 'Bar foo'
        email
: foo@test.net
        uid
: 6001
        groups
:
         
- ring-admins
         
- ring-users
     
      bar
:
        state
: present
        company
: 'Foo bar'
        email
: bar@tlt.com      
        uid
: 6002
        groups
:
         
- ring-admins
         
- ring-users


  tasks
:
   
- debug:
        msg
: "{{ item }} , {{ USERS[item].state }} , {{ USERS[item].company }}, {{ USERS[item].email }}, {{ USERS[item].uid }} , {{ USERS[item].groups|join( ',' ) }}"
      with_items
: "{{ USERS | list }}"


Result of the playbook:

╰─➤  ansible-playbook -i localhost, playbook.yml

PLAY [all] *******************************************************************************************************************

TASK [debug] *****************************************************************************************************************
ok: [localhost] => (item=foo) => {
    "item": "foo",
    "msg": "foo , present , Bar foo, f...@test.net, 6001 , ring-admins,ring-users"
}
ok: [localhost] => (item=bar) => {
    "item": "bar",
    "msg": "bar , present , Foo bar, b...@tlt.com, 6002 , ring-admins,ring-users"
}

PLAY RECAP *******************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0


Hope that might help you.
Reply all
Reply to author
Forward
0 new messages