Hello All,
I have a script below that will get the instance ID and the subnet ID and then out put the information. I am using the module ec2_instance_info https://docs.ansible.com/ansible/latest/modules/ec2_instance_info_module.html#ec2-instance-info-module. I am able to get the instance ID information, but not able to get the subnet Id. None of the following worked. Can you please assist?
-
name: setting fact for subnet ID
set_fact: SUBNETID=“{{ item.subnet_id }}”
with_items: “{{ ec2_facts.instances.network_interfaces }}” -
name: setting fact for subnet ID
set_fact: SUBNETID=“{{ item.subnet_id }}”
with_items: “{{ ec2_facts.instances }}” -
name: setting fact for subnet ID
set_fact: SUBNETID=“{{ item.subnet_id }}”
with_items: “{{ ec2_facts.instances[network_interfaces] }}”
My data is below:
`
{‘root_device_type’: ‘ebs’, ‘private_dns_name’: ‘ip-10-0-0-147.ec2internal’, ‘cpu_options’: {‘core_count’: 1, ‘threads_per_core’: 2}, ‘source_dest_check’: True, ‘state_reason’: {‘message’: ‘Client.UserInitiatedShutdown: User initiated shutdown’, ‘code’: ‘Client.UserInitiatedShutdown’}, ‘monitoring’: {‘state’: ‘disabled’}, ‘subnet_id’: ‘subnet-’, ‘ebs_optimized’: True, ‘iam_instance_profile’: {‘id’: ‘AIPA2VVODOXJDPDZDA6L3’, ‘arn’: ‘arn:aws:iam:::instance-profile/’}, ‘state’: {‘code’: 80, ‘name’: ‘stopped’}, ‘security_groups’: [{‘group_id’: ‘sg-’, ‘group_name’: ‘EFDEVProject-VPC0API1SecurityGroup-1516O70HNUR1L’}], ‘client_token’: ‘’, ‘virtualization_type’: ‘hvm’, ‘architecture’: ‘x86_64’, ‘public_ip_address’: ‘’, ‘tags’: {‘Name’: ‘tmp api1’}, ‘key_name’: ‘’, ‘image_id’: ‘ami-’, ‘ena_support’: True, ‘hibernation_options’: {‘configured’: False}, ‘capacity_reservation_specification’: {‘capacity_reservation_preference’: ‘open’}, ‘public_dns_name’: ‘ec2-52-202-.compute-1.amazonaws.com’, ‘block_device_mappings’: [{‘device_name’: ‘/dev/xvda’, ‘ebs’: {‘status’: ‘attached’, ‘delete_on_termination’: True, ‘attach_time’: ‘2020-05-18T23:48:08+00:00’, ‘volume_id’: ‘vol-014ccbb674c86e09c’}}], ‘placement’: {‘group_name’: ‘’, ‘tenancy’: ‘default’, ‘availability_zone’: ‘us-east-1d’}, ‘ami_launch_index’: 0, ‘hypervisor’: ‘xen’, ‘network_interfaces’: [{‘status’: ‘in-use’, ‘description’: ‘Primary network interface’, ‘subnet_id’: ‘subnet-0c0d820109’, ‘source_dest_check’: True, ‘ipv6_addresses’: , ‘network_interface_id’: ‘eni-’, ‘private_dns_name’: ‘ip-10-0-0-147.ec2.internal’, ‘attachment’: {‘status’: ‘attached’, ‘device_index’: 0, ‘attachment_id’: ‘eni-attach-09f06b6dea5d4b8db’, ‘delete_on_termination’: True, ‘attach_time’: ‘2020-05-18T23:48:07+00:00’}, ‘private_ip_addresses’: [{‘private_ip_address’: ‘10.0.0.147’, ‘private_dns_name’: ‘ip-10-0-0-147.ec2.internal’, ‘association’: {‘public_ip’: ‘’, ‘public_dns_name’: ‘ec2-52-202-.compute-1.amazonaws.com’, ‘ip_owner_id’: ‘733730797010’}, ‘primary’: True}], ‘mac_address’: ‘0a:50:87:ab:46:ef’, ‘private_ip_address’: ‘10.0.0.147’, ‘vpc_id’: ‘vpc-’, ‘groups’: [{‘group_id’: ‘sg-’, ‘group_name’: ‘EFDEVProject–1516O70HNUR1L’}], ‘association’: {‘public_ip’: ‘’, ‘public_dns_name’: ‘ec2-52-202-254-155.compute-1.amazonaws.com’, ‘ip_owner_id’: ‘733730797010’}, ‘owner_id’: ‘733730797010’}], ‘launch_time’: ‘2020-05-19T12:00:00+00:00’, ‘instance_id’: ‘i-’, ‘instance_type’: ‘m5.large’, ‘root_device_name’: ‘/dev/xvda’, ‘state_transition_reason’: ‘User initiated (2020-05-20 00:18:14 GMT)’, ‘private_ip_address’: ‘10.0.0.147’, ‘vpc_id’: ‘vpc-’, ‘product_codes’: }
`