您的位置:首页 > 其它

实例学习Ansible系列:配置文件ansible.cfg的设定与使用

2019-07-11 05:09 686 查看

知识点:Ansible中通过多层次的ansible.cfg的设定,利用不同目录结构下的配置文件的作用域,还可以结合ANSIBLE_CONFIG环境变量进行自行指定,较为方便和灵活。

环境准备

[root@host131 ~]# ansible --version
ansible 2.4.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
[root@host131 ~]#

注:从这篇文章开始Ansible的版本为2.4.2.0,一般来说版本影响不大。

配置文件:ansible.cfg

配置文件 路径 作用域说明 优先度
系统级配置文件 /etc/ansible/ansible.cfg 为系统级别的设定文件,对所有用户起效 最低
用户级配置文件 ~/ansible.cfg 为用户级设定文件,对当前用户起效 高于系统级配置文件
当前运行应用级配置文件 ./ansible.cfg 当前运行的playbook等所指定的配置文件,对当前执行操作起效 高于用户级配置文件
ANSIBLE_CONFIG环境变量指定配置文件 export ANSIBLE_CONFIG=配置文件路径 当前运行的playbook等所指定的配置文件,对当前执行操作起效 高于当前路径下的ansible.cfg配置文件

实验1:验证系统级配置文件

  • 事前准备

前提条件1:确保当前目录(如果当前目录不在当前用户的HOME目录下的情况下)和当前用户的HOME目录下没有ansible.cfg配置文件

前提条件2: ANSIBLE_CONFIG没有设定值

前提条件3: /etc/ansible/hosts中有如下设定

...省略
10 [all]
11 host131
12 host132
...省略
  • 结果确认
[root@host131 ~]# ansible all --list-hosts
hosts (2):
host131
host132
[root@host131 ~]#

结果说明:
在前面的设定前提下,此时起作用的是系统级别的ansible.cfg配置文件,此文件关联的hosts文件为/etc/ansible/hosts,由于在前提条件中将[all]中包含的机器设定为host131和host132,所以使用ansible all --list-hosts出现的结果则是/etc/ansible/ansible.cfg缺省关联的/etc/ansible/hosts中的设定内容起效。

实验2: 验证用户级配置文件

前提条件1: /etc/ansible/hosts中有如下设定

...省略
10 [all]
11 host131
12 host132
...省略

前提条件2:确保当前目录(如果当前目录不在当前用户的HOME目录下的情况下)和当前用户的HOME目录下没有ansible.cfg配置文件

前提条件3: ANSIBLE_CONFIG没有设定值

前提条件4: 当前用户的配置文件设定信息如下所示:

[root@host131 ~]# cat ~/ansible.cfg
[defaults]
inventory      = ~/user_hosts
[root@host131 ~]#
[root@host131 ~]# cat ~/user_hosts
[all]
host133
host134
[root@host131 ~]#
  • 结果确认
[root@host131 ~]# ansible all --list-hosts
hosts (2):
host133
host134
[root@host131 ~]#

结果说明:
在前面的设定前提下,此时起作用的是用户级别的ansible.cfg配置文件,此文件关联的hosts文件为~/user_hosts,由于在前提条件中将[all]中包含的机器设定为host133和host134,所以使用ansible all --list-hosts出现的结果则是/ansible.cfg缺省关联的/user_hosts中的设定内容起效。需要注意的是此时系统级配置文件也有设定,但是起效的是用户级的配置文件。

实验3: 验证当前运行应用级配置文件

前提条件1: /etc/ansible/hosts中有如下设定

...省略
10 [all]
11 host131
12 host132
...省略

前提条件2: 当前用户的配置文件设定信息如下所示:

[root@host131 ~]# cat ~/ansible.cfg
[defaults]
inventory      = ~/user_hosts
[root@host131 ~]#
[root@host131 ~]# cat ~/user_hosts
[all]
host133
host134
[root@host131 ~]#

前提条件3: ANSIBLE_CONFIG没有设定值

前提条件4:确保当前目录(当前目录不在当前用户的HOME目录下)的配置文件信息如下

[root@host131 ~]# cd ~/test
[root@host131 test]# ls
ansible.cfg  hosts
[root@host131 test]# cat ansible.cfg
[defaults]
inventory      = ~/test/hosts
[root@host131 test]# cat hosts
[all]
host141
host142
host143
host144
[root@host131 test]#
  • 结果确认
[root@host131 test]# ansible all --list-hosts
hosts (4):
host141
host142
host143
host144
[root@host131 test]#

结果说明:
在前面的设定前提下,此时起作用的是当前目录下的ansible.cfg配置文件,此文件关联的hosts文件为当前目录下的hosts文件,由于在前提条件中将[all]中包含的机器设定为host141到host144四台机器,所以使用ansible all --list-hosts出现的结果则是当前目录下的hosts文件中的设定内容起效。需要注意的是此时系统级配置文件和用户级别也有设定,但是起效的是当前目录下的配置文件ansible.cfg。

实验4: 验证ANSIBLE_CONFIG环境变量指定配置文件

前提条件1: /etc/ansible/hosts中有如下设定

...省略
10 [all]
11 host131
12 host132
...省略

前提条件2: 当前用户的配置文件设定信息如下所示:

[root@host131 ~]# cat ~/ansible.cfg
[defaults]
inventory      = ~/user_hosts
[root@host131 ~]#
[root@host131 ~]# cat ~/user_hosts
[all]
host133
host134
[root@host131 ~]#

前提条件3:确保当前目录(当前目录不在当前用户的HOME目录下)的配置文件信息如下

[root@host131 ~]# cd ~/test
[root@host131 test]# ls
ansible.cfg  hosts
[root@host131 test]# cat ansible.cfg
[defaults]
inventory      = ~/test/hosts
[root@host131 test]# cat hosts
[all]
host141
host142
host143
host144
[root@host131 test]#

前提条件4: ANSIBLE_CONFIG设定值以及设定值所指向的配置文件内容如下

[root@host131 test]# ls
ansible.cfg  env  hosts
[root@host131 test]# cat ~/test/env/ansible.cfg
[defaults]
inventory      = ~/test/env/hosts
[root@host131 test]# cat ~/test/env/hosts
[all]
host151
host152
host153
host154
[root@host131 test]#
[root@host131 test]# export ANSIBLE_CONFIG=~/test/env/ansible.cfg
  • 结果确认
[root@host131 test]# ansible all --list-hosts
hosts (4):
host151
host152
host153
host154
[root@host131 test]#

结果说明:
在前面的设定前提下,此时起作用的是ANSIBLE_CONFIG环境变量中所指定的配置文件,此文件关联的hosts文件为其同一层相对目录下的hosts文件,由于在前提条件中将[all]中包含的机器设定为host151到host154四台机器,所以使用ansible all --list-hosts出现的结果也是此hosts文件中的设定内容起效。需要注意的是此时系统级配置文件和用户级别以及当前目录下的ansible.cfg也有设定,但是起效的是ANSIBLE_CONFIG中指定的配置文件。

其他内容

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐