ansible学习笔记(二)Iventory、Variables
ansible同时和多种系统工作,它会选择inventory文件中的部分主机执行不同的操作,这个文件默认位于/etc/ansible/hosts。你也可以同时使用多个inventory或者动态地从云端pull。
host and group
inventory文件的模式类似ini:1
2
3
4
5
6
7
8
9
10mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
方括号中是组名,同个主机可以位于多个组中,此时变量将来自所有其所属的组。如果ssh主机使用非默认的22端口,可以使用:将其指定于主机名之后badwolf.example.com:5309。
如果是静态IP想取个别名,可使用以下的方式。1
jumper ansible_port=5555 ansible_host=192.168.1.50
jumper并不需要是一个真正的Hostname,以上方式通过inventory文件定义了一个特殊变量。
如果有很多主机,也不需要穷举1
2[webservers]
www[01:50].example.com
上下界是包括的,字母也可以1
2[databases]
db-[a:f].example.com
还可以为每个主机选择连接类型和用户1
2
3
4[targets]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh ansible_user=mpdehaan
other2.example.com ansible_connection=ssh ansible_user=mdehaan
在inventory文件中设置host_var是一个临时方案,我们以后会介绍如何在将它们存储在host_var目录下的单独文件中。
host variables
上面已经提到,可以设置主机变量以便在脚本在使用。1
2
3[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
比如要为不同的主机分别设置hostname,就可以将各自的hostname写在主机之后
group variables
也可以为group设置变量,方式与host变量稍有不同1
2
3
4
5
6
7[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
组的组
利用:children后缀,可以将多个组再次组合为一个新组。1
host1
host2
[raleigh]
host2
host3
[southeast:children]
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
如果需要存储列表或hash数据,或单纯只是喜欢存储在单独文件在, 可以见下节。
单独存储host/group数据
ansible不推荐将变量与Host/Group的inventroy文件混在一起,而是存储在相关的单独文件中,这些文件可以以yml,yaml,json或无后缀名(参见yaml语法资料)。
假设inventroy目录是/etc/ansible/hosts,如果主机foosball属于组webserver和raleign,那么以下位置的yaml文件将与主机关联。1
2
3/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball
当然以上文件也可以并不存在,这些都可选的。作为一个高级选项,你也可以创建与host/group同名的目录,其中的文件都会被读取。1
2/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings
单独文件可以让你的inventory非常巨大时更有组织,另外也可以使部分主机加入 ansible vault。
另外group_vars / host_vars目录也可以位于playbook所在或其inventory子目录,如果二者都存在时,playbook目录会覆盖子目录。
另外推荐将inventory和变量文件用git管理,以方便跟踪版本变化。
行为变量
可以设定以下变量来改变ansible与主机互动方式。
ansible_connection
常用值为 local, smart, ssh or paramiko, 缺省为smart
SSH相关变量
ansible_host
连接的主机名
ansible_port
ssh端口,如果不是22
ansible_user
指定用户名
ansible_ssh_pass
密码。强烈不推荐,请使用-k选项输入密码
ansible_ssh_private_key_file
指定私钥,当使用多个key连接机群并不想使用ssh代理时有用
ansible_ssh_common_args
设定 sftp, scp, ssh参数 可为某主机或组配置ProxyCommand.
ansible_sftp_extra_args
指定sftp参数
ansible_scp_extra_args
指定scp参数
ansible_ssh_extra_args
指定ssh参数
ansible_ssh_pipelining
是否使用ssh pipeline, 级别优于ansible.cfg
远程主机环境变量
ansible_shell_type
指定shell类型,默认为sh。除非设定了ansibleshell_executable否则不要使用。
ansible_python_interpreter
python路径。当主机有多个python版本时可用,机制不同于#!/usr/bin/evn,不需要路径设定,可执行文件也不一定要名为python。
**ansible_interpreter*
ruby or perl 版的 ansible_python_interpreter.
Ansible2.1新增
ansible_shell_executable
改变shell类型,除非没有安装/bin/sh否则最好不要修改它
一个段hostfile文件示例:1
2
3
4some_host ansible_port=2222 ansible_user=manager
aws_host ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
freebsd_host ansible_python_interpreter=/usr/local/bin/python
ruby_module_host ansible_ruby_interpreter=/usr/bin/ruby.1.9.3
