Loop using with_items twice

491 views
Skip to first unread message

Anfield

unread,
Jul 20, 2017, 9:49:55 PM7/20/17
to Ansible Project
What is the correct way to use with_Items to loop over two files? The playbook is failing at the 2nd task

Getting an error - TASK [Create users from vars users] ********************************************
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 'users'\n\nThe error appears to have been in '/etc/ansible/playbooks/exercises/usersgroups.yml': line 15, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n   - name: Create users from vars users\n     ^ here\n"}

users.yml (is below, grouplist.yml is similar)
---
users:
- jock
- dejan
- joel

Playbook is below -

    - users.yml
    - grouplist.yml
  tasks:

   - name: Create groups from vars file
     group:
        name: "{{item}}"
        state: present
     with_items: "{{grouplist}}"

   - name: Create users from vars users
     user:
        name: "{{item.users}}"
        state: present
        groups: "{{item.grouplist}}"
     with_items:
         - "{{ users}}"
         - "{{grouplist}}"

Branko Majic

unread,
Jul 21, 2017, 8:31:48 AM7/21/17
to ansible...@googlegroups.com
Assuming that what you want to do is create a number of groups listed
in the grouplist variable, and then create the users listed in the
users variable, where each user is member of all groups in the
grouplist variable, your second task should be one of the following
(Ansible 2.3 can accept lists directly in "groups" attribute):

- name: Create users from vars users (Ansible 2.3+)
user:
name: "{{ item }}"
state: present
groups: "{{ grouplist }}"
with_items: "{{ userlist }}"

- name: Create users from vars users (Ansible <2.3)
user:
name: "{{ item }}"
state: present
groups: "{{ grouplist | join(',') }}"
with_items: "{{ userlist }}"

Best regards

--
Branko Majic
XMPP: bra...@majic.rs
Please use only Free formats when sending attachments to me.

Бранко Мајић
XMPP: bra...@majic.rs
Молим вас да додатке шаљете искључиво у слободним форматима.

Anfield

unread,
Jul 21, 2017, 9:26:23 AM7/21/17
to Ansible Project
Yes thats exactly what I wanted to do but I thought I had to use with_items at the bottom to provide the list that it would loop through. Thanks


Reply all
Reply to author
Forward
0 new messages