如何在ansible中迭代库存组

如何在ansible中迭代库存组

本文介绍了如何在ansible中迭代库存组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了一些与我的问题类似的 SO 帖子:Ansible : iterate在库存组Ansible 迭代主机在由变量设置的库存组中 和其他人,但我仍然不明白我做错了什么:

I have looked at some of the SO post that are similar to my question: Ansible : iterate over inventory groups, Ansible iterate over hosts in inventory group set by variable and others but I still don't understand what I am doing wrong:

这是我的库存文件:

[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

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

Here is the part of my playbook that is having trouble:

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

这是我使用该清单文件运行剧本时收到的错误消息:

Here is the error message I get when I run the playbook with that inventory file:

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

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

What am I doing wrong here? thanks

推荐答案

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

ok I figured it out. I needed to write that task like so:

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

这篇关于如何在ansible中迭代库存组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:12