ansible 2 lists in playbook vars file

29 views
Skip to first unread message

Anfield

unread,
Jul 8, 2017, 5:52:45 PM7/8/17
to Ansible Project
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

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}}"

Playbook output -

TASK [Copy files into target directories] **************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'dest'\n\nThe error appears to have been in '/etc/ansible/playbooks/copy.yml': line 18, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Copy files into target directories\n      ^ here\n"}





Kai Stian Olstad

unread,
Jul 8, 2017, 5:59:32 PM7/8/17
to ansible...@googlegroups.com
On 08. juli 2017 23:52, Anfield wrote:
> 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


--
Kai Stian Olstad

Anfield

unread,
Jul 8, 2017, 6:09:43 PM7/8/17
to Ansible Project, ansible-pr...@olstad.com
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


Mauricio Tavares

unread,
Jul 8, 2017, 6:24:03 PM7/8/17
to ansible...@googlegroups.com
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 }} "


> --
> 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.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/6f9dc917-80a1-42ec-bb7c-65659fac9d43%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Jul 8, 2017, 6:26:09 PM7/8/17
to ansible...@googlegroups.com
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.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages