【问题标题】:How do you add tags to AWS security groups through Ansible?如何通过 Ansible 向 AWS 安全组添加标签?
【发布时间】:2019-05-27 22:00:50
【问题描述】:

我正在尝试通过 Ansible 管理 AWS 安全组,并希望向它们添加标签。 谁能给我一个例子来说明如何做到这一点?

例如,我有一个安全组“test_security_group”,我想向该安全组添加一个标签“foo”。

根据 Ansible 文档,ec2_tag 模块可以工作,但到目前为止我还没有成功地将它与安全组一起使用。

【问题讨论】:

    标签: amazon-web-services ansible


    【解决方案1】:

    像这样:

    - name: Create security group for app instances
      local_action:
        module: ec2_group
        name: "http-everywhere"
        description: "My Security Group"
        vpc_id: "vpc=abcd1234"
        region: "us-east-1"
        rules: 
          - proto: tcp
            from_port: 80
            to_port: 80
            cidr_ip: 0.0.0.0/0
      register: aws_sg
    
    - name: Tag the security group with a name
      local_action:
        module: ec2_tag
        resource: "{{aws_sg.group_id}}"
        region: "us-east-1"
        state: present
        tags:
          Name: "My Security Group Name"
          env: "production"
          service: "web"
    

    【讨论】:

    • 我总是知道该组不存在,即使它确实存在(记录状态和 aws 控制台),任何想法为什么或如何重试直到成功?
    【解决方案2】:

    从 ansible 2.4 开始,您可以直接指定标签

    - name: Create ec2 security group
      ec2_group:
        name: SSH
        description: SSH
        vpc_id: "{{ default_vpc_id }}"
        region: "{{ aws_region }}"
        tags:
          Name: SSH
          Tag1: Value1
          Tag2: Value2
        rules:
          - proto: tcp
            ports:
              - 22
            cidr_ip: 0.0.0.0/0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-01
      • 2021-05-08
      • 2020-10-09
      • 2016-12-25
      • 2023-03-17
      • 2019-02-17
      • 2015-01-06
      • 1970-01-01
      相关资源
      最近更新 更多