# Need help with how to grab public ip and use it for later part in playbook

**URL:** <https://forum.ansible.com/t/need-help-with-how-to-grab-public-ip-and-use-it-for-later-part-in-playbook/29946>\
**Category:** Ansible Project\
**Created:** [March 21, 2019, 9:48pm UTC](https://forum.ansible.com/t/need-help-with-how-to-grab-public-ip-and-use-it-for-later-part-in-playbook/29946 "2019-03-21T21:48:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tinkuchowdary32](https://avatars.discourse-cdn.com/v4/letter/t/e99b99/32.png) [@tinkuchowdary32](https://forum.ansible.com/u/tinkuchowdary32)\
**Post date:** [March 21, 2019, 9:48pm UTC](https://forum.ansible.com/t/need-help-with-how-to-grab-public-ip-and-use-it-for-later-part-in-playbook/29946/1 "2019-03-21T21:48:03Z")

</div>

here is the dubug output ec2-facts

“ec2”: {  
“changed”: false,  
“failed”: false,  
“instances”: [  
{  
“ami\_launch\_index”: 0,  
“architecture”: “x86\_64”,  
“block\_device\_mappings”: [  
{  
“device\_name”: “/dev/xvda”,  
“ebs”: {  
“attach\_time”: “2019-03-21T18:28:03+00:00”,  
“delete\_on\_termination”: true,  
“status”: “attached”,  
“volume\_id”: “vol-xxxx”  
}  
}  
],  
“client\_token”: “”,  
“cpu\_options”: {  
“core\_count”: 1,  
“threads\_per\_core”: 1  
},  
“ebs\_optimized”: false,  
“ena\_support”: true,  
“hibernation\_options”: {  
“configured”: false  
},  
“hypervisor”: “xen”,  
“image\_id”: “ami-xxxx”,  
“instance\_id”: “i-xxx”,  
“instance\_type”: “t2.micro”,  
“key\_name”: “xxxxx”,  
“launch\_time”: “2019-03-21T18:28:03+00:00”,  
“monitoring”: {  
“state”: “disabled”  
},  
“network\_interfaces”: [  
{  
“association”: {  
“ip\_owner\_id”: “amazon”,  
“public\_dns\_name”: “[ec2-xxxxx.us-east-2.compute.amazonaws.com](http://ec2-xxxxx.us-east-2.compute.amazonaws.com)”,  
“public\_ip”: “xx.xx.xx.51”  
},  
“attachment”: {  
“attach\_time”: “2019-03-21T18:28:03+00:00”,  
“attachment\_id”: “eni-attach-xxxxxx”,  
“delete\_on\_termination”: true,  
“device\_index”: 0,  
“status”: “attached”  
},  
“description”: “”,  
“groups”: [  
{  
“group\_id”: “sg-xx”,  
“group\_name”: “xxxxxx”  
}  
],  
“ipv6\_addresses”: ,  
“mac\_address”: “xx:66:xx:77:d4:f4”,  
“network\_interface\_id”: “eni-xxxxxxx”,  
“owner\_id”: “xxxxxx”,  
“private\_dns\_name”: “ip-xxxxxxx8.us-east-2.compute.internal”,  
“private\_ip\_address”: “1xxxxxx”,  
“private\_ip\_addresses”: [  
{  
“association”: {  
“ip\_owner\_id”: “amazon”,  
“public\_dns\_name”: “[ec2-xx-xxx-xxx-xx.us-east-2.compute.amazonaws.com](http://ec2-xx-xxx-xxx-xx.us-east-2.compute.amazonaws.com)”,  
“public\_ip”: "18.x.xx.xx  
},  
“primary”: true,  
“private\_dns\_name”: “xxxxxx.us-east-2.compute.internal”,  
“private\_ip\_address”: “xxxxxx”  
}  
],  
“source\_dest\_check”: true,  
“status”: “in-use”,  
“subnet\_id”: “subnet-xxxx”,  
“vpc\_id”: “vpc-xxxxx”  
}  
],  
“placement”: {  
“availability\_zone”: “us-east-2c”,  
“group\_name”: “”,  
“tenancy”: “default”  
},  
“private\_dns\_name”: “xxxxxxx.us-east-2.compute.internal”,  
“private\_ip\_address”: “xxx.xx.xx.xx”,  
“product\_codes”: ,  
“public\_dns\_name”: “[xxxxxxx.us-east-2.compute.amazonaws.com](http://xxxxxxx.us-east-2.compute.amazonaws.com)”,  
“public\_ip\_address”: “xx.xxx.44.51”,  
“root\_device\_name”: “/dev/xvda”,  
“root\_device\_type”: “ebs”,  
“security\_groups”: [  
{  
“group\_id”: “sg-xxxx”,  
“group\_name”: “xxxxxx”  
}  
],  
“source\_dest\_check”: true,  
“state”: {  
“code”: 64,  
“name”: “stopping”  
},  
“state\_reason”: {  
“code”: “Client.UserInitiatedShutdown”,  
“message”: “Client.UserInitiatedShutdown: User initiated shutdown”  
},  
“state\_transition\_reason”: “User initiated (2019-03-21 19:33:16 GMT)”,  
“subnet\_id”: “subnet-xxx”,  
“virtualization\_type”: “hvm”,  
“vpc\_id”: “vpc-xxxx”  
}  
]  
}  
}

i want to use debug option in playbook to print only the public ip

ec2.instances.public\_ip is not working

can anyone please help

---

<div class="post-metadata">

**Author:** ![Sebastian\_Meyer](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@Sebastian\_Meyer](https://forum.ansible.com/u/Sebastian_Meyer)\
**Post date:** [March 21, 2019, 10:10pm UTC](https://forum.ansible.com/t/need-help-with-how-to-grab-public-ip-and-use-it-for-later-part-in-playbook/29946/2 "2019-03-21T22:10:58Z")

</div>

On 21.03.19 22:48, tinkuchowdary32@gmail.com wrote:\> here is the dubug  
output ec2-facts

> "ec2": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"failed": false,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"instances": [  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ami\_launch\_index": 0,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"architecture": "x86\_64",

[..]

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"product\_codes": ,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"public\_dns\_name":  
> "xxxxxxx.us-east-2.compute.amazonaws.com",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"public\_ip\_address": "xx.xxx.44.51",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"root\_device\_name": "/dev/xvda",

[...]

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> }
> 
> i want to use debug option in playbook to print only the public ip
> 
> ec2.instances.public\_ip is not working

instances is an array of dicts here.

You'll need a 0 in there to access the first element of that list:

ec2.instances.0.public\_ip\_address

- Sebastian

---

<div class="post-metadata">

**Author:** ![tinkuchowdary32](https://avatars.discourse-cdn.com/v4/letter/t/e99b99/32.png) [@tinkuchowdary32](https://forum.ansible.com/u/tinkuchowdary32)\
**Post date:** [March 21, 2019, 10:19pm UTC](https://forum.ansible.com/t/need-help-with-how-to-grab-public-ip-and-use-it-for-later-part-in-playbook/29946/3 "2019-03-21T22:19:33Z")

</div>

Thank you sebastian for quick reply

unfortunately that didn’t work

here the part that i am running

- name: “gather instance facts”  
ec2\_instance\_facts:  
instance\_ids: i-xxxxxx  
region: “{{ region }}”  
delegate\_to: localhost  
when: ec2\_instance\_prov == ‘no’  
register: ec2  
tags:
- gather-facts
- provision-ec2
- debug:  
var: ec2.instances.0.public\_ip\_address  
tags:
- gather-facts

after i run this i see no output

PLA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\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“ec2.instances.0.public\_ip\_address”: “VARIABLE IS NOT DEFINED!”  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=2 changed=0 unreachable=0 failed=0

---

<div class="post-metadata">

**Author:** ![Sebastian\_Meyer](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@Sebastian\_Meyer](https://forum.ansible.com/u/Sebastian_Meyer)\
**Post date:** [March 22, 2019, 8:04am UTC](https://forum.ansible.com/t/need-help-with-how-to-grab-public-ip-and-use-it-for-later-part-in-playbook/29946/4 "2019-03-22T08:04:52Z")

</div>

Hi,

in this case verify that

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;var: ec2

works first.

Then try:

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;var: ec2.instances

Afterwards:

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;var: ec2.instances.0

So basically you try to build the path to the value you want. If one  
fails, please post the output of the last succeeding debug.

- Sebastian

---

<div class="post-metadata">

**Author:** ![tinkuchowdary32](https://avatars.discourse-cdn.com/v4/letter/t/e99b99/32.png) [@tinkuchowdary32](https://forum.ansible.com/u/tinkuchowdary32)\
**Post date:** [March 27, 2019, 6:00am UTC](https://forum.ansible.com/t/need-help-with-how-to-grab-public-ip-and-use-it-for-later-part-in-playbook/29946/5 "2019-03-27T06:00:49Z")

</div>

Hello Sebastian

Thank you for the help i was able to get it working

i have another problem how can i get only the arn value but not complete output saved in to variable

in the below example

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [localhost] =\> {

“template\_arn.results.0.stdout\_lines[1]”: " "assessmentTemplateArn": "arn:aws:inspector:us-west-2:024283424342:target/0-Rrtw16WK/template/0-5KIIPQREcC""

i want to print only arn:aws:inspector:us-west-2:024283424342:target/0-Rrtw16WK/template/0-5KIIPQREcC\ i dont want “”““assessmentTemplateArn””“” to be saved to my variable  
Can anyone please help

Thank you

---

<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:** [March 27, 2019, 11:26am UTC](https://forum.ansible.com/t/need-help-with-how-to-grab-public-ip-and-use-it-for-later-part-in-playbook/29946/6 "2019-03-27T11:26:49Z")

</div>

Please send a new message for that and don't pollute this thread for  
(judging by the error) an unrelated issue.  
Be sure to add enough information though (tasks/playbooks).

thx!

Dick
