# Cannot use add\_host when using ec2.py?

**URL:** <https://forum.ansible.com/t/cannot-use-add-host-when-using-ec2-py/14435>\
**Category:** Ansible Project\
**Created:** [January 27, 2014, 7:05pm UTC](https://forum.ansible.com/t/cannot-use-add-host-when-using-ec2-py/14435 "2014-01-27T19:05:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [January 27, 2014, 7:05pm UTC](https://forum.ansible.com/t/cannot-use-add-host-when-using-ec2-py/14435/1 "2014-01-27T19:05:19Z")

</div>

Is it normal for add\_host to not work when using ec2.py? I’m in a playbook that is using ec2.py as the inventory source and I’m trying to create a few groups dynamically using ec2 tag values.

When I run debug on the hostvars afterwards, the new group name isn’t there.

**Inventory output:**

> jenkins@ip-172-31-35-124:~/jobs/JobName/workspace$ bin/devtools/ansible/ec2.py --list --refresh-cache  
> …
> 
> “tag\_ec2id\_test10”: [  
> “172.31.31.72”,  
> “172.31.45.13”  
> “tag\_ec2type\_mysql”: [  
> “172.31.25.13”  
> ],  
> “tag\_ec2type\_apache”: [  
> “172.31.31.72”
> 
> …

**Start of the play**

> - name: Gather ec2 facts  
> ec2\_facts:  
> when: provider == “ec2”
> 
> - name: Add instance to iaas-provider-agnostic type group  
> add\_host: hostname={{ inventory\_hostname }} groups=“{{ ec2\_tag\_ec2type }}”  
> when: provider == “ec2”

**A call to debug on one of these hosts, inserted after the above calls:**

> ```auto
> "ec2_tag_ec2id": "test10", 
> "ec2_tag_ec2id_and_ec2type": "test10_apache2", 
> "ec2_tag_ec2type": "apache2", 
> 
> ```
> 
> ```auto
> 
> ...
> 
> "group_names": [
> " ***************************", 
> " ***************************", 
> "i- ********", 
> "key_ *******", 
> "security_group_ ***************************", 
> "security_group_ ***************************", 
> "security_group_ ***************************", 
> " ***************************", 
> " ***************************", 
> "tag_ec2id_and_ec2type_test10_apache2", 
> "tag_ec2id_test10", 
> "tag_ec2type_apache2", 
> "tag_provisioned_True", 
> "type_m1_medium", 
> "us-west-2", 
> "us-west-2b"
> ], 
> 
> ```

TIA for any insights…hopefully I’ve just got an obvious typo.  
-Mark

---

<div class="post-metadata">

**Author:** ![tannerjc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tannerjc/32/985_2.png) [@tannerjc](https://forum.ansible.com/u/tannerjc)\
**Post date:** [January 27, 2014, 7:25pm UTC](https://forum.ansible.com/t/cannot-use-add-host-when-using-ec2-py/14435/2 "2014-01-27T19:25:20Z")

</div>

I can’t see any reason in the code for this to not work. Could you show us the -vvvv output from ansible-playbook? Maybe something isn’t evaulated the way you expect.

---

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [January 27, 2014, 9:23pm UTC](https://forum.ansible.com/t/cannot-use-add-host-when-using-ec2-py/14435/3 "2014-01-27T21:23:11Z")

</div>

Yea, seems like it should work. I opened a github issue with a simpler localhost test-case.

[https://github.com/ansible/ansible/issues/5791](https://github.com/ansible/ansible/issues/5791)

A lot of these are obviously redacted :), but I’ve triple-checked and there is no ‘apache2’ or ‘mysql’ group there.

---

<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:** [January 27, 2014, 9:24pm UTC](https://forum.ansible.com/t/cannot-use-add-host-when-using-ec2-py/14435/4 "2014-01-27T21:24:28Z")

</div>

FWIW, add\_host and ec2.py are unrelated.

There’s probably just a basic understanding issue here, and I don’t think we’re quite into “file a github ticket” area yet.

---

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [January 27, 2014, 9:25pm UTC](https://forum.ansible.com/t/cannot-use-add-host-when-using-ec2-py/14435/5 "2014-01-27T21:25:37Z")

</div>

Bah! Obfuscation gone wrong…I never know how much is prudent to remove. 🙂

In my last message, the group webnode==apache2 and db==mysql…I missed some of them when replacing.

Sorry!

---

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [January 27, 2014, 11:06pm UTC](https://forum.ansible.com/t/cannot-use-add-host-when-using-ec2-py/14435/6 "2014-01-27T23:06:58Z")

</div>

Ok, saw the note on the github ticket (thanks, and sorry if filing it was premature). Not sure whether to continue here or there; here I guess…

So for my real use case of this on ec2, I posted more debug output…should that be working? More or less I’m attempting something like this from ansible-examples: [https://github.com/ansible/ansible-examples/blob/master/openshift/roles/ec2/tasks/main.yml](https://github.com/ansible/ansible-examples/blob/master/openshift/roles/ec2/tasks/main.yml), except these instances already exist so I’m using ec2.py to pull in the tags to use for the provider-agnostic group names, which will then be used when calling deployment roles. This playbook I’m trying to do this grouping in is sort of an intermediary…I have existing plays for these roles and I didn’t want to go changing _ **groups: db** _ to _ **groups: tag\_ec2type\_db** _ for roles that otherwise have nothing to do with ec2.

Thank you,  
Mark

---

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [February 3, 2014, 10:07pm UTC](https://forum.ansible.com/t/cannot-use-add-host-when-using-ec2-py/14435/7 "2014-02-03T22:07:45Z")

</div>

I swapped the add\_host tasks for some group\_by’s and its working.

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![Quentin\_Stafford-Fra](https://avatars.discourse-cdn.com/v4/letter/q/b2d939/32.png) [@Quentin\_Stafford-Fra](https://forum.ansible.com/u/Quentin_Stafford-Fra)\
**Post date:** [December 17, 2014, 2:17pm UTC](https://forum.ansible.com/t/cannot-use-add-host-when-using-ec2-py/14435/8 "2014-12-17T14:17:16Z")

</div>

This may also be useful for anyone having this problem:

[https://groups.google.com/forum/?utm\_medium=email&utm\_source=footer#!msg/ansible-project/cpnrBRxLy0E/W-nqNgYkR2sJ](https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/ansible-project/cpnrBRxLy0E/W-nqNgYkR2sJ)
