Ansible Inventory: Difference between revisions

From The Power of Many
No edit summary
No edit summary
Line 17: Line 17:
</syntaxhighlight>
</syntaxhighlight>


* 指定主机 mail.example.com
<syntaxhighlight lang="bash">
mail.example.com


* 指定主机组 web, db
[web]
web[1:5].example.com


* 主机组嵌套  prod
[db]
db[1:3].example.com


<br />
[prod:children]
web
db
</syntaxhighlight>
 
*指定主机 mail.example.com
 
*指定主机组 web, db
 
*主机组嵌套  prod
 
'''未被分组的主机默认在ungrouped组内'''
 
=== 选择主机和组 ===
举例主机清单<syntaxhighlight lang="bat">
srv1.example.com
srv2.example.com
s1.lab.example.com
s2.lab.example.com
 
[web]
jupiter.lab.example.com
saturn.example.com
 
[db]
db1.example.com
db2.example.com
db3.example.com
#db[1:3].example.com
 
[lb]
lb1.lab.example.com
lb2.lab.example.com
 
[boston]
db1.example.com
jupiter.lab.example.com
lb2.lab.example.com
 
[london]
db2.example.com
db3.example.com
file1.lab.example.com
lb1.lab.example.com
 
[dev]
web1.lab.example.com
db3.example.com
 
[stage]
file2.example.com
db2.example.com
 
[prod]
lb2.lab.example.com
db1.example.com
jupiter.lab.example.com
 
[function:children]
web
db
lb
city
 
[city:children]
boston
london
environments
 
[environments:children]
dev
stage
prod
new
 
[new]
172.25.252.23
172.25.252.44
</syntaxhighlight>
 
==== 匹配所有主机 ====
<code>ansible all --list-hosts</code>
 
==== 匹配指定的主机或主机组 ====
 
===== 匹配单个主机 =====
<code>ansible db1.example.com --list-hosts</code>
 
===== 匹配多个主机 =====
<code>ansible 'db1.example.com,s1.lab.example.com' --list-hosts</code>
 
===== 匹配单个组 =====
<code>ansible prod --list-hosts</code>
 
===== 匹配多个组 =====
<code>ansbile 'london,boston' --list-hosts</code>
 
===== 匹配不属于任何组的主机 =====
<code>ansible ungrouped --list-hosts</code>
 
==== 通配符匹配 ====
匹配 *.example.com
 
<code>ansible '*.example.com' --list-hosts</code>
 
==== 通配符组合匹配 ====
 
==== 正则表达式匹配 ====
 
==== 通过 --limit 明确指定主机或组 ====

Revision as of 03:37, 13 April 2020

批量管理主机的时候, 需要预先定义主机或者组, 这个文件叫 Inventory, 默认位于 /etc/ansible/hosts.

/etc/ansible/hosts 文件的格式如下:

mail.example.com

[web]
web[1:5].example.com

[db]
db[1:3].example.com

[prod:children]
web
db
mail.example.com

[web]
web[1:5].example.com

[db]
db[1:3].example.com

[prod:children]
web
db
  • 指定主机 mail.example.com
  • 指定主机组 web, db
  • 主机组嵌套 prod

未被分组的主机默认在ungrouped组内

选择主机和组

举例主机清单

srv1.example.com
srv2.example.com
s1.lab.example.com
s2.lab.example.com

[web]
jupiter.lab.example.com
saturn.example.com

[db]
db1.example.com
db2.example.com
db3.example.com
#db[1:3].example.com

[lb]
lb1.lab.example.com
lb2.lab.example.com

[boston]
db1.example.com
jupiter.lab.example.com
lb2.lab.example.com

[london]
db2.example.com
db3.example.com
file1.lab.example.com
lb1.lab.example.com

[dev]
web1.lab.example.com
db3.example.com

[stage]
file2.example.com
db2.example.com

[prod]
lb2.lab.example.com
db1.example.com
jupiter.lab.example.com

[function:children]
web
db
lb
city

[city:children]
boston
london
environments

[environments:children]
dev
stage
prod
new

[new]
172.25.252.23
172.25.252.44

匹配所有主机

ansible all --list-hosts

匹配指定的主机或主机组

匹配单个主机

ansible db1.example.com --list-hosts

匹配多个主机

ansible 'db1.example.com,s1.lab.example.com' --list-hosts

匹配单个组

ansible prod --list-hosts

匹配多个组

ansbile 'london,boston' --list-hosts

匹配不属于任何组的主机

ansible ungrouped --list-hosts

通配符匹配

匹配 *.example.com

ansible '*.example.com' --list-hosts

通配符组合匹配

正则表达式匹配

通过 --limit 明确指定主机或组