# error with ec2 inventory variable

**URL:** <https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716>\
**Category:** Ansible Project\
**Created:** [April 24, 2013, 4:02pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716 "2013-04-24T16:02:52Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Filip\_Slunecko](https://avatars.discourse-cdn.com/v4/letter/f/58956e/32.png) [@Filip\_Slunecko](https://forum.ansible.com/u/Filip_Slunecko)\
**Post date:** [April 24, 2013, 4:02pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/1 "2013-04-24T16:02:52Z")

</div>

Hi,

I am getting this error (part of a playbook included):

[http://pastie.org/7709606](http://pastie.org/7709606)

Work flow:  
I will create nodes on EC2 and install them.  
Everything goes very until this line:

when: “‘master’ in ec2\_tag\_Name and hadoop\_exists.rc == 1”

ec2\_tag\_Name doesn’t seem to exist.

But when I run the same playbook and use the same machines (installed in the previous partial unsuccessful run) it works as expected.

ec2\_tag\_Name exists and playbook finishes just fine.

In both cases an output of a command:

---

<div class="post-metadata">

**Author:** ![Lester\_Wade](https://avatars.discourse-cdn.com/v4/letter/l/97f17d/32.png) [@Lester\_Wade](https://forum.ansible.com/u/Lester_Wade)\
**Post date:** [April 24, 2013, 6:19pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/2 "2013-04-24T18:19:58Z")

</div>

I might be misinterpreting what you are trying to do but I don’t see where ec2\_tag\_Name is defined as a variable. It _looks_ like you want to reference the group name and key off that somehow, tag\_Name\_ANS-Test You’d do this by referencing: ${groups.tag\_Name\_ANS-Test}.

---

<div class="post-metadata">

**Author:** ![Filip\_Slunecko](https://avatars.discourse-cdn.com/v4/letter/f/58956e/32.png) [@Filip\_Slunecko](https://forum.ansible.com/u/Filip_Slunecko)\
**Post date:** [April 25, 2013, 5:00pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/3 "2013-04-25T17:00:13Z")

</div>

I tried it still no luck.

- action: debug msg=“EC2 tag ${group.ec2\_tag\_Name} ${hadoop\_dir\_src}”

TASK: [debug msg=“EC2 tag ${group.ec2\_tag\_Name} …/files/hadoop/cdh4”] \*\*\*\*\*\*\*\*  
ok: =\> {“item”: “”, “msg”: “EC2 tag ${group.ec2\_tag\_Name} …/files/hadoop/cdh4”}  
ok: =\> {“item”: “”, “msg”: “EC2 tag ${group.ec2\_tag\_Name} …/files/hadoop/cdh4”}

=\> Conditional expression must evaluate to True or False: {% if ‘master’ in ${group.sec2\_tag\_Name} %} True {% else %} False {% endif %}

Strange is that this:  
{% if ‘master’ in sec2\_tag\_Name %}

works.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 25, 2013, 8:57pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/4 "2013-04-25T20:57:07Z")

</div>

You have ec2 in one and sec2 in the other.

Suspect it is that 🙂

---

<div class="post-metadata">

**Author:** ![Filip\_Slunecko](https://avatars.discourse-cdn.com/v4/letter/f/58956e/32.png) [@Filip\_Slunecko](https://forum.ansible.com/u/Filip_Slunecko)\
**Post date:** [April 26, 2013, 9:39am UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/5 "2013-04-26T09:39:47Z")

</div>

Those types of bugs are embarrassing :).

A summary first:

ec2\_tag\_Name works in post run - create instances in one playbook run and install them in second.  
group.ec2\_tag\_Name doesn’t exist

ec2\_tag\_Name is not evaluated in debug msg, but it’s in debug output.

when: “‘master’ in ${group.ec2\_tag\_Name}” is failing, but I am pretyy sure it woked yesterday =\> I should not be working with dev branch 🙂

Anyway the issues is still there (I am using devel branch):

Run with group.ec2\_tag\_Name:

- name: format namenode  
command: su hdfs -c “/usr/bin/hadoop namenode -format -nonInteractive” create=${hadoop\_dir\_nn}/current  
when: “‘master’ in ${group.ec2\_tag\_Name}”

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 26, 2013, 12:17pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/6 "2013-04-26T12:17:30Z")

</div>

It is hard to help over email at a decent rate here 🙂

I would not use the $foo variables in templates, even in 1.X, but obviously the point of 1.2 is to allow the same variable logic we could always use in templates to be used in mainline playbooks.  
There should be no reason to not run 1.2 at this time – it’s quite stable.

So anyway, I would in any version rewrite this:

{% if ‘master’ in ${group.ec2\_tag\_Name} %}

As:

{% if ‘master’ in group.ec2\_tag\_Name % }

This makes it a valid Jinja2 statement and doesn’t rely on the $foo replacement stuff.

Obviously if you want to debug available variables, you can just do this, and I would recommend it:

{% groups | to\_nice\_json %}  
{% hostvars | to\_nice\_json %}

---

<div class="post-metadata">

**Author:** ![Filip\_Slunecko](https://avatars.discourse-cdn.com/v4/letter/f/58956e/32.png) [@Filip\_Slunecko](https://forum.ansible.com/u/Filip_Slunecko)\
**Post date:** [April 26, 2013, 6:04pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/7 "2013-04-26T18:04:00Z")

</div>

IRC chat have not solved anything 🙂 but just to let you now:

this works:  
when: “‘master’ in ec2\_tag\_Name”

this doesn’t:  
when: “‘master’ in ${ec2\_tag\_Name}”

Real problem is still this error:  
[http://pastie.org/7725328](http://pastie.org/7725328)

Filip

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 26, 2013, 6:21pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/8 "2013-04-26T18:21:29Z")

</div>

ec2 tag name is a string not a list of groups.

And using 1.2, all that outer quoting is unnecessary.

---

<div class="post-metadata">

**Author:** ![Filip\_Slunecko](https://avatars.discourse-cdn.com/v4/letter/f/58956e/32.png) [@Filip\_Slunecko](https://forum.ansible.com/u/Filip_Slunecko)\
**Post date:** [April 26, 2013, 6:58pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/9 "2013-04-26T18:58:33Z")

</div>

> ec2 tag name is a string not a list of groups.

Yes, “in” works for list, strings …  
I just thought that:  
${ec2\_tag\_Name} ← save way how to get variable.

In this case ${ec2\_tag\_Name} doesn’t work.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [April 26, 2013, 7:14pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/10 "2013-04-26T19:14:49Z")

</div>

BTW, what you have is not a variable. Not supposed to work. Happy to discuss on IRC.

groups[‘ec2\_tag\_Name’] is the list of matching hosts in the group.

---

<div class="post-metadata">

**Author:** ![Filip\_Slunecko](https://avatars.discourse-cdn.com/v4/letter/f/58956e/32.png) [@Filip\_Slunecko](https://forum.ansible.com/u/Filip_Slunecko)\
**Post date:** [May 3, 2013, 4:22pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/11 "2013-05-03T16:22:35Z")

</div>

Finally I found enough time to look into it - I spent few hours with pdb :).

ec2\_tag\_Name is a variable from ec2 script - you get those from here:

inject.update(self.playbook.inventory.get\_variables(host))

The problem was that I use this to register ec2 variable:

- name: add it to the special group  
local\_action: add\_host hostname=${inst\_res.instances[0].publis\_ip} groupname=${target\_group}

ec2.py does not return inventory host vars by public\_ip (it returns {})

I have changed publis\_ip to public\_dns\_name and it works.

The rest of the ansible was able to work with public ip just fine.

Filip

---

<div class="post-metadata">

**Author:** ![Peter\_Sankauskas](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@Peter\_Sankauskas](https://forum.ansible.com/u/Peter_Sankauskas)\
**Post date:** [May 3, 2013, 4:46pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/12 "2013-05-03T16:46:20Z")

</div>

Hi Filip,

You can run the EC2 inventory file and list out all variables for a host by doing this:

$ /etc/ansible/hosts --host [ec2-50-112-79-155.us-west-2.compute.amazonaws.com](http://ec2-50-112-79-155.us-west-2.compute.amazonaws.com)  
{  
“ec2\_\_in\_monitoring\_element”: false,  
“ec2\_\_previous\_state”: “”,  
“ec2\_ami\_launch\_index”: “0”,  
“ec2\_architecture”: “x86\_64”,  
“ec2\_client\_token”: “d79a7c98-10f7-4b3e-be3d-426098331041”,  
“ec2\_dns\_name”: “[ec2-50-112-79-155.us-west-2.compute.amazonaws.com](http://ec2-50-112-79-155.us-west-2.compute.amazonaws.com)”,  
“ec2\_ebs\_optimized”: false,  
“ec2\_eventsSet”: “”,  
“ec2\_group\_name”: “”,  
“ec2\_hypervisor”: “xen”,  
“ec2\_id”: “i-b2c51888”,  
“ec2\_image\_id”: “ami-ca1582fa”,  
“ec2\_instance\_profile”: “”,  
“ec2\_instance\_type”: “m1.xlarge”,  
“ec2\_ip\_address”: “50.112.79.155”,  
“ec2\_item”: “”,  
“ec2\_kernel”: “aki-ace26f9c”,  
“ec2\_key\_name”: “secretkey”,  
“ec2\_launch\_time”: “2013-05-03T00:01:39.000Z”,  
“ec2\_monitored”: false,  
“ec2\_monitoring”: “”,  
“ec2\_persistent”: false,  
“ec2\_platform”: “”,  
“ec2\_private\_dns\_name”: “ip-10-253-43-92.us-west-2.compute.internal”,  
“ec2\_private\_ip\_address”: “10.253.43.92”,  
“ec2\_public\_dns\_name”: “[ec2-50-112-79-155.us-west-2.compute.amazonaws.com](http://ec2-50-112-79-155.us-west-2.compute.amazonaws.com)”,  
“ec2\_ramdisk”: “”,  
“ec2\_reason”: “”,  
“ec2\_region”: “us-west-2”,  
“ec2\_requester\_id”: “”,  
“ec2\_root\_device\_name”: “”,  
“ec2\_root\_device\_type”: “instance-store”,  
“ec2\_security\_group\_ids”: “sg-5b4e0b6b”,  
“ec2\_security\_group\_names”: “ElasticMapReduce-slave”,  
“ec2\_spot\_instance\_request\_id”: “”,  
“ec2\_state\_reason”: “”,  
“ec2\_subnet\_id”: “”,  
“ec2\_tag\_Name”: “EMR Cluster”,  
“ec2\_tag\_aws\_elasticmapreduce\_instance-group-role”: “CORE”,  
“ec2\_tag\_aws\_elasticmapreduce\_job-flow-id”: “j-122MBJ5QTIUJ1”,  
“ec2\_virtualization\_type”: “paravirtual”,  
“ec2\_vpc\_id”: “”  
}

So in this case, ec2\_ip\_address is the variable for the public IP

Hope that helps.

Peter Sankauskas

---

<div class="post-metadata">

**Author:** ![Filip\_Slunecko](https://avatars.discourse-cdn.com/v4/letter/f/58956e/32.png) [@Filip\_Slunecko](https://forum.ansible.com/u/Filip_Slunecko)\
**Post date:** [May 3, 2013, 5:13pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/13 "2013-05-03T17:13:27Z")

</div>

Hi Peter,

Yes I know. And there is a list of variables in ec2 script.

My problem was that I was using public IP to register a server.

Example of the playbook:

-hostanme localhost

- create EC2 host

- register: add\_host hostname=${inst\_res.instances[  
0].publis\_ip}

-hostanme just\_created\_hostname

- try to use ec2 variable

FAIL

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 3, 2013, 7:10pm UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/14 "2013-05-03T19:10:10Z")

</div>

This is not a real playbook, please paste the actual one please.

---

<div class="post-metadata">

**Author:** ![Filip\_Slunecko](https://avatars.discourse-cdn.com/v4/letter/f/58956e/32.png) [@Filip\_Slunecko](https://forum.ansible.com/u/Filip_Slunecko)\
**Post date:** [May 4, 2013, 6:23am UTC](https://forum.ansible.com/t/error-with-ec2-inventory-variable/12716/15 "2013-05-04T06:23:24Z")

</div>

ansible/bin/ansible-playbook playbooks/test.yml -vvv --private-key key.pem -i ansible/inventory/ -e “ec2\_instance\_name=ANSTEST1 target\_group=tag\_Name\_ANSTEST1”

- hosts: localhost  
connection: local  
user: root  
gather\_facts: False

vars\_files:

- external\_vars.yml
- ec2\_vars.yml

tasks:

- name: spin it up  
local\_action:  
module: ec2  
keypair: ${ec2\_keypair}  
image: ami-xxxxxx  
instance\_type: m1.small  
group: ${ec2\_security\_group}  
ec2\_access\_key: ${ec2\_access\_key}  
ec2\_secret\_key: ${ec2\_secret\_key}  
instance\_tags: “{"Name":"${ec2\_instance\_name}-master"}”  
wait: yes  
count: 1  
register: inst\_res

- name: add it to a group  
local\_action: add\_host hostname=${inst\_res.instances[0].public\_dns\_name} groupname=${target\_group}

- name: wait for the host  
local\_action: wait\_for host=${inst\_res.instances[0].public\_dns\_name} port=22 delay=5 timeout=300

- hosts: ${target\_group}\*

user: root  
gather\_facts: yes  
vars:

- hadoop\_version: cdh4

tasks:

- name: test hadoop\_version {{hadoop\_version}}  
shell: echo “${hadoop\_version}${ec2\_tag\_Name}” \> /tmp/test  
when: “‘master’ in ec2\_tag\_Name”
