ansible 2 lists in playbook vars file

Trying to get a playbook working with yml file containing 2 lists and passing that into the main file to loop over.

Can someone help with letting me know what I’m doing wrong? Thanks

copylist.yml

Trying to get a playbook working with yml file containing 2 lists and
passing that into the main file to loop over.

Can someone help with letting me know what I'm doing wrong? Thanks
  copylist.yml
---
copylist:
     src:
       - /home/ansible/tom/tom.txt
       - /home/ansible/fred/fred.txt
     dest:
       - /home/ansible/tom1
       - /home/ansible/fred1

This is not a list its a dictionary.

Playbook - copy.yml
---
- hosts: localhost
   become: yes
   vars:
     users:
       - tom
       - fred
   vars_files:
       - copylist.yml
   tasks:
     - name: Create users from vars
       user:
         name: "{{item}}"
         state: present
       with_items: "{{users}}"

     - name: Copy files into target directories
       copy:
         src: "{{item.src}}"
         dest: "{{item.dest}}"
       with_items: "{{copylist}}"

Since copylist is not a list but a dictionary this fails.

You need to write copylist as list you like this and you task will work.

copylist:
   - src: /home/ansible/tom/tom.txt
     dest: /home/ansible/tom1
   - src: /home/ansible/fred/fred.txt
     dest: /home/ansible/fred1

Hi,

Thanks for the reponse. This works but I dont quite understand it…

So copylist is now basically a list with two listed values

  • src
  • src

How does it pick up dest when its not really designated as a list item with a dash (-)?
I wouldve expected -dest to be listed aswell twice

Hi,

Thanks for the reponse. This works but I dont quite understand it...

So copylist is now basically a list with two listed values
   - src
   - src

How does it pick up dest when its not really designated as a list item with
a dash (-)?
I wouldve expected -dest to be listed aswell twice

      Does this give you ideas?

# In vars file/entry
normal_users:
  - moe : { uid : "1205", groups : "{{web_groups}},wheel" }
  - larry : { uid : "1203", groups : "{{web_groups}}" }
  - curly : { uid : "1207", groups : "{{web_groups}}" }

# In task file
- name: List all Normal Users
  debug: msg="Users {{ normal_users }} "

- name: List Normal Users one at a time
  debug: msg="Users {{ item }} "
  with_items: "{{ normal_users }} "

The dash indicate start of a list item.
And item contains a dictionary, this has two element src and dest.

You can write a list of dictionary in two ways

copylist:
   - src: /home/ansible/tom/tom.txt
     dest: /home/ansible/tom1
   - src: /home/ansible/fred/fred.txt
     dest: /home/ansible/fred1

or

copylist:
   - {'src':'/home/ansible/tom/tom.txt', 'dest':'/home/ansible/tom1'}
   - {'src':'/home/ansible/fred/fred.txt', 'dest':'/home/ansible/fred1'}

Which syntax used, come down to personal preferences.