ansible-3 主机清单hosts的设置

主机清单的设置参考:http://www.ansible.com.cn/docs/intro_inventory.html

[ceshi]
192.168.220.98
log ansible_ssh_host=192.168.220.116 ansible_ssh_port=10056 ansible_ssh_user=wwwad

名词解释:

[ceshi]  这是主机组名

log:192.168.220.116的别名

ansible_ssh_port=10056:ansible远程控制的端口

ansible_ssh_user=wwwad:远程控制的用户名

注:如果这里把ansible_ssh_host=192.168.220.116给去掉,只写log的话是解析不出来的,需要在/etc/hosts文件里做解析,所以还不如直接在这里写上ansible_ssh_host=192.168.220.116

也可以这么写

log:10056 ansible_ssh_host=192.168.220.116 ansible_ssh_user=wwwad 

可以为一个主机组或者单个主机设置不同的变量,之后再playbook中使用调用变量

下面说一下主机变量

这是单个主机所定义的变量

[atlanta]
host1   ntp_server=ntp.atlanta.example.com
host2   proxy=proxy.atlanta.example.com

这是整个主机组所定义的变量
[atlanta]
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

把一个组作为另一个组的子成员

可以把一个组作为另一个组的子成员,以及分配变量给整个组使用. 这些变量可以给 /usr/bin/ansible-playbook 使用,但不能给 /usr/bin/ansible 使用:

[atlanta]  主机组一
host1
host2

[raleigh]  主机组二
host2
host3

[southeast:children] 主机组一、二下的主机都是主机组southeast孩子(子组)
atlanta
raleigh

[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2

[usa:children]
southeast
northeast
southwest
northwest

分文件定义 Host 和 Group 变量

比如说我们在

/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings

这里在group_vars目录下创建主机组raleigh目录,这个主机组目录下有两个文件  db_settings 和  cluster_settings

这两个文件里定义的变量是唯raleigh这个组使用的,两个文件中设置不同的变量

注意:分文件定义变量的方式只适用于Ansible1.4及其以上的版本     Ansible 1.2 及以上的版本中,group_vars/ 和 host_vars/ 目录可放在 inventory 目录下,或是 playbook 目录下. 如果两个目录下都存在,那么 playbook 目录下的配置会覆盖 inventory 目录的配置.

原文地址:https://www.cnblogs.com/smail-bao/p/5569612.html