ansible的一些重要配置文件
ansible的一些重要配置文件
一、/etc/ansible有以下三个文件或者目录生成(默认配置文件)
1、Hosts
主机清单配置文件
2、ansible.cfg
Ansible配置文件
3、Roles
角色定义目录
[root@ansible ~]# ssh root@master
root@master's password:
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Tue Aug 26 23:14:28 2025 from 192.168.122.1
[root@master ~]# cd /etc/ansible/
[root@master ansible]# ls
ansible.cfg hosts roles
二、ansible配置文件优先级
ANSIBLE_CONFIG :⾸先,Ansible命令会检查环境变量,及这个环境变量指向的配置⽂件
export ANSIBLE_CONFIG=/PATH
./ansible.cfg :其次,将会检查当前目录下的ansible.cfg配置⽂件
~/.ansible.cfg :再次,将会检查当前用户home目录下的.ansible.cfg配置⽂件
/etc/ansible/ansible.cfg :最后,将会检查软件包管理⼯具安装Ansible时产⽣的配置⽂件(默认配置文件)
在没有手动定义 export ANSIBLE_CONFIG=/PATH的背景下:
[root@master ~]# ls //当前目录下没有ansible.cfg的配置文件
anaconda-ks.cfg
[root@master ~]# pwd //当前用户的家目录下也没有隐藏的.ansible.cfg的配置文件
/root
[root@master ~]# ls -a
. anaconda-ks.cfg .bash_history .bash_profile .cshrc .tcshrc
.. .ansible .bash_logout .bashrc .ssh .viminfo[root@master ~]# ansible --version //所以找的是默认配置文件
ansible [core 2.13.3]config file = /etc/ansible/ansible.cfg configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python3.9/site-packages/ansibleansible collection location = /root/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/bin/ansiblepython version = 3.9.16 (main, Dec 8 2022, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)]jinja version = 3.1.2libyaml = True
[root@master ~]# touch ansible.cfg //在当前目录下创建一个ansible.cfg的配置文件
[root@master ~]# ls
anaconda-ks.cfg ansible.cfg
[root@master ~]# ansible --version //找的就是当前目录下的ansible.cfg的配置文件
ansible [core 2.13.3]config file = /root/ansible.cfgconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python3.9/site-packages/ansibleansible collection location = /root/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/bin/ansiblepython version = 3.9.16 (main, Dec 8 2022, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)]jinja version = 3.1.2libyaml = True[root@master ~]# rm -rf ansible.cfg //删除掉当前目录下的ansible.cfg的配置文件
[root@master ~]# ls
anaconda-ks.cfg
[root@master ~]# touch .ansible.cfg //在用户的家目录下创建一个隐藏的.ansible.cfg的配置文件
[root@master ~]# ansible --version //找的就是用户的家目录下创建一个隐藏的.ansible.cfg的配置文件
ansible [core 2.13.3]config file = /root/.ansible.cfgconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python3.9/site-packages/ansibleansible collection location = /root/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/bin/ansiblepython version = 3.9.16 (main, Dec 8 2022, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)]jinja version = 3.1.2libyaml = True
[root@master ~]# touch ansible.cfg
[root@master ~]# ls -a
//如果既有当前目录下的ansible.cfg的配置文件,又有用户的家目录下隐藏的.ansible.cfg的配置文件
. anaconda-ks.cfg .ansible.cfg .bash_history .bash_profile .cshrc .tcshrc
.. .ansible ansible.cfg .bash_logout .bashrc .ssh .viminfo
[root@master ~]# ansible --version //找的就是当前目录下的ansible.cfg的配置文件
ansible [core 2.13.3]config file = /root/ansible.cfgconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python3.9/site-packages/ansibleansible collection location = /root/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/bin/ansiblepython version = 3.9.16 (main, Dec 8 2022, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)]jinja version = 3.1.2libyaml = True
注意:
剧本里定义的优先级比配置文件定义的优先级要高,所以当剧本的定义和配置文件的定义发生冲突时,我们首先考虑剧本里面的定义(但仅限于执行此剧本)
三、主配置文件/etc/ansible/ansible.cfg解析
[defaults] 默认配置
#inventory = /etc/ansible/hosts #主机列表配置文件
#remote_tmp = ~/.ansible/tmp #临时py命令文件存放在远程主机目录
#forks = 5 #默认并发数 (在Linux中因为任务是在受控主机里面执行所以没有影响,在网络设备中因为是在节点里面执行,所以存在影响,值不能过大)
#remote_port = 22 #远程端口号
#remote_user = root ----连接的时候使用什么用户进行连接
#roles_path = /etc/ansible/roles
#host_key_checking = False
#collections_path = 定义集合的路径,第三方模块通常安装在此目录下
[privilege_escalation] 定义对受管主机执行特权升级,默认普通用户是没有权限来执行ansible任务的,但是我们可以给普通用户提权,让它有权限去执行ansible任务
become = true //开启提权
become_method = sudo //提权方式sudo
become_user = root //将普通用户提权成为root用户
become_ask_pass = false //提权时不需要输入密码
[paramiko_connection]、[ssh_connection]、[accelerate]用于优化与受管主机的连接
[selinux] 定义如何配置selinux交互
四、主机清单
清单定义ansible将要管理的一批主机。(写了/etc/hosts可以解析受控主机的别名,没有则可以写对应IP地址)
这些主机也可以分配到组中,以进行集中管理。组可以包含子组,主机也可以是多个组的成员。清单还可以设置应用到它所定义的主机和组的变量。
例:
创建名为/home/student/ansible/inventory的静态清单文件, 以满足以下需求:
node1是dev主机组的成员
node2是test主机组的成员
node3是prod主机组的成员
prod组是webservers主机组的成员
[student@master ~]$ cd /home/student/ansible/
[student@master ansible]$ ls
ansible.cfg collections inventory roles
[student@master ansible]$ vim inventory
[student@master ansible]$ cat inventory
[dev]
node1[test]
node2[prod]
node3[webservers:children]
prod
[student@master ansible]$ ansible all -i inventory --list-hostshosts (3):node1node2node3
[student@master ansible]$ ansible dev -i inventory --list-hostshosts (1):node1