For the same playbook, if my variables are defined in a file (let name it myvars.yml) specified with vars_files statement it works but if the same variables are defined in group_vars/all it don’t.
Example my playbook and it’s associated files using var_files/myvars.yml
playbook.yml
- hosts: all
sudo: yes
vars_files:- myvars.yml
roles:- usertest
myvars.yml
users:
- username: timmy
gecos: Timmy
groups:- group1
- group2
- username: tony
gecos: Tony
groups:- group1
- group3
roles/usertest/tasks/main.yml
- include: tasks_user.yml user=$item
with_items: $users
roles/usertest/tasks/task_user.yml (excerpt)
- name: create user ${user.username}
action: user name=${user.username} comment=${user.gecos}
name: “Create group”
group: name=$item state=present
with_items: ${user.groups}name: add user to groups
action: user name=${user.username} groups=$item append=yes
with_items: ${user.groups}
When I run it, everything fine :
PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [127.0.0.1]TASK: [create user timmy] *****************************************************
ok: [127.0.0.1] => (item={‘username’: ‘timmy’, ‘gecos’: ‘Timmy’, ‘groups’: [‘group1’, ‘group2’]})TASK: [Create group] **********************************************************
ok: [127.0.0.1] => (item=group1)
ok: [127.0.0.1] => (item=group2)TASK: [add user to groups] ****************************************************
ok: [127.0.0.1] => (item=group1)
ok: [127.0.0.1] => (item=group2)TASK: [create user tony] ******************************************************
ok: [127.0.0.1] => (item={‘username’: ‘tony’, ‘gecos’: ‘Tony’, ‘groups’: [‘group1’, ‘group3’]})TASK: [Create group] **********************************************************
ok: [127.0.0.1] => (item=group1)
ok: [127.0.0.1] => (item=group3)TASK: [add user to groups] ****************************************************
ok: [127.0.0.1] => (item=group1)
ok: [127.0.0.1] => (item=group3)PLAY RECAP ********************************************************************
127.0.0.1 : ok=7 changed=0 unreachable=0 failed=0
Now instead of using an external file to define my users setting I want to put them in group_vars/all :
playbook.yml : I remove vars_files
- hosts: all
sudo: yes
roles:- usertest
myvars.yml : deleted, users definition set in group_vars/all instead (same yaml)
users:
- username: timmy
gecos: Timmy
groups:- group1
- group2
- username: tony
gecos: Tony
groups:- group1
- group3
roles/usertest/tasks/main.yml : nothing changed
- include: tasks_user.yml user=$item
with_items: $users
roles/usertest/tasks/task_user.yml (excerpt) : nothing changed
- name: create user ${user.username}
action: user name=${user.username} comment=${user.gecos}
name: “Create group”
group: name=$item state=present
with_items: ${user.groups}name: add user to groups
action: user name=${user.username} groups=$item append=yes
with_items: ${user.groups}
And when I run it, $users is not expended or something like that :
PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [127.0.0.1]TASK: [create user ${user.username}] ******************************************
ok: [127.0.0.1] => (item=$users)TASK: [Create group] **********************************************************
fatal: [127.0.0.1] => with_items expects a listFATAL: all hosts have already failed – aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/Users/ydavid/playbook-test.retry127.0.0.1 : ok=2 changed=0 unreachable=1 failed=0
It create a user named ${user.username} and fail on the loop over groups because as $user is not expanded $user.groups is not a list
The problem comme from the association include and with_item because group_vars/all is readed correctly if I don’t use a loop
Must I open an issue ? Or may I did something wrong ?
Thanks for your help