目录
ansible配置文件
/etc/ansible
ansible.cfg
hosts
ansible.cfg:ansible执行需求的全局性,默认的主配置文件
hosts:默认的主机资产清单文件
查看配置文件
[ ~]# ansible --version ansible 2.4.1.0 config file = None 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-2.4.1.0-py2.7.egg/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
设置主配置文件
未生效
[4ucbfj060qys19Z ~]# export ANSIBLE_CONFIG=/root/test.cfg [ ~]# ansible --version ansible 2.4.1.0 config file = None 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-2.4.1.0-py2.7.egg/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
创建文件
mkdir /etc/ansible vim /etc/ansible/ansible.cfg [ usr]# cat /etc/ansible/ansible.cfg [defaults] inventory = /tmp/hosts [ssh_connection] ssh_args = -o StrictHostKeyChecking=no cat /tmp/hosts [test] 172.31.187.202:22 ansible_ssh_user=root ansible_ssh_pass='123456Aa'
查看配置文件,为什么就变更过来了
[ usr]# ansible --version ansible 2.4.1.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-2.4.1.0-py2.7.egg/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
ansible读取配置文件的优先级
ANSIBLE_CONFIG ./ansible.cfg ~/ansible.cfg /etc/ansible/ansible.cfg
ansible 读取hosts文件的优先级与上相同
#注释/etc/ansible/ansible.cfg中的hosts文件配置 [ usr]# cat /etc/ansible/ansible.cfg [defaults] #inventory = /tmp/hosts [ssh_connection] ssh_args = -o StrictHostKeyChecking=no #显示结果如下 [ ~]# ansible test --list-host [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: Could not match supplied host pattern, ignoring: all [WARNING]: provided hosts list is empty, only localhost is available [WARNING]: Could not match supplied host pattern, ignoring: test [WARNING]: No hosts matched, nothing to do hosts (0):
复制并再次检测
[ ~]# cp /tmp/hosts /etc/ansible/
#出现主机信息
[ ~]# ansible test --list-hosts
hosts (1):
172.31.187.202