如何在ansible中迭代库存组?

如何在ansible中迭代库存组?,ansible,Ansible,我看过一些与我的问题类似的帖子,还有一些帖子,但我仍然不明白我做错了什么: 这是我的库存文件: [root@82c420275711 playbooks]# cat inventory.dev # # - Comments begin with the '#' character # - Blank lines are ignored # - Groups of hosts are delimited by [header] elements # - You can enter

我看过一些与我的问题类似的帖子,还有一些帖子,但我仍然不明白我做错了什么:

这是我的库存文件:

[root@82c420275711 playbooks]# cat inventory.dev
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups
[localhost]
127.0.0.1

[virtual_centers]
172.17.0.2
172.17.0.5
[root@82c420275711 playbooks]# ansible-playbook -i inventory.dev ./configure_syslog-ng_server.yaml
...
    TASK [allow {{ item }} to allow port 514] *******************************************************************************************************************************************************************************************
failed: [127.0.0.1] (item=groups['virtual_centers']) => {"changed": false, "item": "groups['virtual_centers']", "msg": "ERROR: Exception caught: INVALID_ADDR: groups['virtual_centers']"}
以下是我的剧本中遇到问题的部分:

- name: "allow {{ item }} to allow port 514"
  firewalld:
    immediate: yes
    rich_rule: "rule family=\"ipv4\" source address=\"{{ item }}\" port protocol=\"udp\" port=\"514\" accept"
    permanent: yes
    state: enabled
  with_items: groups['virtual_centers']
以下是使用该清单文件运行playbook时收到的错误消息:

[root@82c420275711 playbooks]# cat inventory.dev
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups
[localhost]
127.0.0.1

[virtual_centers]
172.17.0.2
172.17.0.5
[root@82c420275711 playbooks]# ansible-playbook -i inventory.dev ./configure_syslog-ng_server.yaml
...
    TASK [allow {{ item }} to allow port 514] *******************************************************************************************************************************************************************************************
failed: [127.0.0.1] (item=groups['virtual_centers']) => {"changed": false, "item": "groups['virtual_centers']", "msg": "ERROR: Exception caught: INVALID_ADDR: groups['virtual_centers']"}

我做错了什么?谢谢

好的,我知道了。我需要这样写任务:

- name: allow {{ item }} to allow port 514
  firewalld:
    immediate: yes
    rich_rule: "rule family=\"ipv4\" source address=\"{{ item }}\" port protocol=\"udp\" port=\"514\" accept"
    permanent: yes
    state: enabled
  with_items:
    - "{{ groups['virtual_centers'] }}"