- 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.
- Big Data Analytics with Hadoop 3
- 構建高質量的C#代碼
- Hands-On Machine Learning on Google Cloud Platform
- Visual FoxPro 6.0數據庫與程序設計
- Hands-On Cloud Solutions with Azure
- MCSA Windows Server 2016 Certification Guide:Exam 70-741
- Visual C# 2008開發技術詳解
- Java Web整合開發全程指南
- 大學C/C++語言程序設計基礎
- 悟透AutoCAD 2009完全自學手冊
- Linux Shell編程從初學到精通
- TensorFlow Deep Learning Projects
- 無人駕駛感知智能
- 手把手教你學Flash CS3
- 樂高創意機器人教程(中級 上冊 10~16歲) (青少年iCAN+創新創意實踐指導叢書)