How to use add_hosts with ec2 inventory?

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

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?

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