前言 Ansible 配置
主要包括:
主机清单文件”inventory”,extra module放置路径”library” ,远程主机的临时文件位置” remote_tmp” ,管理节点上临时文件的位置”local_tmp”
inventory = /etc/ansible/hosts
library = /usr/share/my_modules/
remote_tmp = $HOME/.ansible/tmp
local_tmp = $HOME/.ansible/tmp
优先级
ansible的默认配置文件是/etc/ansible/ansible.cfg。其实ansible会按照下面的顺序查找配置文件,并使用第一个发现的配置文件。
- ANSIBLE_CONFIG (an environment variable)
- ansible.cfg (in the current directory)
- .ansible.cfg (in the home directory)
- /etc/ansible/ansible.cfg
1 Host Inventory (主机清单)
主机清单,告诉ansible需要管理哪些server,和server的分类和分组信息。可以根据你自己的需要根据地域分类,也可以按照功能的不同分类。
位置 :/etc/ansible/hosts
若想修改修改主机目录
vim /etc/ansible/ansible.cfg
...
inventory = /etc/ansible/hosts
...
1.1 分组
- 简单分组
简单的分组[]内是组名
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
1.2 连接参数和变量
- 参数
指定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
- 变量
[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 目录的配置.