Parsing results

109 views
Skip to first unread message

Andrew Morgan

unread,
May 25, 2020, 8:20:35 PM5/25/20
to Ansible Project
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': []}



---
- hosts: dev
 become: yes
 #connection: local

  tasks:
#  - name: Install boto3 and botocore with pip3 module
#    pip:
#      name:
#      - boto3
#      - botocore
#      executable: pip3.6

  - name: Get Instance info
   ec2_instance_info:
     aws_access_key:
     aws_secret_key:
     region: us-east-1
   register: ec2_facts


  - name: setting fact for instance ID
   set_fact: INSTANCEID="{{ item.instance_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 }}"  


Dick Visser

unread,
May 26, 2020, 2:44:58 AM5/26/20
to ansible...@googlegroups.com
Have you considered using the ec2 dynamic inventory? With that all hosts will have the majority of ec2 related info populated as facts, so you can just reference them as "{{ ec2_id }}".




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2095ca5a-1312-473d-b306-6335dfc6897e%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Andrew Morgan

unread,
May 26, 2020, 6:50:38 PM5/26/20
to Ansible Project
thank you this is a good suggestion, however is beyond the scope of my project at the moment. So unluckily I will have to try and parse the variables.Anyone can assist?

Vladimir Botka

unread,
May 26, 2020, 7:41:48 PM5/26/20
to Andrew Morgan, ansible...@googlegroups.com
On Mon, 25 May 2020 17:20:35 -0700 (PDT)
Andrew Morgan <alonso...@gmail.com> wrote:

> - name: setting fact for subnet ID
> set_fact: SUBNETID="{{ item.subnet_id }}"
> with_items: "{{ ec2_facts.instances.network_interfaces }}"

This one looks promising. To move forward simple debug must work first

- debug:
var: ec2_facts.instances.network_interfaces

* It's difficult read the JSON blob.
* Is there really a leading space in ' network_interfaces' !?


> {'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':

ChrisV

unread,
May 26, 2020, 7:47:22 PM5/26/20
to Ansible Project
`network_interfaces` is a list. You will need to include the index of the interface, which is 0 in your example.

 Ex. "{{ ec2_facts.instances.network_interfaces[0] }}"

ChrisV

Vladimir Botka

unread,
May 26, 2020, 7:52:32 PM5/26/20
to ChrisV, ansible...@googlegroups.com
On Tue, 26 May 2020 16:47:22 -0700 (PDT)
ChrisV <ch...@cracklecode.com> wrote:

> `network_interfaces` is a list. You will need to include the index of the
> interface, which is 0 in your example.
>
> Ex. "{{ ec2_facts.instances.network_interfaces[0] }}"

The loop below would work in this case.

Andrew Morgan

unread,
May 28, 2020, 10:28:13 AM5/28/20
to Ansible Project
Thank you for you assistance , but I am not successful , I am getting the below error.

TASK [setting fact for subnet ID] **********************************************************************************
fatal: [deVPN]: FAILED! => 
  msg: '''list object'' has no attribute ''network_interfaces'''

PLAY RECAP *********************************************************************************************************
deVPN                      : ok=3    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   


I have tried :

  - name: setting fact for subnet ID
   set_fact: SUBNETID="{{ item.subnet_id }}"
   with_items: "{{ ec2_facts.instances.network_interfaces[0] }}"  

  - debug:
     var: ec2_facts.instances.network_interfaces
 and

  - name: setting fact for subnet ID
   set_fact: SUBNETID="{{ item.subnet_id }}"
   with_items: "{{ ec2_facts.instances.network_interfaces }}"  

  - debug:
     var: ec2_facts.instances.network_interfaces

And yes, the name network_instances is correct

Dick Visser

unread,
May 28, 2020, 10:38:28 AM5/28/20
to ansible...@googlegroups.com
On Thu, 28 May 2020 at 16:28, Andrew Morgan <alonso...@gmail.com> wrote:

> And yes, the name network_instances is correct
>
> https://docs.ansible.com/ansible/latest/modules/ec2_instance_info_module.html

I don't see any mention of that name? Typo?


Either way, IIRC ec2_facts.instances is a list, so you'd have to give
it an index.
For example:

ec2_facts.instances[0].network_interfaces

etc etc




--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Andrew Morgan

unread,
May 29, 2020, 1:19:39 PM5/29/20
to Ansible Project
That you very much this code below worked.

  - name: setting fact for subnet ID
   set_fact: SUBNETID="{{ item.subnet_id }}"
   with_items: "{{ ec2_facts.instances[0].network_interfaces }}"
   
  - name: setting fact for Security group
   set_fact: SERCURITYGROUP="{{ item.group_name }}"
   with_items: "{{ ec2_facts.instances[0].network_interfaces[0].groups }}"
Reply all
Reply to author
Forward
0 new messages