Ansible通过inventory文件来配置主机清单,默认文件路径为/etc/ansible/hosts,也可以在自己的管理目录下创建inventory文件。

Inventory参数

参数 说明
ansible_ssh_host 远程的主机名
ansible_ssh_port 远程的端口
ansible_ssh_user SSH远程的用户
ansible_ssh_pass ssh密码
ansible_sudo_pass 建议使用–ask-sudo-pass选项
ansible_sudo_exe sudo路径
ansible_connection 连接方式,如local,ssh或paramiko
ansible_ssh_private_key_file ssh的私钥文件
ansible_shell_type 目标系统的shell类型,默认为sh
ansible_python_interpreter 目标主机的python路径

主机与组

inventory文件的基本格式如下:

10.0.139.163

[appserver]
10.0.139.161
10.0.139.162
10.255.210.15

[dbserver]
10.255.210.10
10.255.210.15

appserver和dbserver是自定义的组,一个主机可以属于多个不同的组,例如10.255.210.15属于appserver同时也可以属于dbserver,我们可以通过组来管理组下面的所有主机。

当主机列表存在一些静态IP地址,希望设置对应的主机别名,而不是配置/etc/hosts,可以通过下列方式定义

app1 ansible_ssh_port=30 ansible_ssh_host=10.0.139.163

主机格式还支持模糊匹配,例如我们可以用下列格式表示app1到app20这20台主机。

app[1:20].example.com

组与组之间还能进行嵌套

[k8s]
10.255.210.9

[db]
10.255.210.10
10.255.210.15

[server:children]
k8s
db

查看server包含的主机

[root@h-luhx-254 ~]# ansible server --list-host -i /data/ansible/Inventory/hosts 
hosts (3):
10.255.210.9
10.255.210.10
10.255.210.15

变量配置

对于主机或组,可以定义一系列变量,提供给playbook调用。例如http端口,文件路径等等

主机和组都可以设定变量

[k8s]
10.255.210.9 docker_path=/data/docker

[db]
10.255.210.10
10.255.210.15

[db:vars]
db_port=3306
version=8.0.23

[server:children]
k8s
db

编写一个简单的playbook作业

[root@h-luhx-254 playbook]# cat /data/ansible//playbook/test.yaml 
- hosts: db
gather_facts: False
tasks:
- name: display variable from inventory file
debug: msg="The {{ inventory_hostname }} Value is {{ db_port }}"

查看变量值

[root@h-luhx-254 playbook]# ansible-playbook test.yaml -i /data/ansible/Inventory -k
SSH password:

PLAY [db] *************************************************************************************************************************************************************************

TASK [display variable from inventory file] ***************************************************************************************************************************************
ok: [10.255.210.10] => {
"msg": "The 10.255.210.10 Value is 3306"
}
ok: [10.255.210.15] => {
"msg": "The 10.255.210.15 Value is 3306"
}

PLAY RECAP ************************************************************************************************************************************************************************
10.255.210.10 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.255.210.15 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

变量还可以保存在单独的文件中,格式为YAML,如下列文件

[root@h-luhx-254 Inventory]# mkdir /data/ansible/Inventory/{group_vars,host_vars}
[root@h-luhx-254 Inventory]# cat /data/ansible/Inventory/group_vars/db
---
db_port: 3306
db_version: 8.0.23

动态Inventory

除了前面基于文本的方式来记录intentory配置信息,Ansible也支持在其它软件系统中保存配置信息。例如EC2、Cobbler、Zabbix等外部系统。

存放主机信息的程序可以使用任何语言开发,其运行结果必须返回Json格式,同时需要支持--list和--host两个选项,--list用于返回所有的主机组信息,每个组包含的host,children、vars,_meta用于存放主机变量;–host则用于指定具体的host。大致格式如下:

{
"docker": {
"hosts": [
"10.255.210.9"
]
},
"mysql": {
"hosts": [
"10.255.210.10",
"10.255.210.15"
],
"vars": {
"mysql_port": 22,
"mysql_version": "8.0.23"
}
}
}

在设计外部系统配置信息时,可以将Inventory配置信息记录在MySQL数据库中,再通过Python脚本获取数据并返回上述格式的JSON结果。