在Ansible的EC2组中标记名称

时间:2015-06-02 02:52:45

标签: amazon-ec2 ansible-playbook

这是来自以下帖子的另一个问题......:

loops over the registered variable to inspect the results in ansible

所以基本上有:

    - name: EC2Group | Creating an EC2 Security Group inside the Mentioned VPC
       local_action:
         module: ec2_group
         name: "{{ item.sg_name }}"
         description: "{{ item.sg_description }}"
         region: "{{ vpc_region }}" # Change the AWS region here
         vpc_id: "{{ vpc.vpc_id }}" # vpc is the resgister name, you can also set it manually
         state: present
         rules: "{{ item.sg_rules }}"
       with_items: ec2_security_groups
       register: aws_sg
- name: Tag the security group with a name
  local_action:
   module: ec2_tag
   resource: "{{ item.group_id }}"
   region: "{{ vpc_region }}"
   state: present
   tags:
     Name: "{{vpc_name }}-group"
  with_items: aws_sg.results

我想知道如何获得TAG NAME

 tags:
     Name: "{{ item.sg_name }}"

与安全组上的主要名称定义相同的值?

   local_action:
     module: ec2_group
     name: "{{ item.sg_name }}"

我试图让这成为可能,但我不知道该怎么做。如果还可以检索该项目吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

运行ec2.py清单脚本后可以使用标记 - 它们的值始终为tag_key_value,其中' key'是标记的名称,'值'是该标记内的值。即如果您创建一个名为'应用程序'并给它一个值'#AwesomeApplication'你会得到' tag_Application_AwesomeApplication'。

也就是说,如果您刚刚创建了实例并希望针对这些新实例运行某些命令,请解析create instance命令的输出以获取IP地址列表,并将它们添加到临时组,然后您可以在同一个剧本中对该组运行命令:

...
    - name: add hosts to temporary group
      add_host: name="{{ item }}" groups=temporarygroup
      with_items: parsedipaddresses
- hosts: temporarygroup
  tasks:
    - name: awesome script to do stuff goes here
...