"users" is an array of three things. So you have to either iterate over
the array as with something like "loop:", or index it as with
"users[0]['username']", "users[0]['homedirectory']",
"users[0]['shell']", or use some sort of filter that does the iteration
for you and selects list items that match some criterion. For example:
*- name: Extract matches for 'test'****debug:****msg: "User {{ item.username }} has homedirectory {{ item.homedir }} and
shell {{ item.shell }}"****loop: "{{ users | selectattr('username', 'equalto', 'test') }}"*
produces this:
TASK [Extract matches for 'test']
************************************************************************************************
ok: [localhost] => (item={'username': 'test', 'homedir': '/home/test', 'shell':
'/bin/bash'}) => {
"msg": "*User test has homedirectory /home/test and shell /bin/bash*"
}
On 9/17/22 2:25 PM, Ahmed Elhusseini wrote:
yes, it does. Thank you.
but I have a question why I cannot use this:
---
- name: show arrays
hosts: ansible1.technyati.com
vars_files:
- /home/ansible/rhce8/lesson5/arrays/vars/users
tasks:
- name: print array values
debug:
msg: "User {{ users.test.username }} has homedirectory {{
users.test.homedir }} and shell {{ users.test.shell }}"
On Thursday, September 15, 2022 at 9:18:22 PM UTC+3 [email protected]
wrote:
Does this help?
*$ cat arrays.yml*
---
- name: show arrays
hosts: localhost
vars:
users:
- username: test
homedir: /home/test
shell: /bin/bash
- username: test1
homedir: /home/test1
shell: /bin/bash
- username: test2
homedir: /home/test2
shell: /bin/bash
tasks:
- name: print array values
debug:
msg: "User {{ item.username }} has homedirectory {{
item.homedir }} and shell {{ item.shell }}"
loop: "{{ users }}"
*$ ansible-playbook arrays.yml*
PLAY [show arrays]
***************************************************************************************************************
TASK [Gathering Facts]
***********************************************************************************************************
ok: [localhost]
TASK [print array values]
********************************************************************************************************
ok: [localhost] => (item={'username': 'test', 'homedir':
'/home/test', 'shell': '/bin/bash'}) => {
"msg": "User test has homedirectory /home/test and shell
/bin/bash"
}
ok: [localhost] => (item={'username': 'test1', 'homedir':
'/home/test1', 'shell': '/bin/bash'}) => {
"msg": "User test1 has homedirectory /home/test1 and shell
/bin/bash"
}
ok: [localhost] => (item={'username': 'test2', 'homedir':
'/home/test2', 'shell': '/bin/bash'}) => {
"msg": "User test2 has homedirectory /home/test2 and shell
/bin/bash"
}
PLAY RECAP
***********************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0
On Thursday, September 15, 2022 at 1:48:14 PM UTC-4
[email protected] wrote:
cat vars/users
users:
- username: test
homedir: /home/test
shell: /bin/bash
- username: test1
homedir: /home/test1
shell: /bin/bash
- username: test2
homedir: /home/test2
shell: /bin/bash
cat arrays.yaml
---
- name: show arrays
hosts: ansible1
vars_files:
- vars/users
tasks:
- name: print array values
debug:
msg: "User {{ users.test.username }} has homedirectory
{{ users.test.homedir }} and shell {{ users.test.shell }}"
~
I got the error below:
fatal: [ansible1]: FAILED! => {"msg": "The task includes an
option with an undefined variable. The error was: 'list
object' has no attribute 'test'\n\nThe error appears to be in
'/home/ansible/arrays.yml': line 7, column 7, but may\nbe
elsewhere in the file depending on the exact syntax
problem.\n\nThe offending line appears to be:\n\n tasks:\n
- name: print array values\n ^ here\n"}
--
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 view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/890b3bd0-59c2-b85a-06d7-cfeee351a74a%40gmail.com.