【发布时间】:2020-04-06 14:02:00
【问题描述】:
我正在尝试修改我的 ufw 防火墙剧本,以便它接受清单中的主机组列表,允许连接。 它适用于一组,但这对我的需求不够灵活。 但是当我尝试这个时,它就不起作用(我没想到它会起作用,但我只是不知道,该怎么做):
- name: allow only specified groups of servers to connect to any port
ufw:
rule: allow
src: "{{ hostvars[item]['ansible_default_ipv4']['address']}}"
with_items:
- groups.{{ ufw_allow_groups }}
变量被定义为一个列表:
ufw_allow_groups:
- group_of_hosts1
- group_of_hosts2
错误信息:
FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['groups.['group_of_hosts1', 'group_of_hosts']']\" is undefined
关于如何实现这一点的任何想法? 感谢您的任何意见!
【问题讨论】:
-
你的变量是
ufw_allow_groups,那你为什么要循环使用groups.{{ ufw_allow_groups }}呢?即使是这样,也应该是{{ groups.ufw_allow_groups }} -
我刚试过,但现在它抱怨字典属性丢失。致命:[shtest.phplist.com]:失败! => {"msg": "'dict object' has no attribute 'ufw_allow_groups'"} 也许我会尝试使用字典变量而不是列表。
-
我在假设您的变量看起来如何时给出了该评论。发布你的变量!