Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 如何让Ansible使用with_项执行多个命令?_Shell_Ansible - Fatal编程技术网

Shell 如何让Ansible使用with_项执行多个命令?

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

我想配置为允许使用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.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列表