- Practical Ansible 2
- Daniel Oh James Freeman Fabio Alessandro Locati
- 628字
- 2021-06-24 16:06:52
Special host management using patterns
We have already established that you will often want to run either an ad hoc command or a playbook against only a subsection of your inventory. So far, we have been quite precise in doing that, but let's now expand upon this by looking at how Ansible can work with patterns to figure out which hosts a command (or playbook) should be run against.
As a starting point, let's consider again an inventory that we defined earlier in this chapter for the purposes of exploring host groups and child groups. For your convenience, the inventory contents are provided again here:
loadbalancer.example.com
[frontends]
frt01.example.com
frt02.example.com
[apps]
app01.example.com
app02.example.com
[databases]
dbms01.example.com
dbms02.example.com
[centos:children]
apps
databases
[ubuntu:children]
frontends
To demonstrate host/group selection by pattern, we shall use the --list-hosts switch with the ansible command to see which hosts Ansible would operate on. You are welcome to expand the example to use the ping module, but we'll use --list-hosts here in the interests of space and keeping the output concise and readable:
- We have already mentioned the special all group to specify all hosts in the inventory:
$ ansible -i hostgroups-children-ini all --list-hosts
hosts (7):
loadbalancer.example.com
frt01.example.com
frt02.example.com
app01.example.com
app02.example.com
dbms01.example.com
dbms02.example.com
The asterisk character has the same effect as all, but needs to be quoted in single quotes for the shell to interpret the command properly:
$ ansible -i hostgroups-children-ini '*' --list-hosts
hosts (7):
loadbalancer.example.com
frt01.example.com
frt02.example.com
app01.example.com
app02.example.com
dbms01.example.com
dbms02.example.com
- Use : to specify a logical OR, meaning "apply to hosts either in this group or that group," as in this example:
$ ansible -i hostgroups-children-ini frontends:apps --list-hosts
hosts (4):
frt01.example.com
frt02.example.com
app01.example.com
app02.example.com
- Use ! to exclude a specific group—you can combine this with other characters such as : to show (for example) all hosts except those in the apps group. Again, ! is a special character in the shell and so you must quote your pattern string in single quotes for it to work, as in this example:
$ ansible -i hostgroups-children-ini 'all:!apps' --list-hosts
hosts (5):
loadbalancer.example.com
frt01.example.com
frt02.example.com
dbms01.example.com
dbms02.example.com
- Use :& to specify a logical AND between two groups, for example, if we want all hosts that are in the centos group and the apps group (again, you must use single quotes in the shell):
$ ansible -i hostgroups-children-ini 'centos:&apps' --list-hosts
hosts (2):
app01.example.com
app02.example.com
- Use * wildcards in a similar manner to what you would use in the shell, as in this example:
$ ansible -i hostgroups-children-ini 'db*.example.com' --list-hosts
hosts (2):
dbms02.example.com
dbms01.example.com
Another way you can limit which hosts a command is run on is to use the --limit switch with Ansible. This uses exactly the same syntax and pattern notation as in the preceding but has the advantage that you can use it with the ansible-playbook command, where specifying a host pattern on the command line is only supported for the ansible command itself. Hence, for example, you could run the following:
$ ansible-playbook -i hostgroups-children-ini site.yml --limit frontends:apps
PLAY [A simple playbook for demonstrating inventory patterns] ******************
TASK [Gathering Facts] *********************************************************
ok: [frt02.example.com]
ok: [app01.example.com]
ok: [frt01.example.com]
ok: [app02.example.com]
TASK [Ping each host] **********************************************************
ok: [app01.example.com]
ok: [app02.example.com]
ok: [frt02.example.com]
ok: [frt01.example.com]
PLAY RECAP *********************************************************************
app01.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
app02.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
frt01.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
frt02.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Patterns are a very useful and important part of working with inventories, and something you will no doubt find invaluable going forward. That concludes our chapter on Ansible inventories; however, it is hoped that this has given you everything you need to work confidently with Ansible inventories.
- 完全掌握AutoCAD 2008中文版:綜合篇
- 中國戰(zhàn)略性新興產(chǎn)業(yè)研究與發(fā)展·智能制造
- Java Web整合開發(fā)全程指南
- 數(shù)據(jù)庫系統(tǒng)原理及應(yīng)用教程(第5版)
- Learning C for Arduino
- SAP Business Intelligence Quick Start Guide
- 網(wǎng)絡(luò)服務(wù)搭建、配置與管理大全(Linux版)
- Salesforce Advanced Administrator Certification Guide
- 空間機(jī)器人
- 基于Proteus的單片機(jī)應(yīng)用技術(shù)
- 貫通開源Web圖形與報(bào)表技術(shù)全集
- 從零開始學(xué)ASP.NET
- 精通ROS機(jī)器人編程(原書第2版)
- MySQL Management and Administration with Navicat
- Raspberry Pi 3 Projects for Java Programmers