如何在ansible中遍历库存组? [英] How to iterate over inventory group in ansible?

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

问题描述

我看过一些与我的问题类似的SO帖子: 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中遍历库存组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆