首先我們看下ansible的配置文件(有刪減):
[root@www ~]# cat /etc/ansible/hosts
Ex 1: Ungrouped hosts, specify before any group headers.
下面是沒有分組的主機,可以寫域名、ip、別名。
www .fuchao.com #域名
alias01 ansible_ssh_port=22 ansible_ssh_host =192.168.1.144 #別名 alias01
192.168.1.144 #ip
192.168.1.145
Ex 2: A collection of hosts belonging to the 'webservers' group
一組主機屬于“網路”組
[webservers] #組名 ,下面的成員可以用域名、ip、別名。
www.fuchao.com #組員域名
192.168.1.144 #組員ip
192.168.1.145
If you have multiple hosts following a pattern you can specify
如果你有多個主機后,您可以指定一個模式
them like this:#他們是這樣的
www[001:006].example.com #支持正則,假如你有一萬臺主機 ,而且很有規則。
www[1:10000].example.com #假如你有一萬臺主機 ,而且很有規則
Ex 3: A collection of database servers in the 'dbservers' group
一個集合的數據庫服務器“dbservers”組
[dbservers] #組名
db01.intranet.mydomain.net
db02.intranet.mydomain.net
10.25.1.56
10.25.1.57
Here's another example of host ranges, this time there are no
leading 0s:
db-[99:101]-node.example.com
[root@www ~]#
定義主機變量:
為了方便讓playbooks配置使用。
[Hvariables]
host1 http_port=80
host2 http_port=8080
定義組變量:
組變量作用于組的所有成員
[Gvariable]
host1
host2
[Gvariable:vars]
ftp_server = ftp.fuchao.com
web_server = www.fuchao.com
目標匹配:
ansible <pattern_goes_here> -m <module_name> -a <argumrnts>
匹配目標地址或主機地址: 用冒號(:)分隔開。
匹配組 : 用冒號(:)分隔開。
匹配所有主機 : ALL 或者星號(*)
支持正則表達方式匹配主機或者ip
匹配 一個組除某個ip的所有成員 :組名:!ip
匹配兩個組織間的交集:組1:&組2