当前位置: 首页 > news >正文

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

在这里插入图片描述

http://www.dtcms.com/a/352331.html

相关文章:

  • 基于 LQG 控制的轨迹跟踪 —— 从原理到实践
  • 游隼可视化项目
  • python删除执行目录
  • 服装行业/服饰品牌OMS订单管理系统:全渠道零售时代的数字化中枢|商派
  • Chrome您的连接不是私密连接怎么办?试下手敲 thisisunsafe
  • Kafka 生态选型地图、最佳实践与落地清单
  • SELinux相关介绍
  • Android 属性 property 系统
  • MyBatis-Flex多表关联查询指南
  • Dify 父子模式详解:如何实现模块化与高效协作
  • 学习做动画4.回转运动
  • Docker移动安装目录的两种实现方案
  • Qwen3-Coder-30B-A3B-Instruct AWQ 量化
  • 基于51单片机的DS18B20大棚温度监控系统
  • TRUST:a thermohydraulic software package for CFD simulations,开源多物理场数值模拟平台
  • Decode Global:以合规资质筑牢全球服务的根基
  • 数据中台的下一步,是数据飞轮吗?
  • Maya绑定基础:创建骨骼、修改骨骼
  • Android之腾讯TBS文件预览
  • JSX深度解析:不是HTML,胜似HTML的语法糖
  • Milvus介绍及多模态检索实践
  • 坑机介绍学习研究1
  • 美的组织架构再调整,微清事业部划入洗衣机事业部
  • uniapp 顶部tab + 占满剩余高度的内容区域swiper
  • 低空经济的中枢神经:实时视频链路如何支撑通信、导航、监视与气象
  • C/C++---浮点数与整形的转换,为什么使用sqrt函数时,要给参数加上一个极小的小数(如1e-6)
  • “喵汪联盟”宠物领养系统的设计与实现(代码+数据库+LW)
  • LangGraph
  • 研究4:海外休闲游戏,如何给主角做萌化处理
  • 基于SpringBoot的摄影跟拍约拍预约系统【2026最新】