ansible 之 inventory文件

    ansible 对于自动化运维非常方便。在这里就记录一点自己觉得好用的地方。

1 ansible 的inventory 文件分组,组变量

    /etc/ansible/hosts

# 分组
[single]
172.28.64.104
172.28.64.105
172.28.64.133
172.28.64.137

# 组变量
[single:vars]
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_pass=borui2020

2 inventory 文件子分组

[single]
172.28.64.104
172.28.64.105
172.28.64.133
172.28.64.137

[single:vars]
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_pass=borui2020

[ceph]
172.18.0.131
172.18.0.132
172.18.0.133

# 主组包含下面两个子组
[test:children]
ceph
single

3 inventory 文件 组参数yml文件

    由于组比较多,每个组的参数也比较多,如果都放在/etc/ansible/hosts 文件中 就不好管理。对组的参数可以分开放在不同yml文件中管理

/etc/ansible/group_vars/group-name.yml

    例如:

#yml 格式
[root@chuangxindasai-prometheus group_vars]# cat ceph.yml 
---
ansible_ssh_port: 22
ansible_ssh_user: root
ansible_ssh_pass: ccssoft
[root@chuangxindasai-prometheus group_vars]# ls -l
total 4
-rw-r----- 1 root root 74 Jul 28 16:11 ceph.yml

由上例子可以知道:test的children组single  的变量在hosts 文件,test的children组ceph 的变量在./group_vars/ceph.yml 中(yml文件名一定要和组名一致)

变量可以放在hosts 文件中,也可以放在组列表中。但是如果同时存放则组列表中的会覆盖hosts中的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章