gather tag name about instance

24 views
Skip to first unread message

Colin Byrne

unread,
Jan 29, 2016, 6:40:32 PM1/29/16
to Ansible Project
Hey, 

So I want to access the tag name I set at initial ec2 creation to use it as my hostname throughout the server. To set the tag_name, I use a dictionary / sub_list as such:


server_group_settings:
  integ:
    security_groups: ['sg-81398ee4', 'sg-a6398ec3']
    image: ami-06116566
    instance_type: t2.medium #needs to match the base image size   
    region: us-west-1
    key_name: ansible_provisioning
    termination_protection: yes
    subnet_id: subnet-819f7cd8

server_groups
:
 
- type: integ
    servers
:
     
- { name: integ-bob, private_ip: 10.101.1.182 }
     
- { name: staging-steve, private_ip: 10.101.1.30 }
     
- { name: staging-mike, private_ip: 10.101.1.31 }
   prod
:
     
- { name: staging-joey, private_ip: 10.101.1.34 }


Once the server has been created, I want to be able to access the tag_name by gathering facts about the remote sever and pulling down the tag name. Unfortunately, I cannot connect with the hosts through a dynamic inventory, due to them being with a VPC. They do show up in the ec2.py --list, so if I were able to somehow register the results of 'ec2.py --list' as a variable, I would be able to find the tag_name by using the ip, which I can get through ec2_facts (or a couple other modules, I believe).

Unfortunately with the way I nest the server_groups dictionary (which is the only way i could get nesting so deep to work), the server name is not nested below the private ip. 

What is the best way to do this? should I keep a second list (very redundant) with the server name nested below? Here is my ec2 playbook:

---
- name: Launch instances based on server list
  ec2:
    key_name: "{{ server_group_settings[item.0.type].key_name }}"
    group_id: "{{ server_group_settings[item.0.type].security_groups }}"
    instance_type: "{{ server_group_settings[item.0.type].instance_type }}"
    image: "{{ server_group_settings[item.0.type].image  }}"
    termination_protection: "{{ server_group_settings[item.0.type].termination_protection }}"
    wait: true
    region: "{{ server_group_settings[item.0.type].region }}"
    instance_tags:
      Type: "{{ item.0.type}}"
      Name: "{{ item.1.name }}"
    exact_count: 1  ## !! CAREFUL. Increasing this will likely create duplicates of all instances. 
    count_tag:
      Name: "{{ item.1.name }}"
    vpc_subnet_id: "{{ server_group_settings[item.0.type].subnet_id}}"
    assign_public_ip: no
    private_ip: "{{ item.1.private_ip }}"
  register: ec2
  with_subelements:
     - server_groups
     - servers

Sincere thanks for any help. It is greatly appreciated. 




Reply all
Reply to author
Forward
0 new messages