【问题标题】:How can I get the security group id associated with an ec2 instance using ansible如何使用 ansible 获取与 ec2 实例关联的安全组 ID
【发布时间】:2016-03-30 04:40:07
【问题描述】:

我正在尝试使用 Ansible 获取与 EC2 实例关联的安全组 ID。

它在 ec2_facts 中可用,但是变量名称包含"-" 分隔的 MAC 地址,这给我带来了问题。

不确定如何获取该信息。请问有人吗?

谢谢, Ankit

【问题讨论】:

  • 你到底得到了什么?你可以发布输出吗?我尝试时只得到了安全组。
  • 我正在尝试向实例添加新的安全组,首先我使用“ec2_group”创建一个新的安全组,然后(因为我没有获得任何 Ansible 方法来添加新的安全组一个正在运行的实例)使用 AWS CLI 和“原始”模块添加安全组。我面临的问题是我必须首先获取现有的安全组 ID,以便我可以将现有的 ID 和新的 ID 传递给 aws modify-instance-attribute。如果我只通过新的,它会删除旧的。
  • 好的。你还是没有回答我的问题。您从 ec2_facts 作为安全组得到什么。你提到它有一些额外的字符。发布您获得的值或 ec2_facts 输出的一部分
  • 当我调用 ec2_facts 时,我得到类似:“ansible_ec2_network_interfaces_macs_xx_xx_xx_xx_xx_xx_security_group_ids”:“sg-XXXXXXX”,其中 xx_xx_xx_xx_xx_xx 是机器的 MAC 地址。
  • 我可以获取安全组名称,但不能获取 sec 组 ID(很容易)。

标签: ubuntu amazon-web-services amazon-ec2 ansible


【解决方案1】:

刚刚想通了:

- set_fact:
    myval: "{{item['value']}}"
  when: item.key.find('security_group_ids') != -1
  with_dict: "{{facts.ansible_facts}}"

再次感谢您的帮助!你为我节省了很多时间!!

【讨论】:

  • 另一个相关问题,当附加了多个安全组时,它返回“sg-xxxxxxxx\nsg-xxxxxxxx”。如果它是 ',' 而不是 '\n' 生活会更简单。
【解决方案2】:

有点粗鲁,但能胜任。

  - name: Gather facts
    action: ec2_facts
    register: facts
  - set_fact: sg={{ item.value | regex_replace('\n', ',') }}
    when: item.key | search("security_group_ids")
    with_dict: facts.ansible_facts
  - debug: var=sg

【讨论】:

  • 太棒了!谢谢!只有一件事,我怎样才能将提取的值存储在一个单独的变量中以供进一步使用?
  • 刚刚想通了。
  • 我必须做一些小调整才能让它与 ansible v2.2.0.0 一起工作:with_dict: "{{ facts.ansible_facts }}"
猜你喜欢
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 1970-01-01
  • 2010-10-12
  • 2015-05-13
  • 1970-01-01
  • 2018-07-19
相关资源
最近更新 更多