bindo
bindo

Reputation: 87

Ansible looping through values specified in a file

Trying to get a hang of ansible loops as a beginner. I've got a role for setting up multiple users with their respective attributes. I can loop through the values successfully when the values are specified within the task file but struggling to call the values if they are kept in a different file.

Content of "/roles/myusers/tasks/useradd.yml

- name: Add serveral users
  user:
    name: "{{ item.name }}"
    group: sftp
    groups: sftp
    state: present
    uid: "{{ item.uid }}"
  loop:
    {{ users }}

Content of "/roles/myusers/vars/main.yml"

---
users:
  - { name: 'testuser1', uid: '691' }
  - { name: 'testuser2', uid: '692' }```

Content of Content of "/roles/myusers/tasks/main.yml"

---
- import_tasks: useradd.yml

Please can someone shed some light? My understanding is that when the values are kept in /var/main.yml file there doesnt appear to be a reason to use "include_vars", "include" and etc..? Admit that I haven't spent enough time going through the documentation. Looks to be it isn't the easiest thing to read. TIA guys.

Upvotes: 0

Views: 68

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68384

Quotation is missing. Try

loop: "{{ users }}"

Upvotes: 1

Related Questions