You loop needs something to iterate over. Since ansible treats variables as
strings, you need to make is a list. Try something like below:
# cat ./split_users.yml
---
- hosts: localhost
connection: local
gather_facts: no
vars:
userList: "{{ users }}"
tasks:
- name: split the user list
debug: var=item
with_items: userList.split(',')
# ansible-playbook -vvvv split_users.yml -e 'users=moe,larry,curly'
PLAY [localhost]
**************************************************************
TASK: [split the user list]
***************************************************
ok: [localhost] => (item=moe) => {
"item": "moe",
"var": {
"item": "moe"
}
}
ok: [localhost] => (item=larry) => {
"item": "larry",
"var": {
"item": "larry"
}
}
ok: [localhost] => (item=curly) => {
"item": "curly",
"var": {
"item": "curly"
}
}
PLAY RECAP
********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
On Wednesday, July 15, 2015 at 7:33:25 AM UTC-4, Dimitar Hristov wrote:
>
> Hi Guys,
>
> I get an error when I run a playbook, which aims to create new users and
> set authorized keys for them. The error:
>
> TASK: [create new users]
> ******************************************************
> fatal: [testvm1] => with_items expects a list or a set
> fatal: [testvm2] => with_items expects a list or a set
>
>
> Here's a part of the playbook (the first task fails):
>
> - name: create new users
> user: name={{ item.name }} group=wheel append=yes
> password={{user_password}}
> with_items: "{{users}}"
>
> - name: set pub keys
> authorized_key: "user={{ item.0.name }} key='{{ lookup('file',
> item.1) }}'"
> with_subelements:
> - users
> - authorized
>
> - name: set pass expiration
> command: /usr/bin/chage -d 0 {{ item.name }}
> with_items: "{{users}}"
>
> Here's the var file:
>
> ---
> wheelsregex: # *%wheel *ALL=\(ALL\) *ALL
> user_password: 12345678
> users:
> - name: test
> authorized:
> - /etc/ansible/add_users/files/test.pub
> - name: test1
> authorized:
> - /etc/ansible/add_users/files/test1.pub
> - name: test2
> authorized:
> - /etc/ansible/add_users/files/test2.pub
>
> Any idea where's my mistake? I saw that it might be related to ansible
> version, so mine is 1.9.2.
>
>
> Regards,
> Dimitar
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/2c398bff-a7eb-4c74-ac37-e8febcdb4827%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.