# How to use add\_hosts with ec2 inventory?

**URL:** <https://forum.ansible.com/t/how-to-use-add-hosts-with-ec2-inventory/13232>\
**Category:** Ansible Project\
**Created:** [July 31, 2013, 8:24pm UTC](https://forum.ansible.com/t/how-to-use-add-hosts-with-ec2-inventory/13232 "2013-07-31T20:24:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark1](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@Mark1](https://forum.ansible.com/u/Mark1)\
**Post date:** [July 31, 2013, 8:24pm UTC](https://forum.ansible.com/t/how-to-use-add-hosts-with-ec2-inventory/13232/1 "2013-07-31T20:24:43Z")

</div>

Hello!

I’m calling a playbook with:

ansible-playbook provision.yml -i ./ec2.py --extra-vars “role=webserver env=dev01 hosts=type\_t1\_micro”

And in my playbook I want to add the hosts in the type\_t1\_micro group to the ‘webservers’ group, e.g.:

tasks:

- name: Adding new instance to host group  
local\_action: add\_host hostname=${item} groupname=webservers  
with\_items: $hosts

This doesn’t work because it tries to use ‘type\_t1\_micro’ as a host name - it doesn’t add the actual hosts in the group.

How can I do this?

Thanks

---

<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:** [July 31, 2013, 8:37pm UTC](https://forum.ansible.com/t/how-to-use-add-hosts-with-ec2-inventory/13232/2 "2013-07-31T20:37:19Z")

</div>

public service announcement.

So in Ansible 1.2 and later there is no need to do “$hosts”

with\_items: hosts

you should also do hostname={{ item }}.

I’m not sure where you set “hosts” though, perhaps you can show the rest of the playbook?

---

<div class="post-metadata">

**Author:** ![Mark1](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@Mark1](https://forum.ansible.com/u/Mark1)\
**Post date:** [July 31, 2013, 9:01pm UTC](https://forum.ansible.com/t/how-to-use-add-hosts-with-ec2-inventory/13232/3 "2013-07-31T21:01:20Z")

</div>

I’ve been having issues with the ‘{{ var }}’ format and yaml compatibility so I’ve had to mix-and-match ${ var } syntax. Perhaps I just always need to use curlies quoted like “{{ var }}”?

I might be misunderstanding with setting $hosts, so here’s my complete playbook (launch-ec2.yml):

# Launches an EC2 instance and provisions it as the specified role
