Ansible:强化列表以填充字典

Ansible: fortigate list to popolate a dictionary

remote_address_phase2:
  - 192.168.88.0/24
  - 192.168.1.0/24

任务:

i have to create a firewall group 

  - name: "addrgrp"
    fortios_firewall_addrgrp:
      vdom:  "{{ vdom }}"
      state: "present"
      firewall_addrgrp:
        allow_routing: "disable"
        #category: "default"
        color: "21"
        comment: "try"
        exclude: "disable"
        fabric_object: "disable"
        member:
          - name: "NET-{{ item}}"
      
        name: "try"

        type: "default"

    with_items:  "{{  remote_address_phase2 }}"

如果我做了这个 activity 我有 2 个不同的任务,但最后一个操作会覆盖第一个

有什么想法吗?

按照 module 文档中的示例,member: 参数似乎采用了字典列表。

来自模块文档的示例:

        member:
         -
            name: "default_name_7 (source firewall.address.name firewall.addrgrp.name)"

还没有测试过,但是我们可以在 "addrgrp" 任务之前用 set_fact 创建一个类似的结构,并使用新创建的变量。

    - set_fact:
        fw_members: "{{ fw_members | default([]) + [{'name': 'NET-' ~ item}] }}"
      loop: "{{ remote_address_phase2 }}"

这给出:

    "fw_members": [
        {
            "name": "NET-192.168.88.0/24"
        },
        {
            "name": "NET-192.168.1.0/24"
        }
    ]

然后应该可以将此变量作为值传递给 member: 参数。示例:

  - set_fact:
      fw_members: "{{ fw_members | default([]) + [{'name': 'NET-' ~ item}] }}"
    loop: "{{ remote_address_phase2 }}"

  - name: "addrgrp"
    fortios_firewall_addrgrp:
      vdom:  "{{ vdom }}"
      state: "present"
      firewall_addrgrp:
        allow_routing: "disable"
        #category: "default"
        color: "21"
        comment: "try"
        exclude: "disable"
        fabric_object: "disable"
        member: "{{ fw_members }}"
        name: "try"
        type: "default"