gpt4 book ai didi

linux - 使用 ansible 启动配置模块 ec2_lc 和安全组名称与 id

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:23:58 26 4
gpt4 key购买 nike

我想在 aws ec2 中完成以下任务:

  • 使用 ansible 模块 ec2_group 创建安全组。
  • 使用 ansible 模块 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 }}"

最佳答案

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

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

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

关于linux - 使用 ansible 启动配置模块 ec2_lc 和安全组名称与 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38459240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com