Ansible:多重循环进入 json 文件

Ansible : multy loop into json file

我有一个 json 文件作为源,其中包含 NetworkFlow 密钥列表,我想从中提取信息以创建安全规则,使用ansible中的双循环。

下面是我的 json 文件中的示例:

{
   "Name":"Some_name",
   "NetworkFlow":[
      {
         "GroupName":"Test1",
         "Type":"Ingress",
         "Env":"prod",
         "Server":[
            "192.168.1.1",
            "192.168.1.2"
         ],
         "Service":[
            {
               "Protocol":"TCP",
               "Port":"443,22,53"
            },
            {
               "Protocol":"UDP",
               "Port":"21"
            }
         ]
      },
      {
         "GroupName":"Test2",
         "Type":"Egress",
         "Env":"dev",
         "Server":[
            "192.168.1.3",
            "192.168.1.4"
         ],
         "Service":[
            {
               "Protocol":"UDP",
               "Port":"9996,9997"
            }
         ]
      }
   ]
}

所以首先我必须为每个 NetworkFlow 部分循环,在每个部分中,我必须在 servers 列表中循环并且还在 服务列表 (协议和端口)中获得如下类似的解析:

#rule= Server,Protocol,Port,Type,Env,GroupName
  msg: 192.168.1.1,TCP,443,Ingress,prod,Test1
  msg: 192.168.1.1,TCP,22,Ingress,prod,Test1
  msg: 192.168.1.1,TCP,53,Ingress,prod,Test1
  msg: 192.168.1.1,UDP,21,Ingress,prod,Test1
  msg: 192.168.1.2,TCP,443,Ingress,prod,Test1
  msg: 192.168.1.2,TCP,22,Ingress,prod,Test1
  msg: 192.168.1.2,TCP,53,Ingress,prod,Test1
  msg: 192.168.1.2,UDP,21,Ingress,prod,Test1
  
  msg: 192.168.1.3,UDP,9996,Egress,dev,Test2
  msg: 192.168.1.3,UDP,9997,Egress,dev,Test2
  msg: 192.168.1.4,UDP,9996,Egress,dev,Test2
  msg: 192.168.1.4,UDP,9997,Egress,dev,Test2

在我的剧本任务下方:

我的主要任务:

---
- name: Include JSON file
  include_vars:
    file: test.json

- include_tasks: rules.yml
  loop: "{{ NetworkFlow }}"
  loop_control:
    loop_var: oi

我的规则任务:

---
- set_fact:
    Services: "{{ Services|from_yaml }}"
  vars:
    Services: |
      {% for service in oi.Service %}
      {% for port in service.Port.split(',') %}
        - Protocol: {{ service.Protocol }}
          Port: {{ port }}
      {% endfor %}
      {% endfor %}

- debug:
    msg: "{{ i.0 }},{{ i.1.Protocol }},{{ i.1.Port }},{{ oi.Type }},{{ oi.Env }},{{ oi.GroupName }}"
  with_nested:
    - "{{ oi.Server }}"
    - "{{ Services }}"
  loop_control:
    loop_var: i

For info, my oi.Service.Port can have a list of port separated by a comma !

我尝试在 with_nested 中使用 loop 并且它适用于第一个键 Test1 ,但我没有得到第二个 NetworkFlowTest2

的正确解析
TASK [test : set_fact] *****************************************************************************************************************************************
ok: [localhost]

TASK [test : debug] ********************************************************************************************************************************************
    "msg": "192.168.1.1,TCP,443,Ingress,prod,Test1"
    "msg": "192.168.1.1,TCP,22,Ingress,prod,Test1"
    "msg": "192.168.1.1,TCP,53,Ingress,prod,Test1"
    "msg": "192.168.1.1,UDP,21,Ingress,prod,Test1"
    "msg": "192.168.1.2,TCP,443,Ingress,prod,Test1"
    "msg": "192.168.1.2,TCP,22,Ingress,prod,Test1"
    "msg": "192.168.1.2,TCP,53,Ingress,prod,Test1"
    "msg": "192.168.1.2,UDP,21,Ingress,prod,Test1"
}

TASK [test : set_fact] *****************************************************************************************************************************************
ok: [localhost]

TASK [test : debug] ********************************************************************************************************************************************
    "msg": "192.168.1.3,TCP,443,Egress,dev,Test2"
    "msg": "192.168.1.3,TCP,22,Egress,dev,Test2"
    "msg": "192.168.1.3,TCP,53,Egress,dev,Test2"
    "msg": "192.168.1.3,UDP,21,Egress,dev,Test2"
    "msg": "192.168.1.4,TCP,443,Egress,dev,Test2"
    "msg": "192.168.1.4,TCP,22,Egress,dev,Test2"
    "msg": "192.168.1.4,TCP,53,Egress,dev,Test2"
    "msg": "192.168.1.4,UDP,21,Egress,dev,Test2"

有人知道如何处理吗?

下面的任务创建列表服务,包括服务器

- set_fact:
    Services: "{{ Services|default([]) + Service|from_yaml }}"
  vars:
    Service: |
      {% for Port in item.1.Port.split(',') %}
      - {{ item.0 }},{{ item.1.Protocol }},{{ Port }},{{ oi.Type }},{{ oi.Env }},{{ oi.GroupName }}
      {% endfor %}
  with_nested:
    - "{{ oi.Server }}"
    - "{{ oi.Service }}"
  Services:
  - 192.168.1.1,TCP,443,Ingress,prod,Test1
  - 192.168.1.1,TCP,22,Ingress,prod,Test1
  - 192.168.1.1,TCP,53,Ingress,prod,Test1
  - 192.168.1.1,UDP,21,Ingress,prod,Test1
  - 192.168.1.2,TCP,443,Ingress,prod,Test1
  - 192.168.1.2,TCP,22,Ingress,prod,Test1
  - 192.168.1.2,TCP,53,Ingress,prod,Test1
  - 192.168.1.2,UDP,21,Ingress,prod,Test1
  - 192.168.1.3,UDP,9996,Egress,dev,Test2
  - 192.168.1.3,UDP,9997,Egress,dev,Test2
  - 192.168.1.4,UDP,9996,Egress,dev,Test2
  - 192.168.1.4,UDP,9997,Egress,dev,Test2