Ansible Inventory使用进阶
1. /etc/ansible/hosts配置
Ansible 从 Inventory 读取列表或组,可同时并发操作这些受控节点或主机。
# ansible默认hosts的配置文件
$ cat /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
# Ex 1: Ungrouped hosts, specify before any group headers.
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
# Ex 2: A collection of hosts belonging to the 'webservers' group
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
# If you have multiple hosts following a pattern you can specify
# them like this:
## www[001:006].example.com
# Ex 3: A collection of database servers in the 'dbservers' group
## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
# Here's another example of host ranges, this time there are no
# leading 0s:
## db-[99:101]-node.example.com
2. Inventory基础
- Inventory 文件可以有多种格式,取决于你使用什么插件,最常用的格式是 YAML 和 INI。
# 括号中的标题是组名,用于对主机进行分类,用于确定什么时间、什么目的、相对哪些主机做什么事情
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
- YAML 格式的示例
all:
hosts:
mail.example.com:
children:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
- 默认组:
- 默认有两个分组:
all
andungrouped
。all
组顾名思义包括所有主机。ungrouped
则是all
组之外所有主机。所有的主机要不属于all
组,要不就属于ungrouped
组。 - 尽管
all
和ungrouped
始终存在,但它们以隐式的方式出现,而不出现在诸如group_names
的组列表中。
- 默认有两个分组:
- 多主机组
- 你可以把一台主机放在多个组中。
all:
hosts:
mail.example.com:
children:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
east:
hosts:
foo.example.com:
one.example.com:
two.example.com:
west:
hosts:
bar.example.com:
three.example.com:
prod:
hosts:
foo.example.com:
one.example.com:
two.example.com:
test:
hosts:
bar.example.com:
three.example.com:
# 可以看到 one.example.com 同时存在 dbservers, east, and prod 组中 您还可以使用嵌套组来简化此清单中的 prod and test 组,优化后结果如下:
all:
hosts:
mail.example.com:
children:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
east:
hosts:
foo.example.com:
one.example.com:
two.example.com:
west:
hosts:
bar.example.com:
three.example.com:
prod:
children:
east:
test:
children:
west:
- 增加主机段
- 如果有许多具有相似模式的主机,则可以将它们添加为一个范围,而不必分别列出每个主机名
# in INI
[webservers]
www[01:50].example.com
# in YAML
...
webservers:
hosts:
www[01:50].example.com:
# 对于数字匹配 [0-9], 也支持字母正则 [a-z]:
[databases]
db-[a:f].example.com
- 在 Inventory 清单中定义的 host 或 group 变量
- 给单台主机设置变量
# 在playbook中的写法
# in INI
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
# in YAML
atlanta:
host1:
http_port: 80
maxRequestsPerChild: 808
host2:
http_port: 303
maxRequestsPerChild: 909
- 定义别名
# in INI
jumper ansible_port=5555 ansible_host=192.0.2.50
# in YAML
...
hosts:
jumper:
ansible_port: 5555
ansible_host: 192.0.2.50
- 多主机设置变量
# in INI
[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
# in YAML
atlanta:
hosts:
host1:
host2:
vars:
ntp_server: ntp.atlanta.example.com
proxy: proxy.atlanta.example.com
# 组变量是将变量同时应用于多个主机,如果该主机是多个组的成员,Ansible将读取所在组的所有变量值。如果在不同组被赋予不同的值,Ansible会根据内部规则来选择需要使用的值
- 继承变量值
# in INI
[atlanta]
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
southwest
northwest
# in YAML
all:
children:
usa:
children:
southeast:
children:
atlanta:
hosts:
host1:
host2:
raleigh:
hosts:
host2:
host3:
vars:
some_server: foo.southeast.example.com
halon_system_timeout: 30
self_destruct_countdown: 60
escape_pods: 2
northeast:
northwest:
southwest:
# 1.子组成员默认属于父组成员
# 2.子组的变量比父组的变量优先级高,即会覆盖父组的变量
# 3.组可以有多个父组或者孩子,但不能循环关系
# 4.主机也可以隶属多个组,但是只有一个主机实例,可以合并多个组中的数据
- 编排主机和组变量
- Ansible通过对应的inventory文件或者playbook文件加载主机和组的变量文件。
/etc/ansible/group_vars/raleigh
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball
# 主机foosball属于raleigh和webserver组
# 在hosts_vars或者group_vars目录下面创建目录,Ansible将顺序读取所有文件
- 变量合并
- 默认情况下,在运行前变量会合并/展开到特定的目标主机,组的定义依赖于inventory和host,ansible会按顺序覆盖重复变量
# 优先级由低到高排序
# 1. all group
# 2. parent group
# 3. child group
# 4. host
# 通过 ansible_group_priority 更改优先级,此变量默认优先级为1
a_group:
testvar: a
ansible_group_priority: 10
b_group:
testvar: b
# 若a_group没有设置优先级,则 testvar == b ,现在 testvar == a
-
使用多个inventory
- 通过 -i inventory -i inventory 指定多个inventory
- 变量合并的顺序是有inventory的输入顺序决定的
-
主机连接
- ansible_connection:连接受控主机的方式,
smart
,ssh
,paramiko
,默认是smart - 常用连接参数:
- ansible_host:连接受控主机名
- ansible_port:连接端口,默认22
- ansible_user:连接远程用户
- ansible_password:认证密码
- ssh连接选项:
- ansible_ssh_private_key_file:指定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 管道,可以在
ansible.cfg
设置 - ansible_ssh_executable(在2.2版本加入):此设置将覆盖默认行为以使用系统 ssh。 这样会覆盖
ansible.cfg
文件中的ssh_executable
设置
- 提权设置
- ansible_become:等同
ansible_sudo
oransible_su
, 允许强制特权升级 - ansible_become_method:允许设置权限提升方法
- ansible_become_user:等同
ansible_sudo_user
oransible_su_user
, 允许设置通过特权升级成为的用户 - ansible_become_password:等同
ansible_sudo_password
oransible_su_password
, 允许您设置特权升级密码。 - ansible_become_exe:等同 to
ansible_sudo_exe
oransible_su_exe
, 允许您为所选的升级方法设置可执行文件 - ansible_become_flags:等同
ansible_sudo_flags
oransible_su_flags
, 允许您设置传递给所选升级方法的标志。 也可以在中ansible.cfg
全局设置sudo_flags
- ansible_become:等同
- 远程主机环境变量选项:
- ansible_shell_type:指定远程主机使用的 Shell。 在使用该选项前一定要先将 ansible_shell_executable 设置为 non-Bourne (sh) 。 默认命令使用
sh
. 设置csh
orfish
将会在远程主机上使用csh
fish
,而非默认的sh
- ansible_python_interpreter:目标主机 python 目录。 对于一台主机上有多个 Python 环境或者默认路径不是 /usr/bin/python 的 *BSD 环境,或者 where /usr/bin/python 的不是 2.X 系统的 Python 情况有用。我们不使用:command:/usr/bin/env 命令机制,因为这需要设置远程用户的路径,并且假定 python 可执行文件名为 python ,其中可执行文件可能命名为像 python2.6 一样的程序。
- ansible_*_interpreter:适用于 ruby or perl 等类型 ansible_python_interpreter 环境。这将替换运行模块在远程主机上的 shabang。
- ansible_shell_executable:设置远程主机使用何种 shell,默认 /bin/sh,会覆盖
executable
inansible.cfg
。 如果远程主机没有安装 /bin/sh ,则需要修改下了。 ( 比如: /bin/sh 在远程主机没有安装或者无法 sudo 运行 )
- ansible_shell_type:指定远程主机使用的 Shell。 在使用该选项前一定要先将 ansible_shell_executable 设置为 non-Bourne (sh) 。 默认命令使用
- ansible_connection:连接受控主机的方式,
-
Non-SSH连接类型(通过设置
ansible_connection=<connector>
选项来修改连接类型。)- Non-SSH选项
- local:部署应用到管理机自身。
- docker:该连接器使用本地Docker客户端将 Playbook 直接部署到 Docker 容器中。
- ansible_host:要连接的 Docker 容器名字
- ansible_user:在容器内操作的用户名,用户必须存在于容器内
- ansible_become 如果设置为
true
,则become_user
将用于在容器内进行操作。 ansible_docker_extra_args可以是一个字符串,该字符串由 Docker 可识别的命令选项成,不限定命令任何其他参数。这个选项主要用于配置远程主机 Docker daemon 服务。
- Non-SSH选项
- name: create jenkins container
docker_container:
docker_host: myserver.net:4243
name: my_jenkins
image: jenkins
- name: add container to inventory
add_host:
name: my_jenkins
ansible_connection: docker
ansible_docker_extra_args: "--tlsverify --tlscacert=/path/to/ca.pem --tlscert=/path/to/client-cert.pem --tlskey=/path/to/client-key.pem -H=tcp://myserver.net:4243"
ansible_user: jenkins
changed_when: false
- name: create directory for ssh keys
delegate_to: my_jenkins
file:
path: "/var/jenkins_home/.ssh/jupiter"
state: directory