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文件

[root@control ~]# cat inventory 
manage1
manage2
manage3

2、配置ip

[root@control ~]# cat inventory 
192.168.100.137
192.168.100.138
192.168.100.139

3、配置组

[root@control ~]# cat inventory 
[storage1]
manage1
manage2
manage3

[stroage2]
manage1
manage2

[storage3]
manage1
manage3

4、嵌套组

前提是需要配置组

[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个主机就可以使用定义范围的方式

[root@control ~]# cat inventory  
 192.168.0.[0:255]
 192.168.1.[0:255]
 
 manage[1:100]
 server[a:z]

将主机加入到组内,主机可以重复,这样,ansible就可以控制一个组内的主机进行操作

3、指定配置文件,并查看

[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 <document start>  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的配置文件在安装的是时候,就有一个缺省的配置文件

[root@control ~]# rpm -qf /etc/ansible/ansible.cfg 
ansible-core-2.14.9-1.el9.x86_64

如果你不想使用这个安装自带的ansible配置文件,你可以自己创建

一般在生产环境中,都是创建一个你自己的目录,然后再该目录下创建自己的ansible配置文件

2、查看当前正在使用的配置文件

[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

[root@control ~]# ansible --version | grep cfg
  config file = /etc/ansible/ansible.cfg

2、家目录下的.ansible.cfg优先级高于/etc/ansible/ansible.cfg

[root@control ~]# touch ~/.ansible.cfg
[root@control ~]# ansible --version | grep cfg
  config file = /root/.ansible.cfg

3、当前目录下的ansible.cfg优先级高于.ansible.cfg

[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变量指定的配置文件优先级最高

[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、生成配置文件

当没有头绪的时候,可以查看实例文件

[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生成一个默认的配置文件

使用命令自动生成一个配置文件

[root@control ~]# ansible-config init --disabled > ansible.cfg

2、查看配置文件

cfg类型的文件中一般使用#;开头,表示注释

 过滤掉注释和多余的空行

[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]

[defaults]
inventory=/etc/ansible/hosts
##  表示该配置文件默认使用的inventory文件是/etc/ansible/hosts
remote_user=user
## 表示给配置文件使用user用户来进行ssh连接
ask_pass=False
## 表示使用ergou用户去ssh连接的时候不提示输入密码
[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命令不提示输入密码

相关推荐

  1. MySQL 中数据文件配置文件

    2024-07-19 23:44:01       30 阅读
  2. nginx配置文件详解

    2024-07-19 23:44:01       38 阅读
  3. MyBatis核心配置文件

    2024-07-19 23:44:01       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 23:44:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 23:44:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 23:44:01       45 阅读
  4. Python语言-面向对象

    2024-07-19 23:44:01       55 阅读

热门阅读

  1. 【算法基础】Dijkstra 算法

    2024-07-19 23:44:01       20 阅读
  2. MyBatis中的优点和缺点?

    2024-07-19 23:44:01       13 阅读
  3. linux 挂载u盘。卸载u盘

    2024-07-19 23:44:01       18 阅读
  4. 采购管理者常用的管理工具有哪些?

    2024-07-19 23:44:01       19 阅读
  5. MySQL零散拾遗(三)

    2024-07-19 23:44:01       18 阅读
  6. ArcEngine 非SDE方式加载postgis数据

    2024-07-19 23:44:01       19 阅读
  7. C语言习题~day32

    2024-07-19 23:44:01       17 阅读
  8. 安康古韵长,汉水碧波扬

    2024-07-19 23:44:01       14 阅读