Monday, July 23, 2018

Ansible Patterns


Introduction
------------

- specifies on which hosts in the inventory ansible will talk to
- used on the ff:
    a. ad-hoc commands
    b. plays
- examples patterns in ad-hoc commands:
    ansible webservers -m yum -a "name=httpd state=present"
    ansible -i ~/my_hosts db1:db2 -m ping
- some patterns like "!" and "&" needs to be escaped ("\") in shell

List of Patterns that can be used
---------------------------------

all hosts
all
*
hosts w/o a group
ungrouped
specific hosts or
set of hosts
one.example.com
one.example.com:two.example.com
192.0.2.50
192.0.2.*
*.example.com
*.com
one or more groups
All hosts under webservers group
webservers

Colon indicates "or" which means the host may be in one group or the other. Host will
still be managed if it is on both groups.
webservers:dbservers

In version 1.9, you can use "," instead of ":". "," is preferrable on ranges dealing
with IPv6.

As of version 2.0, ";" is deprecated as a host separator.
excluding a group
All hosts must be in webservers but not in phoenix. Any host that is a member of
webservers but also a member of phoenix will not be managed.
webservers:!phoenix
intersection
Ampersand means "and". Hosts must be in webservers and must also be in staging.
webservers:&staging
combination
All machines in webservers and dbservers must be in staging also to be managed.
Machines will not be managed if they are in phoenix.
webservers:dbservers:&staging:!phoenix
passing variables
webservers:!{{excluded}}:&{{required}}
mixing wildcards and
groups
one*.com:dbservers
selecting by position
In this example group,
[webservers]
cobweb
webbing
weber

you can refer to hosts by specifying a subscript:
webservers[0]       # == cobweb
webservers[-1]      # == weber
webservers[0:1]     # == webservers[0],webservers[1]
                    # == cobweb,webbing
webservers[1:]      # == webbing,weber
using regular
expressions
~(web|db).*\.example\.com
limiting
This will talk to hosts within datacenter2 group only. This can be used in "ansible" or
"ansible-playbook" commands
ansible-playbook site.yml --limit datacenter2
ansible all --limit datacenter2 -m ping
getting from a file
You can read list of hosts from a filename with "@" (available on version 1.2 up).
This is commonly used to retry a playbook on failed hosts.
ansible-playbook site.yml --limit @retry_hosts.txt
   

No comments:

Post a Comment