Ansible Patterns

Overview

One of the cool things with Ansible are Patterns. With Patterns, you can combine entries in your inventory file. Instead of having a bunch of tags specific to each site, you can instead have a list of servers in a site and a list of server types and combine it with a Pattern to only run the playbook against the servers that are in both tag groups.

Example Run

If you have an inventory file where you identify all hosts in an environment, such as the Production environment. You might have a tag of longmont.

In addition, if we have different types of servers such as DNS or even Openshift, you might have a tag of openshift that encompasses all openshift servers across all environments. You could even break it down a little further, such as worker.

These tags will have Boulder, Cabo, Tatooine, and Longmont servers all under one tag.

But you don’t want to run this playbook against every openshift worker server. Just against the ones in the staging environment. The staging environment in Tatooine in fact.

ansible-playbook editeth1.yaml -i inventory --ask-pass -e tag='tatooine:&openshift:&worker'

PLAY [tatooine:&openshift:&worker] *********************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************
ok: [tato0cuomwrk01]
ok: [tato0cuomwrk02]
ok: [tato0cuomwrk03]
ok: [tato0cuomwrk04]

How nice is that?

Options

There are other patterns that can be used to manage the inventory plus they can be combined.

  • [tag]:&[tag] – Only servers that are in both lists.
  • [tag]:![tag] – Only servers that are NOT in the second list.
  • [tag]:[tag] – A combined list of all servers that are in both lists.

References

This entry was posted in ansible, Computers and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *