文章目录
后续此博客不再更新,欢迎大家搜索关注微信公众号“测开之美”,测试开发工程师技术修炼小站,持续学习持续进步。
机器管理
ansible
和ansible-playbook
命令的-i
参数可以指定机器配置文件(inventory file)路径。可以同时使用多个机器配置文件、动态加载机器配置文件、从云端获取机器配置文件。
机器与分组
机器配置文件支持多种格式,默认的/etc/ansible/hosts
是INI格式(通过更换inventory插件可以是YAML格式):
a.example.com
[webservers]
b.example.com
c.example.com
[dbservers]
d.example.com
e.example.com
INI格式中,等号后面的内容总是被识别成字符串,不会自动转义。方括号的组名将机器分组。上面的INI文件对应的YAML格式为:
all:
hosts:
a.example.com
children:
webservers:
hosts:
b.example.com
c.example.com
dbservers:
hosts:
d.example.com
e.example.com
一个机器可以属于多个分组。如果机器使用非标准SSH端口,在机器名称后面用冒号隔开明确指定,例如a.example.com:5309
。
为机器设置别名:
jumper ansible_port=5309 ansible_host=a.example.com
对应YAML格式:
hosts:
jumper:
ansible_port: 5309
ansible_host: a.example.com
如果机器的名称比较相似,对于数字类型可以使用如下格式:
[webservers]
www[01:50].example.com #左右包含,包含前导零
字母格式:
[databases]
db-[a:f].example.com
每个机器可以单独指定一些配置项:
[example]
localhost ansible_connection=local
a.example.com ansible_connection=ssh ansible_user=testa
b.example.com ansible_connection=ssh ansible_user=testb
机器变量
对每个机器定义变量,可以用在后面的playbook中:
[example]
host1 http_port=80 maxRequestPerC