在Ansible AWS ec2_security_group中解析json以提取安全组ID

bybem2ql  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(108)

这是一个string/json/ansible解析问题。
大图:我正在创建一个VPC,添加一些安全组并创建一些VM。
下面的示例可以很好地处理如下所示的输出。但是,我需要标记新创建的安全组ID,以便可以使用它们来创建VM。

- name: Create an VPC and some security groups and show the security group ID
  hosts: localhost
  vars:
    security_groups: []
    aws_region: "us-east-1"

  tasks:
    - name: "Create VPC test in AWS"
      amazon.aws.ec2_vpc_net:
        name: "test_vpc"
        cidr_block: "10.5.2.0/24"
        state: present
        region: "us-east-1"
      register: create_vpc_output

    - name: "Set VPC ID as fact"
      ansible.builtin.set_fact:
        vpc_id: "{{ create_vpc_output.vpc.id }}"

    - name: "Configuring security groups for VPC {{ vpc_id }}"
      amazon.aws.ec2_security_group:
        name: "{{ item.value.name }}"
        description: "{{ item.value.description }}"
        tags:
          Name: "{{ item.value.name }}"
        state: present
        vpc_id: "{{ vpc_id }}"
        region: "{{ aws_region }}"
      loop: "{{ query('dict', vpc_security_groups) }}"
      register: "create_sg_output"

      vars:
        vpc_security_groups:
          private_test1:
            name: "test1"
            description: "Allow all connections"
          private_test2:
            name: "test2"
            description: "Allow all connections"

    - name: Extract group_id and group_name
      set_fact:
        sg_groups: "{{ create_sg_output.results | map(attribute='group_id') | zip(create_sg_output.results | map(attribute='group_name')) }}"

    - name: Display extracted groups
      debug:
        var: sg_groups

结果如下:

TASK [Display extracted groups] ****************************************************************************************
ok: [localhost] => {
    "sg_groups": [
        [
            "sg-08xxxxxxxxxxxxxxxx",
            "test1"
        ],
        [
            "sg-05xxxxxxxxxxxxxxxx",
            "test2"
        ]
    ]
}

我如何调整它,使输出像这样:

"sg_groups": [
        {
            "group_id" : "sg-08xxxxxxxxxxxxxx",
            "group_name" : "test1"
        },
        {
            "group_id": "sg-05xxxxxxxxxxxxxx",
            "group_name" : "test2"
        }
    ]
}

注意添加的“group_id”和“group_name”,这是从ec2_security_group调用返回的json中数据的标签。

yjghlzjz

yjghlzjz1#

还有更多的选择:

  • 使用过滤器json_query
sg_groups: "{{ create_sg_output.results|
                 json_query('[].{group_id: group_id,
                                 group_name: group_name}') }}"

给你想要的

sg_groups:
    [
        {
            "group_id": "sg-08",
            "group_name": "test1"
        },
        {
            "group_id": "sg-05",
            "group_name": "test2"
        }
    ]

此选项允许您重命名属性。详情请参见JMESPath Specification

  • 下一个选项是过滤器ansible.utils.keep_keys。下面的表达式给出了相同的结果
sg_groups: "{{ create_sg_output.results|
                 ansible.utils.keep_keys(target=['group_id', 'group_name']) }}"

用于测试的完整剧本示例

- hosts: localhost

  vars:

    create_sg_output:
      results:
        - group_id: sg-08
          group_name: test1
        - group_id: sg-05
          group_name: test2

    sg_groups: "{{ create_sg_output.results|map(attribute='group_id')|
                   zip(create_sg_output.results|map(attribute='group_name')) }}"

    sg_group2: "{{ create_sg_output.results|
                   json_query('[].{group_id: group_id,
                                   group_name: group_name}') }}"

    sg_group3: "{{ create_sg_output.results|
                   ansible.utils.keep_keys(target=['group_id', 'group_name']) }}"

  tasks:

    - debug:
        var: sg_groups

    - debug:
        var: sg_group2|to_nice_json

    - debug:
        var: sg_group3|to_nice_json
  • 如果 group_id 的值是唯一的,则最简单的选项是过滤器 items2dict
sg_groups_dict: "{{ create_sg_output.results|
                      items2dict(key_name='group_id',
                                 value_name='group_name') }}"

给字典

sg_groups_dict:
    sg-05: test2
    sg-08: test1

你可以把这本字典翻过来

- debug:
        msg: "{{ item.key }} {{ item.value }}"
      with_dict: "{{ sg_groups_dict }}"

gives(abridged)

ok: [localhost] => (item={'key': 'sg-08', 'value': 'test1'}) => 
  msg: sg-08 test1
ok: [localhost] => (item={'key': 'sg-05', 'value': 'test2'}) => 
  msg: sg-05 test2

如果你想将字典转换为列表,请使用过滤器 dict2items

sg_groups: "{{ sg_groups_dict|
                 dict2items(key_name='group_id',
                            value_name='group_name') }}"

给出了预期的结果

sg_groups:
    [
        {
            "group_id": "sg-08",
            "group_name": "test1"
        },
        {
            "group_id": "sg-05",
            "group_name": "test2"
        }
    ]

用于测试的完整剧本示例

- hosts: localhost

  vars:

    create_sg_output:
      results:
        - group_id: sg-08
          group_name: test1
        - group_id: sg-05
          group_name: test2

    sg_groups_dict: "{{ create_sg_output.results|
                        items2dict(key_name='group_id',
                                   value_name='group_name') }}"
    sg_groups: "{{ sg_groups_dict|
                   dict2items(key_name='group_id',
                              value_name='group_name') }}"

  tasks:

    - debug:
        var: sg_groups_dict

    - debug:
        var: sg_groups|to_nice_json

    - debug:
        msg: "{{ item.key }} {{ item.value }}"
      with_dict: "{{ sg_groups_dict }}"

相关问题