# Parsing results

**URL:** <https://forum.ansible.com/t/parsing-results/33023>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [May 26, 2020, 12:20am UTC](https://forum.ansible.com/t/parsing-results/33023 "2020-05-26T00:20:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [May 26, 2020, 12:20am UTC](https://forum.ansible.com/t/parsing-results/33023/1 "2020-05-26T00:20:35Z")

</div>

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](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](http://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’: }

`

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 26, 2020, 6:44am UTC](https://forum.ansible.com/t/parsing-results/33023/2 "2020-05-26T06:44:29Z")

</div>

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 }}”.

[https://docs.ansible.com/ansible/latest/plugins/inventory/aws\_ec2.html](https://docs.ansible.com/ansible/latest/plugins/inventory/aws_ec2.html)

---

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [May 26, 2020, 10:50pm UTC](https://forum.ansible.com/t/parsing-results/33023/3 "2020-05-26T22:50:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 26, 2020, 11:41pm UTC](https://forum.ansible.com/t/parsing-results/33023/4 "2020-05-26T23:41:26Z")

</div>

> - name: setting fact for subnet ID  
> &nbsp;&nbsp;&nbsp;set\_fact: SUBNETID="{{ item.subnet\_id }}"  
> &nbsp;&nbsp;&nbsp;with\_items: "{{ ec2\_facts.instances.network\_interfaces }}"

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

&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ec2\_facts.instances.network\_interfaces

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

---

<div class="post-metadata">

**Author:** ![ChrisV](https://avatars.discourse-cdn.com/v4/letter/c/f9ae1b/32.png) [@ChrisV](https://forum.ansible.com/u/ChrisV)\
**Post date:** [May 26, 2020, 11:47pm UTC](https://forum.ansible.com/t/parsing-results/33023/5 "2020-05-26T23:47:22Z")

</div>

`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

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 26, 2020, 11:52pm UTC](https://forum.ansible.com/t/parsing-results/33023/6 "2020-05-26T23:52:16Z")

</div>

> `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.

---

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [May 28, 2020, 2:28pm UTC](https://forum.ansible.com/t/parsing-results/33023/7 "2020-05-28T14:28:13Z")

</div>

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

[https://docs.ansible.com/ansible/latest/modules/ec2\_instance\_info\_module.html](https://docs.ansible.com/ansible/latest/modules/ec2_instance_info_module.html)

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 28, 2020, 2:37pm UTC](https://forum.ansible.com/t/parsing-results/33023/8 "2020-05-28T14:37:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [May 29, 2020, 5:19pm UTC](https://forum.ansible.com/t/parsing-results/33023/9 "2020-05-29T17:19:38Z")

</div>

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 }}”
