前言
在学习后面的playbook剧本之前,需要先了解一些变量参数的一些基础知识,从而更好的学习后面的playbook
一、主机清单配置文件
- ansible默认的主机清单是/etc/ansible/hosts文件
- 主机清单可以手动设置,也可以通过Dynamic Inventory动态生成
- 一般主机名使用FQDN(完全合格域名,如:www.baidu.com;域名为:baidu.com)
ansible主机清单中的配置文件解析
vi /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
# Ex 1: Ungrouped hosts, specify before any group headers.
## green.example.com
## blue.example.com
## 192.168.1.1
## 192.168.1.10
# Ex 2: A collection of hosts belonging to the 'webservers' group
## [webservers] #方括号设置组名
## alpha.example.org #定义被监控的主机,可以是主机名也可以是IP地址,主机名需要修改/etc/hosts文件
## beta.example.org
## 192.168.1.100
## 192.168.1.110
##www2.example.org:2222 #冒号后定义远程连接端口,默认是ssh的22端口
如果是名称类似的主机,可以使用列表的方式标识各个主机,这边定义了ssh远程节点的用户与密码,可以直接实现免密登录
[webserver]
www[01:50].example.org ansible_ssh_user=root ansible_ssh_pass=123456
[dbbservers]
db-[a:f].example.org 1/支持匹配a b c ... f
# If you have multiple hosts following a pattern you can specify
# them like this:
## www[001:006].example.com #表示有6个节点
例如:
www001.example.com
www002.example.com
www003.example.com
www004.example.com
www005.example.com
www006.example.com
这样的形式会产生数据冗余:重复的内容过多,不利于加载与读取数据的时候过慢
# Ex 3: A collection of database servers in the 'dbservers' group
## [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:
##
二、Inventory(库存)中变量
2.1 主机变量
[webserver]
#节点1中定义的http端口为80,最大连接数为808
www1.magedu.com http_port=80 maxRequestsChild=808
#节点2中定义的http端口为8080,最大连接数为909
www2.magedu.com http_port=8080 maxRequestsChild=909
2.2 组变量
[servers:vars]
ntp_server=ntp.example.orgnfs_server=nfs.example.org
2.3 组嵌套
[apache]
http1.example.orghttp2.example.org
[nginx]
ngx1.example.org
ngx2.example.org
[webserver:children] #调用前面的主机名,就相当于调用其中的节点
apache
nginx
2.4 inventory变量参数
inventory变量参数(系统中自带的一些变量,这些参数名称是不能修改的)