Linux 使用ansible启动配置模块ec2_lc和securitygroup名称与id

Linux 使用ansible启动配置模块ec2_lc和securitygroup名称与id,linux,amazon-ec2,ansible,Linux,Amazon Ec2,Ansible,我希望在aws ec2中完成以下工作: 使用ansible模块ec2_group创建安全组 使用ansible module ec2_lc创建启动配置,并连接先前创建的安全组 现在,我想使用安全组名称而不是id,因为我希望能够在需要时使用ansible重新创建整个基础结构 重新创建安全组将导致组的id不同。 但是ec2_lc模块只接受安全组id 是否有任何方法可以将安全组id映射到名称 我对安全组的定义如下: - name: create ec2 group ec2_group:

我希望在aws ec2中完成以下工作:

  • 使用ansible模块ec2_group创建安全组
  • 使用ansible module ec2_lc创建启动配置,并连接先前创建的安全组
现在,我想使用安全组名称而不是id,因为我希望能够在需要时使用ansible重新创建整个基础结构

重新创建安全组将导致组的id不同。 但是ec2_lc模块只接受安全组id

是否有任何方法可以将安全组id映射到名称

我对安全组的定义如下:

- name: create ec2 group
  ec2_group:
    name: "{{ item.name }}"
    description: "{{ item.description }}"
    vpc_id: "{{ item.vpc_id }}"
    region: "{{ item.region }}"
    state: present
    rules: "{{ item.rules }}"
    rules_egress: "{{ item.rules_egress }}"
  register: sg
- name: Create Launch Configuration
  ec2_lc:
    region: "{{ item.region }}"
    name: "{{ item.name }}"
    image_id: "{{ item.image_id }}"
    key_name: "{{ item.key_name }}"
    security_groups: "{{ item.security_groups }}" # how can i refer to specific group_id based on a group name?
    instance_type: "{{ item.instance_type }}"
    user_data: "{{ item.ec2_user_data }}"
    instance_profile_name: "{{ item.instance_profile_name }}"
    assign_public_ip: "{{ item.assign_public_ip }}"
启动配置代码如下所示:

- name: create ec2 group
  ec2_group:
    name: "{{ item.name }}"
    description: "{{ item.description }}"
    vpc_id: "{{ item.vpc_id }}"
    region: "{{ item.region }}"
    state: present
    rules: "{{ item.rules }}"
    rules_egress: "{{ item.rules_egress }}"
  register: sg
- name: Create Launch Configuration
  ec2_lc:
    region: "{{ item.region }}"
    name: "{{ item.name }}"
    image_id: "{{ item.image_id }}"
    key_name: "{{ item.key_name }}"
    security_groups: "{{ item.security_groups }}" # how can i refer to specific group_id based on a group name?
    instance_type: "{{ item.instance_type }}"
    user_data: "{{ item.ec2_user_data }}"
    instance_profile_name: "{{ item.instance_profile_name }}"
    assign_public_ip: "{{ item.assign_public_ip }}"
对于问题,您可以尝试以下方法:

- name: Create Launch Configuration
  ec2_lc:
    ...
    security_groups: "{{ sg.results | selectattr('item.name','equalto',item) | join('',attribute='group_id') }}"
    ...
对于问题,您可以尝试以下方法:

- name: Create Launch Configuration
  ec2_lc:
    ...
    security_groups: "{{ sg.results | selectattr('item.name','equalto',item) | join('',attribute='group_id') }}"
    ...

您可以编写一个过滤器,它可以动态地为您进行AWSAPI调用。 例如,我的vars/main.yml中有类似的内容

public_sg_id: "{{ 'Public' |get_sg(public_vpc_id, aws_region) }}"
下面是过滤器的代码


您可以编写一个过滤器,它可以动态地为您进行AWSAPI调用。 例如,我的vars/main.yml中有类似的内容

public_sg_id: "{{ 'Public' |get_sg(public_vpc_id, aws_region) }}"
下面是过滤器的代码


使用ec2_group-facts按名称查询安全组:

- ec2_group_facts:
    filters:
      group-name:
      - "{{ sg.name }}"
  register: ec2sgs

- debug:
    msg: "{{ ec2sgs.security_groups | map(attribute='group_id')| list }}"

使用ec2_group-facts按名称查询安全组:

- ec2_group_facts:
    filters:
      group-name:
      - "{{ sg.name }}"
  register: ec2sgs

- debug:
    msg: "{{ ec2sgs.security_groups | map(attribute='group_id')| list }}"

我已经提交了一份PR,可以在安全组名称和ID之间进行混合。PR就在这里:我已经提交了一份PR,可以在安全组名称和ID之间进行混合。公共关系在这里: