二、主机目录(Host Inventory)

一、引子

什么叫主机目录管理,告诉ansible需要管理那些server,和server的分类和分组信息。可以根据你自己的需要根据地域分类,也可以按照功能的不同分类。

二、主机目录的配置文件

# 默认文件
/etc/ansible/hosts


# 修改主机目录的配置文件

...
inventory      = /etc/ansible/hosts
...




# 命令行中传递主机目录配置文件
$ ansible-playbook -i hosts site.yml
或者参数—inventory-file
$ ansible-playbook --inventory-file hosts site.yml

三、远程主机的分组

简单的分组[]内是组名

mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
[webservers]
www[01:50].example.com
[databases]
db-[a:f].example.com

分组usa的子组还可以是其它的组,例如[usa:children]中还可以子组southeast, [southeast:children]中还可以包含atlanta和releigh

[atlanta]
host1
host2
[raleigh]
host2
host3
[southeast:children]
atlanta
raleigh
[usa:children]
southeast
northeast
southwest
northwest

四、远程主机的连接参数和变量

参数

指定Server的连接参数,其他包括连接方法,用户等。

[targets]
localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_user=mpdehaan
other2.example.com     ansible_connection=ssh        ansible_user=mdehaan
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909

所有可以指定的参数在文档中

http://docs.ansible.com/ansible/intro_inventory.html#list-of-behavioral-inventory-parameters

变量

为一个组指定变量

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

五、按目录结构存储变量

假设inventory文件为/etc/ansible/hosts,那么相关的hosts和group变量可以放在下面的目录结构下

/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball

/etc/ansible/group_vars/raleigh 文件内容可以为

---
ntp_server: acme.example.org
database_server: storage.example.org

如果对应的名字为目录名,ansible会读取这个目录下面所有文件的内容

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

group_vars/ 和 host_vars/ 目录可放在 inventory 目录下,或是 playbook 目录下. 如果两个目录下都存在,那么 playbook 目录下的配置会覆盖 inventory 目录的配置.

posted @ 2018-07-22 20:11  云原生运维社区  阅读(612)  评论(0编辑  收藏  举报