【问题标题】:How to iterate over inventory group in ansible?如何在ansible中迭代库存组?
【发布时间】:2019-01-07 18:59:03
【问题描述】:

我查看了一些与我的问题类似的 SO 帖子:Ansible : iterate over inventory groupsAnsible iterate over hosts in inventory group set by variable 等,但我仍然不明白我做错了什么:

这是我的库存文件:

[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

这是我的剧本中遇到问题的部分:

- 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]# 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']"}

我在这里做错了什么?谢谢

【问题讨论】:

    标签: ansible


    【解决方案1】:

    好的,我想通了。我需要像这样编写该任务:

    - 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'] }}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多