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
cat vars/users
users:
cat arrays.yaml
Does this help?
$ cat arrays.yml
yes, it does. Thank you.
but I have a question why I cannot use this:
“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**"
}
Thank you so much, I appreciated