joeの小窝

Loading...

ansible基础

# 主要介绍这个ansible的原理,安装的方式

# ansible的配置文件讲解,ansible的配置普通用户管理

ansible基础

一、概述

1、ansible简介

  • ansible是一个自动化的工具,不需要服务启动,通过这个python开发,无代理结构

  • 基于模块化工作的,本身没有批量部署的能力,具有批量部署的是ansible所运行的模块,不需要安装客户端,因为通过ssh远程管理的

2、ansible特点

  • agentless 不需要在被管理节点上面安装客户端,默认使用的就是sshd,在安装系统的时候就已经安装好了

  • serverless 服务端不需要启动任何服务,只需要执行命令就行,ansible是一个工具

  • modules in any language 基于模块工作

  • 对于简单的命令使用 ad-hoc,复杂的任务使用ansible的playbook来实现,ansible的每一条的命令就是对于这个模块的调用

  • 可以实现多级的控制,有一个主控节点,管理3个被控节点,然后这个被控节点又充当主控节点管理多个被控节点,这样的话,分担主控节点的压力

  • ansible也有一个web界面,AWX平台

3、ansible的功能和优势

  • 自动化的执行环境

    • 将ansible的软件包封装在容器镜像之中,通过拉取不同的容器镜像获得不同的ansible版本,而不同ansible的版本之间所能实现特性是不同的,(例如ansible2.8中可能不支持模块A,但是ansible2.6是支持模块A,因此可以拉取ansible2.6的容器镜像来执行模块A的指令),ansible的自动化执行环境实现ansible版本与模块的分离
  • 实现自动化的标准和规范化

    • 通过自动化控制中心(ansible-tower)拥有一整套完整的流程和权限管理机制,流程指的是可以根据定制执行流,同时可以通过外部的第三方代码库来管理和开发ansible的playbook也就是剧本(比如通过git来管理playbook)
  • 自动化网络管理

    • 通过网络管理的模块来实现网络的自动化

4、有代理架构和无代理架构

  • 有代理架构就是一个C/S,被管理节点需要安装客户端,通过客户端执行服务端下发的指令

  • 无代理架构,指的使用应用携带的能力来管理应用本身,不需要专门去安装一个客户端,ssh

二、ansible的架构

1、ansible的架构图

img

  • ansible: ansible的核心程序

  • host invntory: ansible的主机清单,也就是ansible能够管理的被控节点,需要在主机清单中定义才行,且ansible只能管理主机清单中的主机,其他的不能管理

  • playbook:ansible的剧本,在里面定义一系列的任务,通过执行剧本达到所有任务执行的目的

  • core modules:ansible核心的模块,大部分都是自带的,在安装的时候就有的

  • custom modules: 扩展模块,弥补核心模块不足的问题

  • connection plugins:连接插件,linux默认使用ssh,windows使用rm插件

2、ansible的工作原理

  • 用户下发任务到ansible主程序,如果任务是一个playbook,则ansible根据剧本中定义的主机,在主机清单中选择该主机,然后使用连接插件连接到被控端,连接后根据剧本定义的任务选择响应的模块,并将其推送到被控端执行,执行后删除在被控端的模块,完成任务的执行

3、ansibel的执行过程

  • 首先加载自己的配置文件,默认是/etc/ansible/ansibe.cfg

  • 查找对应的主机配置文件,找到要执行的主机或者组

  • 加载自己对应的模块文件

  • 通过ansible将模块或者命令生成对应的临时py文件(也就是python脚本),并且将这个文件传输到远程服务器

  • 对应执行用户的家目录的.ansible/tmp/xxx/xxx.py文件

  • 给文件执行x权限

  • 执行并返回结果

  • 删除临时py文件

三、ansible安装方式

1、rpm包安装ansible

欧拉的仓库自带ansible的源,安装后,会有配置文件的

yum -y install ansible

# 模块有3387个,非常的多,如果是centos或者redhat的话,默认是安装的ansible-core,也就是只安装核心模块,只有十几个
[root@server ~]# ansible-doc -l | wc -l
3387

# 查看版本

[root@server ~]# ansible --version
ansible 2.9.27  # 版本是2.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
  executable location = /usr/bin/ansible
  python version = 3.9.9 (main, Dec 28 2023, 13:48:32) [GCC 10.3.1]

2、源码包安装ansible

  • 编译安装的话,是没有配置文件的,需要自己生成

  • https://github.com/ansible/ansible ansible源码包,里面有最新版本的

  • 如果下载不下来的话,使用github代理

wget https://ghfast.top/https://github.com/ansible/ansible/archive/refs/tags/v2.9.27.zip

# unzip解压
unzip v2.9.27.zip

# 安装一些编译需要的依赖包(后面编译的时候出现了错误,就需要安装缺少的包)
yum -y install gcc libffi-devel openssl-devel python3 python3-devel  python3-cryptography

cd ansible-2.9.27/

python3 setup.py build

python3 setup.py install

# 查看版本
[root@server ~]# ansible --version
ansible 2.9.27
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.9/site-packages/ansible-2.9.27-py3.9.egg/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.9.9 (main, Dec 28 2025, 14:12:37) [GCC 10.3.1]

这个编译安装可以自定义的,python3 setup.py install --prefix=/opt/ansible

编译安装后,怎么删除了?

# 默认安装的话,首先删除ansible的一些命令

[root@server lib]# which ansible
/usr/local/bin/ansible

[root@server lib]# cd /usr/local/bin/
[root@server bin]# ls
ansible             ansible-console  ansible-inventory  ansible-test
ansible-config      ansible-doc      ansible-playbook   ansible-vault
ansible-connection  ansible-galaxy   ansible-pull

# 删除所有ansible命令
[root@server bin]# rm -rf ansible*
[root@server bin]# ls

# 删除ansible的一些模块
[root@server lib]# ls
python3.9

# 删除成功了

3、pip安装

使用的是清华大学的镜像源,

  • 也是需要自己生成配置文件的
# 默认是安装的最新版本的
pip install ansible -i https://pypi.tuna.tsinghua.edu.cn/simple

[root@server ~]# ansible --version
ansible [core 2.15.13]  # 安装的是2.15.13版本
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.9.9 (main, Dec 28 2025, 14:12:37) [GCC 10.3.1] (/usr/bin/python3)
  jinja version = 3.1.6
  libyaml = True

# 大于2.9版本的ansible可以生成配置文件
[root@server ansible]# ansible-config init > ansible.cfg

删除pip安装的ansible

# 列出安装的包
pip list | grep ansible

# 删除包
pip uninstall ansible-core
pip uninstall ansible

四、ansible的配置文件

主要就是详细介绍ansible配置文件,常见的配置

1、ansible配置段

  • [defaults] 通用的配置项,一些基础的配置就是在这个下面配置

  • [inventory] 定义主机清单的配置项

  • [privilege_escalation] 提权的配置项

  • [paramiko_connection] 旧版本的paramiko连接插件的配置项

  • [ssh_connection] ssh连接的配置项

  • [persistent_connection] 持久化连接配置项

  • [accelerate] 加速的配置项

  • [selinux] selinux的配置项

  • [colors] 任务的输出颜色配置项

  • [diff] 打印任务前后的差异

2、ansible配置文件详解

1、defaults段

[defaults]
inventory = /etc/ansible/hosts  # 主机清单的路径,ansible管理的被控节点存放的位置

library = /usr/share/my_modules/ # ansible库存放的路径

forks = 5 # 主机任务的并发数,表示可以同时在5个主机上面执行任务

ask_sudo_pass = false # 表示被控节点提权的时候不需要输入密码

ask_pass = false # 表示管理被控节点的时候不需要输入密码,反之,则需要密码

remote_user = root  # 表示连接被控节点时使用的用户

host_key_checking = false # 表示不需要检查被控节点的主机公钥,在主控节点连接被控节点的时候,需要将主控公钥分发给被控节点上面,这样才能连接上去,这个参数可以不用管这个

deprecation_warnings=False  # 关闭警告

log_path = /var/log/ansible.log # 指定一个存储ansible日志文件,默认是不记录日志的

2、privilege_escalation段

become=True  # 是否提权
become_method=sudo  # 提权的方式是sudo
become_user=root  # 提权到root用户
become_ask_pass=False  # 提权的时候不需要输入密码

3、ssh_connection

ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s  # ssh 的连接选项 -C 启用压缩传输, ControlPersist 用于提升性能,存在60秒,不需要执行下一个任务的时候建立一个新的连接

3、ansible配置文件的优先级

  • 高到低顺序为 环境变量 > 当前的工作目录 > 家目录 > /etc/ansible/ansible.cfg

  • 环境变量是 ansible_config

  • 当前工作目录 ansible.cfg

  • 家目录下面是隐藏的文件 .ansible.cfg

五、普通用户管理被控节点

  • 在实际中,使用的是普通用户管理的ansible

  • 全程在主控节点使用ansible来完成

主控节点安装ansible

yum -y install ansible

编写ansible配置文件,主机清单,域名解析

[root@server ansible]# cat /etc/hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.50.21 node1
192.168.50.22 node2

[root@server ansible]# cat hosts 
node1
node2

[root@server ansible]# cat ansible.cfg
[defaults]
inventory = /etc/ansible/hosts

测试连接是否成功

root@server ansible]# ansible all -m ping -u root -k
SSH password: 
[WARNING]: Platform linux on host node1 is using the discovered Python interpreter at
/usr/bin/python3, but future installation of another Python interpreter could change this.
See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for
more information.
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
[WARNING]: Platform linux on host node2 is using the discovered Python interpreter at
/usr/bin/python3, but future installation of another Python interpreter could change this.
See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for
more information.
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

# 连接成功了,却发现了这个python警告的信息。就是出现了多个python

# 第一个解决方式就是在主机清单中指定特定的版本

[root@server ansible]# cat hosts 
node1 ansible_python_interpreter=/usr/bin/python3
node2 ansible_python_interpreter=/usr/bin/python3

# 第二个解决方式就是在配置文件中忽略掉这些信息

[root@server ansible]# cat ansible.cfg
[defaults]
inventory = /etc/ansible/hosts
interpreter_python=auto_legacy_silent

创建普通用户,提权

[root@server ansible]# ansible all -m shell -a "useradd devops && echo '123' | passwd --stdin devops " -u root -k 
SSH password: 
node1 | CHANGED | rc=0 >>
Changing password for user devops.
passwd: all authentication tokens updated successfully.
node2 | CHANGED | rc=0 >>
Changing password for user devops.
passwd: all authentication tokens updated successfully.

[root@server ansible]# ansible all -m shell -a "echo 'devops ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers" -u root -k
SSH password: 
node1 | CHANGED | rc=0 >>

node2 | CHANGED | rc=0 >>

# 可以去被控节点测试一下,看是否拥有这个root权限

主控节点免密登录被控节点

[root@server ansible]# useradd devops && echo 123 | passwd --stdin devops
Changing password for user devops.
passwd: all authentication tokens updated successfully.

ssh-keygen
ssh-copy-id devops@node1
ssh-copy-id devops@node2

# 在主控节点的devops用户下创建ansible目录
mkdir ansible

[devops@server ansible]$ cat ansible.cfg 
[defaults]
inventory = ./hosts
remote_user = devops
ask_pass = false
ask_sudo_pass = false
interpreter_python=auto_legacy_silent

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

[devops@server ansible]$ cat hosts 
node1
node2


测试普通用户管理被控节点

[devops@server ansible]$ ansible all -m ping 
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

总结

  • 普通用户管理被控节点注意事项

    • 免密登录需要ssh的配置,ask_pass=false 一起生效

    • 提权的时候不输入密码,sudoers文件和ask_sudo_pass=false 一起生效

    • 发送密钥的时候,注意是以devops用户发送,而不是root用户

  • ansible的特点

    • 非常的简洁,容易操作

    • 被控节点不需要安装客户端,就能控制

    • 具有幂等性

posted @ 2026-03-02 11:03  乔的港口  阅读(23)  评论(0)    收藏  举报