Quentin, really useful post.
However I had to add two things in order to skip the errors on hosts that 
were unreachable. I think this might be helpful in case they are not 
started and you would like to start them later from the ec2 module.
Tried to suppress the stuff using configuration variables

host_key_checking=True
display_skipped_hosts=True

but that still getting the 
PLAY 
***************************************************************************
messages, which is really annoying. 
It is going to be a good idea to include this playbook in the beginning so 
other scripts can reuse the dynamic group(s).


- hosts: ec2
  serial: 1
*  gather_facts: false*
*  ignore_errors: yes*
  
  tasks:
    - name: Add host(s) from EC2 tag group to Controller group
      local_action: add_host
        name={{inventory_hostname}}
        groupname='Put_your_new_dynamic_group_name_here'
      #when: item.startswith('tag_Name_') and item.endswith(
*your_name_variable*) and (inventory_hostname in groups[item]) #optional 
loose matching
      when: item == 'tag_Name_' + *your_name_variable* and 
(inventory_hostname in groups[item])
      with_items: group_names

On Tuesday, December 16, 2014 at 10:45:10 AM UTC-8, Quentin Stafford-Fraser 
wrote:
>
> I spent a long time trying to get this to work, and eventually managed it, 
> so just in case it's useful for anyone else...
>
> I wanted to tag my EC2 instances with, say 'webserver: True' and have them 
> then appear in the 'webserver' host group locally, but I wanted to do it as 
> a general case, without having to write special code for every group.  
>
> The ec2.py inventory script will put each host into a group called 'ec2' 
> and also, for example, into 'tag_webserver_True'.  
>
> So this is what I did next:
>
> ---
> - hosts: ec2
>   remote_user: ubuntu
>   serial: 1
>   tasks:
>     - name: Add hosts in EC2 tag groups to local groups
>       local_action: add_host
>         name={{inventory_hostname}}
>         groupname={{item[4:-5]}}
>       when: item.startswith('tag_') and item.endswith('_True') and 
> (inventory_hostname in groups[item])
>       with_items: group_names
>
>
> It took me a couple of hours playing with all sorts of variations on this 
> theme to realise that the *really important bit is to include 'serial: 
> 1'.  *
>
> Adding things to groups using add_host *does not work reliably* if there 
> are multiple hosts being processed simultaneously.  You will probably find 
> that a single host gets added to the group but no more.
>
> This, however, seems to work fine, and can easily be included in playbooks 
> where it will be ignored if you're not using any EC2 hosts.
>
> Hope that's useful for someone!
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/c4c49595-c7d0-4fcc-b5a0-6f72276ff897%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to