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/035508e1-8c1a-4dda-ad8e-2245028ef1bfn%40googlegroups.com.

Reply via email to