Skip to content

Instantly share code, notes, and snippets.

@SolomonHD
Last active July 6, 2018 14:10
Show Gist options
  • Save SolomonHD/4f7ee46ff91e2b82142c2f00f76db7d7 to your computer and use it in GitHub Desktop.
Save SolomonHD/4f7ee46ff91e2b82142c2f00f76db7d7 to your computer and use it in GitHub Desktop.
EC2 Name Mapping
---
- name: Build list of instance IDs by instance name
ec2_instance_facts:
region: "{{ region }}"
filters:
"tag:NightlyBackup": "Yes"
# "tag:Name": "{{ groups['backup'] }}"
register: ec2_facts
- debug:
var: ec2_facts
- name: Set fact instances
set_fact:
instances: "{{ ec2_facts.instances | map(attribute='instance_id') | list }}"
- name: Map Name
set_fact:
map_name: "{{ ec2_facts.instances | map(attribute='tags.Name') | list }}"
- debug:
var: map_name
#- name: Replace undefined
# set_fact:
# "{{ item[0] }}": "{{ item[1] }}"
# when: item[0] == "Undefined"
# with_together:
# - "{{ map_name }}"
# - "{{ instances }}"
- debug:
var: instances
- meta: end_play
- name: List of instance IDs to be backed up
debug:
msg: "{{ item }}"
loop: "{{ instances }}"
"ec2_facts": {
"changed": false,
"failed": false,
"instances": [
{
"ami_launch_index": 0,
"architecture": "x86_64",
"block_device_mappings": [
{
"device_name": "/dev/sda1",
"ebs": {
"attach_time": "2018-06-07T23:24:18+00:00",
"delete_on_termination": true,
"status": "attached",
"volume_id": "vol-046b2e520c788971d"
}
}
],
"client_token": "",
"ebs_optimized": false,
"ena_support": true,
"hypervisor": "xen",
"iam_instance_profile": {
"arn": "arn:aws:iam::229792048549:instance-profile/ec2-s3-full-access",
"id": "AIPAID3GG2QMCRY7TOW56"
},
"image_id": "ami-43a15f3e",
"instance_id": "i-0468fc479c884d8b7",
"instance_type": "t2.medium",
"key_name": "general_key",
"launch_time": "2018-07-01T01:48:30+00:00",
"monitoring": {
"state": "disabled"
},
"network_interfaces": [
{
"attachment": {
"attach_time": "2018-06-07T23:24:16+00:00",
"attachment_id": "eni-attach-4a27dcef",
"delete_on_termination": true,
"device_index": 0,
"status": "attached"
},
"description": "Primary network interface",
"groups": [
{
"group_id": "sg-99882ad1",
"group_name": "sshd"
},
{
"group_id": "sg-fc64c6b4",
"group_name": "http+https"
}
],
"ipv6_addresses": [],
"mac_address": "12:e6:a5:c2:d9:8a",
"network_interface_id": "eni-117ea387",
"owner_id": "229792048549",
"private_dns_name": "ip-172-31-95-229.ec2.internal",
"private_ip_address": "172.31.95.229",
"private_ip_addresses": [
{
"primary": true,
"private_dns_name": "ip-172-31-95-229.ec2.internal",
"private_ip_address": "172.31.95.229"
}
],
"source_dest_check": true,
"status": "in-use",
"subnet_id": "subnet-66f7984a",
"vpc_id": "vpc-bc7408c5"
}
],
"placement": {
"availability_zone": "us-east-1a",
"group_name": "",
"tenancy": "default"
},
"private_dns_name": "ip-172-31-95-229.ec2.internal",
"private_ip_address": "172.31.95.229",
"product_codes": [],
"public_dns_name": "",
"root_device_name": "/dev/sda1",
"root_device_type": "ebs",
"security_groups": [
{
"group_id": "sg-99882ad1",
"group_name": "sshd"
},
{
"group_id": "sg-fc64c6b4",
"group_name": "http+https"
}
],
"source_dest_check": true,
"state": {
"code": 80,
"name": "stopped"
},
"state_reason": {
"code": "Client.UserInitiatedShutdown",
"message": "Client.UserInitiatedShutdown: User initiated shutdown"
},
"state_transition_reason": "User initiated (2018-07-01 01:49:14 GMT)",
"subnet_id": "subnet-66f7984a",
"tags": {
"Name": "solomon-test-2",
"NightlyBackup": "Yes"
},
"virtualization_type": "hvm",
"vpc_id": "vpc-bc7408c5"
},
{
"ami_launch_index": 0,
"architecture": "x86_64",
"block_device_mappings": [
{
"device_name": "/dev/sda1",
"ebs": {
"attach_time": "2018-06-12T19:45:14+00:00",
"delete_on_termination": true,
"status": "attached",
"volume_id": "vol-0d7fc053396341fbc"
}
}
],
"client_token": "",
"ebs_optimized": false,
"ena_support": true,
"hypervisor": "xen",
"iam_instance_profile": {
"arn": "arn:aws:iam::229792048549:instance-profile/ec2-s3-full-access",
"id": "AIPAID3GG2QMCRY7TOW56"
},
"image_id": "ami-43a15f3e",
"instance_id": "i-04ff7bb389bdf86f0",
"instance_type": "t2.xlarge",
"key_name": "general_key",
"launch_time": "2018-07-01T01:48:31+00:00",
"monitoring": {
"state": "disabled"
},
"network_interfaces": [
{
"attachment": {
"attach_time": "2018-06-12T19:45:14+00:00",
"attachment_id": "eni-attach-140c66ab",
"delete_on_termination": true,
"device_index": 0,
"status": "attached"
},
"description": "",
"groups": [
{
"group_id": "sg-99882ad1",
"group_name": "sshd"
},
{
"group_id": "sg-fc64c6b4",
"group_name": "http+https"
}
],
"ipv6_addresses": [],
"mac_address": "0e:2a:97:0c:aa:9a",
"network_interface_id": "eni-ce1c7154",
"owner_id": "229792048549",
"private_dns_name": "ip-172-31-32-142.ec2.internal",
"private_ip_address": "172.31.32.142",
"private_ip_addresses": [
{
"primary": true,
"private_dns_name": "ip-172-31-32-142.ec2.internal",
"private_ip_address": "172.31.32.142"
}
],
"source_dest_check": true,
"status": "in-use",
"subnet_id": "subnet-9a2543c0",
"vpc_id": "vpc-bc7408c5"
}
],
"placement": {
"availability_zone": "us-east-1c",
"group_name": "",
"tenancy": "default"
},
"private_dns_name": "ip-172-31-32-142.ec2.internal",
"private_ip_address": "172.31.32.142",
"product_codes": [],
"public_dns_name": "",
"root_device_name": "/dev/sda1",
"root_device_type": "ebs",
"security_groups": [
{
"group_id": "sg-99882ad1",
"group_name": "sshd"
},
{
"group_id": "sg-fc64c6b4",
"group_name": "http+https"
}
],
"source_dest_check": true,
"state": {
"code": 80,
"name": "stopped"
},
"state_reason": {
"code": "Client.UserInitiatedShutdown",
"message": "Client.UserInitiatedShutdown: User initiated shutdown"
},
"state_transition_reason": "User initiated (2018-07-01 01:49:14 GMT)",
"subnet_id": "subnet-9a2543c0",
"tags": {
"Name": "solomon-test",
"NightlyBackup": "Yes"
},
"virtualization_type": "hvm",
"vpc_id": "vpc-bc7408c5"
},
{
"ami_launch_index": 0,
"architecture": "x86_64",
"block_device_mappings": [
{
"device_name": "/dev/xvda",
"ebs": {
"attach_time": "2018-07-04T19:54:52+00:00",
"delete_on_termination": true,
"status": "attached",
"volume_id": "vol-0d6dbd54eeffd74e9"
}
}
],
"client_token": "",
"ebs_optimized": false,
"ena_support": true,
"hypervisor": "xen",
"iam_instance_profile": {
"arn": "arn:aws:iam::229792048549:instance-profile/ec2-s3-full-access",
"id": "AIPAID3GG2QMCRY7TOW56"
},
"image_id": "ami-b70554c8",
"instance_id": "i-05736d2c52da72184",
"instance_type": "t2.micro",
"key_name": "general_key",
"launch_time": "2018-07-04T19:54:51+00:00",
"monitoring": {
"state": "disabled"
},
"network_interfaces": [
{
"attachment": {
"attach_time": "2018-07-04T19:54:51+00:00",
"attachment_id": "eni-attach-444ae1fe",
"delete_on_termination": true,
"device_index": 0,
"status": "attached"
},
"description": "",
"groups": [
{
"group_id": "sg-99882ad1",
"group_name": "sshd"
},
{
"group_id": "sg-fc64c6b4",
"group_name": "http+https"
}
],
"ipv6_addresses": [],
"mac_address": "12:90:09:b0:58:c6",
"network_interface_id": "eni-487c11eb",
"owner_id": "229792048549",
"private_dns_name": "ip-172-31-91-170.ec2.internal",
"private_ip_address": "172.31.91.170",
"private_ip_addresses": [
{
"primary": true,
"private_dns_name": "ip-172-31-91-170.ec2.internal",
"private_ip_address": "172.31.91.170"
}
],
"source_dest_check": true,
"status": "in-use",
"subnet_id": "subnet-66f7984a",
"vpc_id": "vpc-bc7408c5"
}
],
"placement": {
"availability_zone": "us-east-1a",
"group_name": "",
"tenancy": "default"
},
"private_dns_name": "ip-172-31-91-170.ec2.internal",
"private_ip_address": "172.31.91.170",
"product_codes": [],
"public_dns_name": "",
"root_device_name": "/dev/xvda",
"root_device_type": "ebs",
"security_groups": [
{
"group_id": "sg-99882ad1",
"group_name": "sshd"
},
{
"group_id": "sg-fc64c6b4",
"group_name": "http+https"
}
],
"source_dest_check": true,
"state": {
"code": 80,
"name": "stopped"
},
"state_reason": {
"code": "Client.UserInitiatedShutdown",
"message": "Client.UserInitiatedShutdown: User initiated shutdown"
},
"state_transition_reason": "User initiated (2018-07-05 04:27:10 GMT)",
"subnet_id": "subnet-66f7984a",
"tags": {
"NightlyBackup": "Yes"
},
"virtualization_type": "hvm",
"vpc_id": "vpc-bc7408c5"
}
]
}
}
TASK [gather-ec2-facts : Set fact instances] **********************************************************************************************************************************************************************
ok: [localhost] => {"ansible_facts": {"instances": ["i-0468fc479c884d8b7", "i-04ff7bb389bdf86f0", "i-05736d2c52da72184"]}, "changed": false}
TASK [gather-ec2-facts : Map Name] ********************************************************************************************************************************************************************************
ok: [localhost] => {"ansible_facts": {"map_name": "[u'solomon-test-2', u'solomon-test', Undefined]"}, "changed": false}
TASK [gather-ec2-facts : debug] ***********************************************************************************************************************************************************************************
ok: [localhost] => {
"map_name": "[u'solomon-test-2', u'solomon-test', Undefined]"
}
TASK [gather-ec2-facts : debug] ***********************************************************************************************************************************************************************************
ok: [localhost] => {
"instances": [
"i-0468fc479c884d8b7",
"i-04ff7bb389bdf86f0",
"i-05736d2c52da72184"
]
}
@SolomonHD
Copy link
Author

Sry for the messy code. What I'm trying to do here is:
Use the instances map to create a list of instance IDs,
Use the Name query to build a list of Name tags-- but I have to account for the fact that it could be blank
Build a list that has the Name tag if it's there but the instance id if Name is blank

@amarao
Copy link

amarao commented Jul 5, 2018

Hello. It really hard to see what's going here without amazon account. Try to change ec2_instance_facts into set_fact or vars for ec2_facts with identical output. (use ec2_instance_facts and then just use debug: var=ec2_facts).

If you want to debug a problem, it's better to split it to few parts. Let's focus on query part.

@SolomonHD
Copy link
Author

I cleaned up the script a bit and added output. In my AWS Console I have 3 ec2: solomon-test-2, solomon-test and 1 w/o a Name tag. As you can see from the output I can get all 3 i-#'s fine, but the Undefined in the Name query is screwing things up.

@amarao
Copy link

amarao commented Jul 5, 2018

Thanks. It's night already, I will look at the lunch tomorrow. I not, poke me, please.

@amarao
Copy link

amarao commented Jul 6, 2018

Thanks for bringing up an interesting puzzle.

My solution (without analyzing why you are doing and what) is:

  - name: Map Name
    set_fact:
      map_name: "{{ ec2_facts.instances |  map(attribute='tags.Name') | select('defined')|list  }}"

It uses select filter which passes only those element of the list which are match for an argument. I pass 'defined' filter to reject all undefined elements.

I hope this will help. Anyway, nice puzzle, thanks.

@SolomonHD
Copy link
Author

Hey thanks for the help! It's a good step forward but I really wanted to replace undefined with some sort of placeholder. Then I can iterate through the list and replace the placeholder with the corresponding i-#.

The end result I want is a list that says: [solomon-test-2, solomon-test, i-05736d2c52da72184]

@amarao
Copy link

amarao commented Jul 6, 2018

Oh, that's simple too. There are filters map and default.

      map_name: "{{ ec2_facts.instances |  map(attribute='tags.Name') | map('default', 'foobar')|list  }}"

@SolomonHD
Copy link
Author

That's what I needed! I can manipulate the list like I wanted now! I looked up defaults in ansible and did not see this command. Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment