Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering multiple tags in Ansible dynamic inventory

Tags:

ansible

I think I've seen an answer to this somewhere, but I can't seem to find it now. I'm creating a dynamic development inventory file for my EC2 instances. I'd like to group all instances tagged with Stack=Development. Moreover, I'd like to specifically identify the development API servers. Those would not only have the Stack=Development tag, but also the API=Yes tag.

My basic setup uses inventory folders:

<root>/development
  ├── base
  ├── ec2.ini
  └── ec2.py

In my base file, I'd like to have something like this:

[servers]
tag_Stack_Development

[apiservers]
tag_Stack_Development && tag_API_Yes

Then I'd be able to run this to ping all of my development api servers:

ansible -i development -u myuser apiservers -m ping

Can something like that be done? I know the syntax isn't right, but hopefully the intent is reasonably clear? I can't imagine I'm the only one who's ever needed to filter on multiple tags, but I haven't been able to find anything that gets me where I'm trying to go.

like image 780
Rob Wilkerson Avatar asked Apr 20 '16 15:04

Rob Wilkerson


People also ask

How do you manage dynamic inventory in Ansible?

To tie your Ansible inventory to Cobbler, copy this script to /etc/ansible and chmod +x the file. Run cobblerd any time you use Ansible and use the -i command line option (for example, -i /etc/ansible/cobbler.py ) to communicate with Cobbler using Cobbler's XMLRPC API.

How do I use multiple inventory files in Ansible?

TL;DR: Inventory can be a folder. Create a folder, add as many inventory files inside this folder and instruct Ansible to use this folder as the inventory (with -i folder_name or in your ansible. cfg file). All inventory files inside the folder will be merged into one (including scripts like ec2.py).

What is difference between static and dynamic inventory in Ansible?

In summary, a static inventory file is a plain text file containing a list of managed hosts or remote nodes whose numbers and IP addresses remain fairly constant. On the other hand, a dynamic host file keeps changing as you add new hosts or decommission old ones.

In which cases Ansible may use multiple inventory sources?

If the location given to -i in Ansible is a directory (or as so configured in ansible. cfg), Ansible can use multiple inventory sources at the same time. When doing so, it is possible to mix both dynamic and statically managed inventory sources in the same ansible run.


3 Answers

The answer provided by xiong-chiamiov does actually work. I've just been using it in my ansible deployment.

So I have a playbook using the dynamic inventory script. with this piece of code:

    ---
    - name: AWS Deploy
      hosts: tag_Environment_dev:&tag_Project_integration
      gather_facts: true

And the process does filter the hosts by both of those tags.

EDIT

Actually expanding on this, you can also use variables to make the host group specification dynamic. like this:

    ---
    - name: AWS Deploy
      hosts: "tag_Environment_{{env}}:&tag_Project_{{tag_project}}"
      sudo: true
      gather_facts: true

I use the {{env}} and {{tag_project}} vars from variable files and arguments given to ansible at runtime. It successfully changes the hosts the playbook runs against.

like image 193
James Morgan Avatar answered Dec 03 '22 02:12

James Morgan


It's not the answer I had in my head, but sometimes what's in my head just gets in the way. Since each inventory directory has its own ec2.ini, I just filter the stack there and then group within that filter.

# <root>/development/ec2.ini
...
instance_filters = tag:Stack=Development

# <root>/development/base
[tag_Role_webserver]
[tag_API_Yes]

[webservers:children]
tag_Role_webserver

[apiservers:children]
tag_API_Yes
like image 30
Rob Wilkerson Avatar answered Dec 03 '22 04:12

Rob Wilkerson


The Ansible documentation has a section on patterns. Rather than creating a new section, you can do a tag intersection when you specify the hosts:

[$] ansible -i development -u myuser tag_Stack_Development:&tag_API_Yes

This also works within playbooks.

like image 37
Xiong Chiamiov Avatar answered Dec 03 '22 03:12

Xiong Chiamiov