Ansible 在剧本中使用多值变量

Ansible 在剧本中使用多值变量,ansible,ansible-2.x,ansible-facts,Ansible,Ansible 2.x,Ansible Facts,我正在网上学习redhat ansible课程,并按照以下步骤在剧本中使用多值变量。但是我遇到了一个错误,说有一个未定义的变量 下面是我的arrays.yaml文件 --- - name: show arrays hosts: ansible1.example.com vars_files: - vars/users tasks: - name: print array values debug: msg: "User {{ item.us

我正在网上学习redhat ansible课程,并按照以下步骤在剧本中使用多值变量。但是我遇到了一个错误,说有一个未定义的变量

下面是我的arrays.yaml文件

---
- name: show arrays
  hosts: ansible1.example.com
  vars_files:
    - vars/users
  tasks:
    - name: print array values
      debug:
        msg: "User {{ item.username }} has homedirectory {{ item.homedir }} and shell {{ item.shell }}"
      with_items: "{{ users }}"
下面是vars/users文件

users:
  linda:
    username: linda
    homedir: /home/linda
    shell: /bin/bash

  gokul:
    username: gokul
    homedir: /home/gokul
    shell: /bin/bash

  saha:
    username: saha
    homedir: /home/gokul/saha
    shell: /bin/bash
下面是我遇到的错误

ansible-playbook arrays.yaml

PLAY [show arrays] *****************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************************************
ok: [ansible1.example.com]

TASK [print array values] **********************************************************************************************************************************************************************************************************************************
fatal: [ansible1.example.com]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'username'\n\nThe error appears to be in '/home/ansible/install/arrays.yaml': 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"}

PLAY RECAP *************************************************************************************************************************************************************************************************************************************************
ansible1.example.com       : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
然而,当我尝试在剧本中引用下面这样的单个值时,效果很好

---
- name: show arrays
  hosts: ansible1.example.com
  vars_files:
    - vars/users
  tasks:
    - name: print array values
      debug:
        msg: "User {{ users.gokul.username }} has homedirectory {{ users.gokul.homedir }} and shell {{ users.gokul.shell }}"

不可能迭代字典。应该更改代码或数据

1。更改数据

使用用户列表而不是字典。例如下面的列表

users:
  - username: linda
    homedir: /home/linda
    shell: /bin/bash
  - username: gokul
    homedir: /home/gokul
    shell: /bin/bash
  - username: saha
    homedir: /home/gokul/saha
    shell: /bin/bash
会像预期的那样工作

- name: print array values
  debug:
    msg: "User {{ item.username }}
          has homedirectory {{ item.homedir }}
          and shell {{ item.shell }}"
  loop: "{{ users }}"
2。更改代码

可以将
用户
字典与过滤器
dict2items
一起使用。例如,下面的任务将给出相同的结果

- name: print array values
  debug:
    msg: "User {{ item.value.username }}
          has homedirectory {{ item.value.homedir }}
          and shell {{ item.value.shell }}"
  loop: "{{ users|dict2items }}"