# Pinging another machine using AWS/ec2 dynamic inventory

**URL:** <https://forum.ansible.com/t/pinging-another-machine-using-aws-ec2-dynamic-inventory/17023>\
**Category:** Ansible Project\
**Tags:** ubuntu, aws\
**Created:** [December 11, 2014, 10:33am UTC](https://forum.ansible.com/t/pinging-another-machine-using-aws-ec2-dynamic-inventory/17023 "2014-12-11T10:33:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sven\_Ehlert](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@Sven\_Ehlert](https://forum.ansible.com/u/Sven_Ehlert)\
**Post date:** [December 11, 2014, 10:33am UTC](https://forum.ansible.com/t/pinging-another-machine-using-aws-ec2-dynamic-inventory/17023/1 "2014-12-11T10:33:30Z")

</div>

Hi all,

I have a rather simple usecase for which I don’t seem to find an answer.

How can I ping a AWS/ec2 instance from another instance in a different play? I’m using the dynamic inventory.

The scenario is like this:

I create two ec2 instance in a play like this:

`  
ec2:  
group: “{{ security\_group }}”  
instance\_type: “{{ instance\_type }}”  
image: “{{ image }}”  
region: “{{ region }}”  
key\_name: “{{ keypair }}”  
instance\_tags:  
Name: Destination

ec2:  
group: “{{ security\_group }}”  
instance\_type: “{{ instance\_type }}”  
image: “{{ image }}”  
region: “{{ region }}”  
key\_name: “{{ keypair }}”  
instance\_tags:  
Name: Source  
`

This works well, the nodes are started and I can see them when running ./ec2.py

Then, in another play (ping.yml) I want to do this

`

- name: pinging host  
hosts: tag\_Name\_Source  
remote\_user: ubuntu

tasks:

- shell: ping {{ tag\_Name\_Destination }}

`

I run it with  
`  
ansible-playbook -i ec2.py ping.yml

`

but it gives me “One or more undefined variables: ‘tag\_Name\_Destination’ is undefined”

What is the right way to get the IP of the target machine?

I’m running Ansible 1.8.2 on Ubuntu.

Thanks for any hints.  
/Sven

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [December 11, 2014, 3:32pm UTC](https://forum.ansible.com/t/pinging-another-machine-using-aws-ec2-dynamic-inventory/17023/2 "2014-12-11T15:32:02Z")

</div>

Hi Sven, this is most likely due to the caching of data by the inventory script. You can set “cache\_max\_age = 0” in your ec2.ini to disable caching, to see if that helps.

---

<div class="post-metadata">

**Author:** ![Sven\_Ehlert](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@Sven\_Ehlert](https://forum.ansible.com/u/Sven_Ehlert)\
**Post date:** [December 18, 2014, 1:44pm UTC](https://forum.ansible.com/t/pinging-another-machine-using-aws-ec2-dynamic-inventory/17023/3 "2014-12-18T13:44:11Z")

</div>

Hi James,

thanks for the reply, however that didn’t work out.  
I get the same error "“One or more undefined variables: ‘tag\_Name\_Destination’ is undefined”

I can even simplify my playbook to “ping myself”

`

- name: pinging myself  
hosts: tag\_Name\_Source

tasks:

- shell: ping {{ tag\_Name\_Source }}

`

and I still get  
`  
PLAY [pinging myself] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [xx.xx.xx.xx]

TASK: [shell ping {{ tag\_Name\_Source }}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [xx.xx.xx.xx] =\> One or more undefined variables: ‘tag\_Name\_Source’ is undefined

`

That is, while Ansible understands ‘tag\_Name\_Source’ in the ‘hosts:’ section, it doesn’t understand the _same_ variable in the “shell:” section.  
Do I need to use a special syntax when using variables with {{ }}?

---

<div class="post-metadata">

**Author:** ![Sven\_Ehlert](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@Sven\_Ehlert](https://forum.ansible.com/u/Sven_Ehlert)\
**Post date:** [February 11, 2015, 11:37am UTC](https://forum.ansible.com/t/pinging-another-machine-using-aws-ec2-dynamic-inventory/17023/4 "2015-02-11T11:37:00Z")

</div>

I found a solution myself. The following playbook works as expected. This should be added to the ec2 tutorial.

`- name: pinging myself`  
`hosts: tag_Name_Source`

`tasks:`  
`- shell: ping {{ groups['tag_Name_Source'][0] }}`
