官术网_书友最值得收藏!

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:

  1. 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
  1. 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
  1. Use ! to exclude a specific groupyou 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
  1. 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
  1. 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.

主站蜘蛛池模板: 大兴区| 新密市| 安西县| 和静县| 雷山县| 商水县| 伊宁市| 郑州市| 平南县| 安平县| 阳原县| 普兰店市| 登封市| 克什克腾旗| 木里| 隆安县| 民县| 三江| 贵溪市| 乐东| 瑞丽市| 商洛市| 康乐县| 萨迦县| 桂平市| 平顶山市| 西充县| 团风县| 岚皋县| 长寿区| 宁陵县| 漳浦县| 新乐市| 西吉县| 西昌市| 响水县| 贵州省| 永和县| 丰都县| 达日县| 青岛市|