need help on simple arrays

90 views
Skip to first unread message

Ahmed Elhusseini

unread,
Sep 15, 2022, 1:48:14 PM9/15/22
to Ansible Project
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"}

Todd Lewis

unread,
Sep 15, 2022, 2:18:22 PM9/15/22
to Ansible Project
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
Message has been deleted

Ahmed Elhusseini

unread,
Sep 17, 2022, 2:26:37 PM9/17/22
to Ansible Project
yes, it does. Thank you.

but I have a question why I cannot use this:

---
- name: show arrays
  hosts: ansible1
  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 }}"

Todd Lewis

unread,
Sep 17, 2022, 4:36:31 PM9/17/22
to ansible...@googlegroups.com, uto...@gmail.com
"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 uto...@gmail.com wrote:

Ahmed Elhusseini

unread,
Sep 20, 2022, 2:23:55 PM9/20/22
to Ansible Project
Thank you so much, I appreciated
Reply all
Reply to author
Forward
0 new messages