Shell 如何让Ansible使用with_项执行多个命令?
我想配置为允许使用Ansible从IP地址列表连接到端口80:Shell 如何让Ansible使用with_项执行多个命令?,shell,ansible,Shell,Ansible,我想配置为允许使用Ansible从IP地址列表连接到端口80: tasks: - name: Allow port 80 HTTP action: shell ufw allow from {{item}} to any 80/tcp with_items: allowed_ips IP地址列表作为散列存储在YAML文件中,在我的playbook的vars/目录中: --- allowed_ips: xxx.xxx.xxx.xxx xxx.xxx.xxx
tasks:
- name: Allow port 80 HTTP
action: shell ufw allow from {{item}} to any 80/tcp
with_items: allowed_ips
IP地址列表作为散列存储在YAML文件中,在我的playbook的vars/
目录中:
---
allowed_ips:
xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx
我正在使用with_items
将IP地址拉入命令,但当Ansible运行playbook时,它会连接IP地址,在每个IP之间插入一个空格:
ufw allow from xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx to any 80/tcp
ufw
不接受此语法,因此我想为每个IP地址运行一次ufw
。我如何才能做到这一点?Ansible的行为正确,因为当前允许的不是列表,而是单个变量。将变量设置为YAML列表:
---
allowed_ips:
- xxx.xxx.xxx.xxx
- xxx.xxx.xxx.xxx
现在,操作的执行次数与允许的\u ips
列表中的项目相同。Ansible的行为正确,因为当前允许的\u ips
不是列表,而是单个变量。将变量设置为YAML列表:
---
allowed_ips:
- xxx.xxx.xxx.xxx
- xxx.xxx.xxx.xxx
现在,该操作的执行次数与允许\u ips
列表中的项目相同。Ansible 1.6有一个模块来管理ufw
。它支持您试图实现的列表
ufw: rule=allow src={{ item }} port=80 proto=tcp
with_items:
- 10.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/16
或者,您可以尝试为ufw
提供几个随时可用的角色,其中一些角色还支持IP列表。Ansible 1.6有一个模块来管理ufw
。它支持您试图实现的列表
ufw: rule=allow src={{ item }} port=80 proto=tcp
with_items:
- 10.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/16
或者,您可以尝试为ufw
提供几个随时可用的角色,其中一些还支持IP列表