ansible——ansible的配置文件

一、ansible的inventory文件

1、什么是inventory文件

inventory文件定义了ansible管理的主机,说白了就是inventory文件中的内容是被管理的主机

inventory文件分为两种,一种是静态的inventory文件,一种是动态inventory文件

静态的inventory文件,其实就是txt文本记录的被管理主机。只要不修改这个静态inventory文件,被管理的主机就不会发生变化

动态的inventory文件指动态的输出被管理主机。动态inventory文件的原理就是一个脚本,大部分都是python的脚本,动态inventory文件能连接到某个管理系统的节点信息数据库,并将节点信息以特定的格式输出(绝大多数情况下是json),也就是说动态inventory文件输出的主机信息是管理系统节点信息数据库的内容。也就是说节点信息数据库的变化就会导致inventory文件输出的被管理主机数量不同。所以称之为动态inventory文件

2、配置inventory

配置inventory有五种方式,可以根据需求进行配置

1、配置主机名

需要提前配置好/etc/hosts文件

bash 复制代码
[root@control ~]# cat inventory 
manage1
manage2
manage3

2、配置ip

bash 复制代码
[root@control ~]# cat inventory 
192.168.100.137
192.168.100.138
192.168.100.139

3、配置组

bash 复制代码
[root@control ~]# cat inventory 
[storage1]
manage1
manage2
manage3

[stroage2]
manage1
manage2

[storage3]
manage1
manage3

4、嵌套组

前提是需要配置组

bash 复制代码
[root@control ~]# cat inventory 
[storage1]
manage1
manage2
manage3

[stroage2]
manage1
manage2

[storage3]
manage1
manage3

[computer:children]
storage1
storage2

NAME:children\]:这个下面定义的是上面组的内容 #### 5、定义主机范围 假设像定义192.168.0.0\~192.168.0.255和192.168.1.0\~192.168.1.255,总共512个主机就可以使用定义范围的方式 ```bash [root@control ~]# cat inventory 192.168.0.[0:255] 192.168.1.[0:255] manage[1:100] server[a:z] ``` 将主机加入到组内,主机可以重复,这样,ansible就可以控制一个组内的主机进行操作 ### 3、指定配置文件,并查看 ```bash [root@control ~]# ansible -i /root/inventory storage1 --list-host [WARNING]: * Failed to parse /root/inventory with yaml plugin: We were unable to read either as JSON nor YAML, these are the errors we got from each: JSON: Expecting value: line 1 column 1 (char 0) Syntax Error while loading YAML. did not find expected The error appears to be in '/root/inventory': line 7, column 1, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: ## 第二种配置方式 192.168.100.137 ^ here [WARNING]: * Failed to parse /root/inventory with ini plugin: /root/inventory:27: Section [computer:children] includes undefined group: stroage1 [WARNING]: Unable to parse /root/inventory as an inventory source [WARNING]: No inventory was parsed, only implicit localhost is available hosts (3): manage1 manage2 manage3 ``` * `-i`:指定inventory文件的路径 * `storage1`:表示storage主机组 * `--list--host`:表示列出你想查看的主机 由于在你的ansible控制节点里面可能有多个Inventory文件,所以使用正确的Inventory文件就非常有必要。当然还有其他方式可以指定Inventory文件的路径,但是-i参数的优先级是最高的。 `/etc/ansible/hosts`是ansible配置文件指定Inventory默认的文件。如果使用ansible命令不加-i参数,默认就会使用这个Inventory文件。 ## 二、ansible配置文件 ### 1、关于ansible的配置文件 关于ansible的配置文件,有一些非常重要的知识需要掌握 ansible的配置文件不是全局的,任何用户都可以有自己的ansible配置文件 ansible的配置文件在安装的是时候,就有一个缺省的配置文件 ```bash [root@control ~]# rpm -qf /etc/ansible/ansible.cfg ansible-core-2.14.9-1.el9.x86_64 ``` 如果你不想使用这个安装自带的ansible配置文件,你可以自己创建 一般在生产环境中,都是创建一个你自己的目录,然后再该目录下创建自己的ansible配置文件 ### 2、查看当前正在使用的配置文件 ```bash [root@control ~]# ansible --version ansible [core 2.14.9] 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/ansible ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections executable location = /usr/bin/ansible python version = 3.9.18 (main, Sep 7 2023, 00:00:00) [GCC 11.4.1 20230605 (Red Hat 11.4.1-2)] (/usr/bin/python3) jinja version = 3.1.2 libyaml = True [root@control ~]# ansible --version | grep cfg config file = /etc/ansible/ansible.cfg ``` ### 3、配置文件的级别 ansible的配置文件有四种可以指定,四种指定方式优先级是不同的、 1、如果没有其他任何ansible配置文件,默认就会使用`/etc/ansible/ansible.cfg` ```bash [root@control ~]# ansible --version | grep cfg config file = /etc/ansible/ansible.cfg ``` 2、家目录下的`.ansible.cfg`优先级高于`/etc/ansible/ansible.cfg` ```bash [root@control ~]# touch ~/.ansible.cfg [root@control ~]# ansible --version | grep cfg config file = /root/.ansible.cfg ``` 3、当前目录下的`ansible.cfg`优先级高于`.ansible.cfg` ```bash [root@control opt]# pwd /opt [root@control opt]# touch ansible.cfg [root@control opt]# ansible --version | grep cfg config file = /opt/ansible.cfg ``` 4、ANSIBLE_CONFIG变量指定的配置文件优先级最高 ```bash [root@control opt]# touch /tmp/ansible.cfg [root@control opt]# export ANSIBLE_CONFIG=/tmp/ansible.cfg [root@control opt]# ansible --version | grep cfg config file = /tmp/ansible.cfg ``` ### 4、配置文件的基本参数 #### 1、生成配置文件 当没有头绪的时候,可以查看实例文件 ```bash [root@control ~]# cat /etc/ansible/ansible.cfg # Since Ansible 2.12 (core): # To generate an example config file (a "disabled" one with all default settings, commented out): # $ ansible-config init --disabled > ansible.cfg # # Also you can now have a more complete file by including existing plugins: # ansible-config init --disabled -t all > ansible.cfg # For previous versions of Ansible you can check for examples in the 'stable' branches of each version # Note that this file was always incomplete and lagging changes to configuration settings # for example, for 2.9: https://github.com/ansible/ansible/blob/stable-2.9/examples/ansible.cfg ``` 在这个版本中,这个目录里面没有默认的配置文件,但是这个文件中说可以使用`ansible-config init --disabled > ansible.cfg`生成一个默认的配置文件 使用命令自动生成一个配置文件 ```bash [root@control ~]# ansible-config init --disabled > ansible.cfg ``` #### 2、查看配置文件 > cfg类型的文件中一般使用`#`和`;`开头,表示注释 过滤掉注释和多余的空行 ```bash [root@control ~]# grep -v "#" ansible.cfg | grep -v "^$" | grep -v ";" [defaults] [privilege_escalation] [persistent_connection] [connection] [colors] [selinux] [diff] [galaxy] [inventory] [netconf_connection] [paramiko_connection] [jinja2] [tags] ``` ansible的配置文件中是以sector作为划分的。每个方括号就表示一个sector #### 3、主要参数说明 主要使用的有两个组,\[defaults\]和\[privilege_escalation

bash 复制代码
[defaults]
inventory=/etc/ansible/hosts
##  表示该配置文件默认使用的inventory文件是/etc/ansible/hosts
remote_user=user
## 表示给配置文件使用user用户来进行ssh连接
ask_pass=False
## 表示使用ergou用户去ssh连接的时候不提示输入密码
bash 复制代码
[privilege_escalation]
## 如果你remote_user使用的是root用户,就不需要配置提权部分,如果你的remote_user不是root,但是不需要做特权操作,那么也可以不用配置这部分。如果是普通用户,但是需要做特权操作,那么就需要配置这部分。
become=true
## true表示需要提权,false就表示不需要提权
become_method=sudo
## 表示提权的方式是sudo提权
become_user=root
## 表示提权到root用户:
become_ask_pass=false
#false表示进行sudo操作的时候不提示输入密码,true表示需要输入密码

"不是任何用户作为remote_user,且配置了提权就能真的提权。而必须要在被管理主机里面配置sudoers文件,让这个remote_user有提权的能力才可以。"

ssh可以设置免密登陆,避免remote_user使用ssh登陆的时候需要输入密码

sudoers文件中也可以设置sudo命令不提示输入密码

相关推荐
红肤色8 分钟前
【网络安全基础】CentOS 7超详细安装教程(含镜像)
linux·运维·服务器·安全·网络安全·centos
程序猿(雷霆之王)30 分钟前
Linux——冯 • 诺依曼体系结构&操作系统初识
linux·运维·服务器
Double Point31 分钟前
(三十三)Dart 中使用 Pub 包管理系统与 HTTP 请求教程
网络·网络协议·http
无名之逆1 天前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
大丈夫立于天地间1 天前
ISIS协议中的数据库同步
运维·网络·信息与通信
Dream Algorithm1 天前
路由器的 WAN(广域网)口 和 LAN(局域网)口
网络·智能路由器
IT猿手1 天前
基于CNN-LSTM的深度Q网络(Deep Q-Network,DQN)求解移动机器人路径规划,MATLAB代码
网络·cnn·lstm
吴盐煮_1 天前
使用UDP建立连接,会存在什么问题?
网络·网络协议·udp
rainFFrain1 天前
单例模式与线程安全
linux·运维·服务器·vscode·单例模式
GalaxyPokemon1 天前
Muduo网络库实现 [九] - EventLoopThread模块
linux·服务器·c++