Another option I've used is to modify the ec2.py that comes in the ansible sample inventories. For something simple like this it's not a big deal, but when you start having more custom groups based on tags and you don't want the ec2_tag_ prefix on every group (or are mixing EC2 and non-aws instances), it starts to become more of a pain. It's a pretty simple python script that's easy to modify. Once we made that small leap our playbooks became a lot clearer imo.
On Tue, Dec 16, 2014 at 1:45 PM, Quentin Stafford-Fraser <[email protected]> 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/b39f45aa-b6b8-4836-ab65-82b49c48ea95%40googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- 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/CAJQqANddEJZz7MrRKukOt8SULSF3q%2BAh-4NChuDSW5Xx9ngYFw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
