ansible - with_items 中的多个变量出错
问题描述
我创建了以下剧本来设置 ufw 设置。
---
- name: setup ufw for multi ports
hosts: db
become: yes
tasks:
- name: 'Allow all access for multi ports'
community.general.ufw:
rule: allow
port: "{{ item.port_num }}"
src: "{{ item.dest_ip }}"
with_items:
- { port_num: "33787", dest_ip: "{{web_ip_band}}" }
这是我的group_vars
文件。
web_ip_band:
- '192.168.101.13/24'
- '192.168.101.44/24'
当我执行这个剧本时,我得到了这个错误。
失败:[dbserver01] (item={'port_num': '33787', 'dest_ip': ['192.168.101.13/24', '192.168.101.44/24']}) => {"ansible_loop_var": "item" , "已更改": false, "命令": ["/usr/sbin/ufw 状态详细", "/bin/grep -h '^### tuple' /lib/ufw/user.rules /lib/ufw/ user6.rules /etc/ufw/user.rules /etc/ufw/user6.rules /var/lib/ufw/user.rules /var/lib/ufw/user6.rules", "/usr/sbin/ufw -- version", "/usr/sbin/ufw 允许从 ['192.168.101.13/24', '192.168.101.44/24'] 到任何端口 33787"], "item": {"dest_ip": ["192.168.101.13 /24", "192.168.101.44/24"], "port_num": "33787"}, "msg": "错误:参数数量错误\n"}
我的剧本中是否存在语法错误?
解决方案
来自community.general.ufw
模块文档(重新排列以适合 SO 答案的提取物)
from_ip (别名: from, src )
字符串- 默认值:“任何”
您正在传递一个IP列表,它解释了您的错误消息:
错误:参数数量错误
您必须为 中的每个条目组合port_num
和单个条目执行该任务dest_ip
。你需要的是一个子元素循环:
- name: 'Allow all access for multi ports'
community.general.ufw:
rule: allow
port: "{{ item.0.port_num }}"
src: "{{ item.1 }}"
vars:
my_rules:
- { port_num: "33787", dest_ip: "{{web_ip_band}}" }
loop: "{{ my_rules | subelements('dest_ip') }}"
推荐阅读
- jupyter-notebook - 无法以 root 身份启动笔记本
- c# - 选择所有行(大约 200 行)时 WPF DataGrid 很慢
- c# - 为什么 Visual Studio 代码样式设置认为 DateTime 不是内置类型,但 Guid 的行为就像它一样?
- react-native-navigation - 更改所选 TopTab 边框颜色的颜色
- c# - DropDownList 项目在每次回发后重复其值
- spring-boot - 使用 spring boot gradle 插件无法使用依赖管理执行 junit5 测试
- ios - 通过 iOS Outlook 发送带附件的电子邮件
- java - Collectors.toMap 在对象的不同属性上编写合并函数,而不是不用作值的属性
- node.js - 如果数据库中不存在数组字段值,如何推送数组字段值?
- angular - component.ts 文件中的错误限制了角度渲染