【问题标题】:Multiple variables in with_items made errorwith_items 中的多个变量出错
【发布时间】:2023-03-29 18:49:01
【问题描述】:

我创建了以下剧本来设置 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", "changed": false, "commands": ["/usr/sbin/ufw status verbose", "/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"}

我的剧本有语法错误吗?

【问题讨论】:

    标签: ansible yaml


    【解决方案1】:

    来自community.general.ufw module documentation(摘录重新排列以适应 SO 答案)

    from_ip(别名:from, src

    字符串 - 默认:“任意”

    您正在传递一个 IP 的列表,它解释了您的错误消息:

    错误:参数数量错误

    您必须为port_numdest_ip 中的各个条目的每个组合执行该任务。你需要的是subelements loop:

     - 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') }}"
               
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      • 2022-07-05
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      相关资源
      最近更新 更多