尝试通过 ansible 剧本配置 UFW 时源地址错误

Bad source address when trying to configure UFW via ansible playbook

我是 Ansible 新手。我正在尝试编写一本剧本来配置 UFW。我的任务是这样写的:

 - name: Allow SSH in UFW
    ufw:
      rule: allow
      port: 22
      proto: tcp
      from_ip:
        - "{{ item }}"
    with_items:
      - 192.168.0.0/24
      - 10.200.3.0/24
      - 10.200.2.0/24

我在运行剧本时得到的结果是:

failed: [192.168.255.20] (item=192.168.0.0/24) => {"changed": false, "item": "192.168.0.0/24", "msg": "ERROR: Bad source address\n"}
failed: [192.168.255.20] (item=10.200.3.0/24) => {"changed": false, "item": "10.200.3.0/24", "msg": "ERROR: Bad source address\n"}
failed: [192.168.255.20] (item=10.200.2.0/24) => {"changed": false, "item": "10.200.2.0/24", "msg": "ERROR: Bad source address\n"}

我在 Ansible UFW 文档或 UFW 本身中找不到任何可以阻止它工作的内容。如果我删除 'with_items' 循环并分别输入每个 IP 子网,它们都可以工作,但这可能会导致一些非常长的剧本。谁能告诉我我做错了什么?

我使用的文档在这里:https://docs.ansible.com/ansible/latest/modules/ufw_module.html?highlight=ufw

编辑:在详细模式下包含来自 运行 的文本:

failed: [192.168.255.20] (item=10.200.2.0/24) => {
    "changed": false,
    "invocation": {
        "module_args": {
            "app": null,
            "comment": null,
            "default": null,
            "delete": false,
            "direction": null,
            "from_ip": "['10.200.2.0/24']",
            "from_port": null,
            "insert": null,
            "interface": null,
            "log": false,
            "logging": null,
            "port": 22,
            "proto": "tcp",
            "route": false,
            "rule": "allow",
            "state": null,
            "to_ip": "any",
            "to_port": "22"
        }
    },
    "item": "10.200.2.0/24",
    "msg": "ERROR: Bad source address\n"

问题是您将 list/array 传递给 from_ip,而它需要的是字符串。试试这个:

  - name: Allow SSH in UFW
    ufw:
      rule: allow
      port: 22
      proto: tcp
      from_ip: "{{ item }}"
    with_items:
      - 192.168.0.0/24
      - 10.200.3.0/24
      - 10.200.2.0/24