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
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.
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 {{ }}?
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] }}