更新时间:2023 年 5 月

简介

ansible 官网:Ansible is Simple IT Automation

ansible 官方文档:Ansible Documentation

ansible 的 github 地址:https://github.com/ansible/ansible

功能

  • 批量执行远程命令,可以对远程的多台主机同时进行命令的执行
  • 批量安装和配置软件服务,可以对远程的多台主机进行自动化的方式配置和管理各种服务
  • 编排高级的企业级复杂的 IT 架构任务,Ansible 的 Playbook 和 Role 可以轻松实现大型的 IT 复杂架构
  • 提供自动化运维工具的开发 API,有很多运维工具,如 Jumpserver 就是基于 ansible 实现自动化管理功能

特性

  • 模块化:调用特定的模块完成特定任务,支持自定义模块,可使用任何编程语言写模块

  • Paramiko(python 对 ssh 的实现模块),PyYAML,Jinja2(模板语言)三个关键模块

  • 基于 Python 语言实现

  • 部署简单,基于 Python 和 SSH(默认已安装),agentless,无需代理不依赖 PKI(无需 ssl)

  • 安全,连接基于 OpenSSH

  • 幂等性:一个任务执行 1 遍和执行 n 遍效果一样,不因重复执行带来意外情况,此特性非绝对

  • 支持 Playbook 编排任务,使用 YAML 格式编排任务,支持丰富的数据结构

  • 较强大的多层解决方案 Role

组成

  • INVENTORY:Ansible 管理主机的清单,默认为 /etc/ansible/hosts
  • MODULES:Ansible 执行命令的功能模块,多数为内置核心模块,也可自定义
  • PLUGINS:模块功能的补充,如连接类型插件、循环插件、变量插件、过滤插件等,该功能不常用
  • API:供第三方程序调用的应用程序编程接口

注意事项

  • 执行 ansible 的主机一般称为管理端,主控端,中控,master 或堡垒机

  • 主控端要求参考:Installing Ansible — control-node-requirements

    以下列出一些主控端注意事项:

    • Python 版本 3.9+

    • Windows 不能做为主控端

  • 被控端要求参考:Installing Ansible — managed-node-requirements

    以下列出一些被控端注意事项:

    • Python 版本 2.7、Python 版本 3.5 - 3.11
    • 被控端 Python 版本小于 2.4,需要安装 python-simplejson
    • 被控端如开启 SELinux 需要安装 libselinux-python

目前主流 ansible 版本所需的 Python 版本如下表

ansible-core VersionControl node PythonManaged node Python
2.11Python 2.7, Python 3.5 - 3.9+Python 2.6 - 2.7, Python 3.5 - 3.9
2.12Python 3.8 - 3.10Python 2.6 - 2.7, Python 3.5 - 3.10
2.13Python 3.8 - 3.10Python 2.7, Python 3.5 - 3.10
2.14Python 3.9 - 3.11Python 2.7, Python 3.5 - 3.11

其他注意事项

  • ansible 从 2.10 版本开始分支一个极简对象 ansible-core(2.10 版本称为 ansible-base),请根据需要安装版本;也因此,只要是跨越 2.10 版本的升级,只能卸载重装

  • ansibleansible-core 主要区别在于 ansible-core 只包含基础的运行环境,而 ansible 多了一些官方或社区的插件(及模块)

  • 因为 ansible 2.10 版本问题,不建议使用

  • 至 2021 年 11 月 14 日,RHEL(CentOS)暂不支持 ansible 2.10 以上版本,请使用 ansible 2.9(至 2022 年 10 月 18 日,已经支持使用)

安装ansible

参考:Installing Ansible — Ansible Documentation

准备

Alma 9.2 一台

$ cat /etc/redhat-release 
AlmaLinux release 9.2 (Turquoise Kodkod)

备份并替换默认源(可选)

# 备份并替换
$ sed -e 's|^mirrorlist=|#mirrorlist=|g' \
      -e 's|^# baseurl=https://repo.almalinux.org|baseurl=https://mirrors.aliyun.com|g' \
      -i.bak \
      /etc/yum.repos.d/almalinux*.repo
      
# 缓存
$ dnf makecache

DNF 安装(core 2.14.2)

安装 EPEL 源

$ dnf config-manager --set-enabled crb

$ dnf install epel-release -y

安装 ansible

# 查看 ansible 软件包信息
$ dnf info ansible
Extra Packages for Enterprise Linux 9 - x86_64                                                          2.7 MB/s |  16 MB     00:05    
Last metadata expiration check: 0:00:08 ago on Sun 21 May 2023 12:28:01 AM CST.
Available Packages
Name         : ansible
Version      : 7.2.0
Release      : 1.el9
Architecture : noarch
Size         : 38 M
Source       : ansible-7.2.0-1.el9.src.rpm
Repository   : epel
Summary      : Curated set of Ansible collections included in addition to ansible-core
URL          : https://ansible.com
License      : GPL-3.0-or-later AND Apache-2.0 AND BSD-2-Clause AND BSD-3-Clause AND MIT AND MPL-2.0 AND PSF-2.0
Description  : Ansible is a radically simple model-driven configuration management,
             : multi-node deployment, and remote task execution system. Ansible works
             : over SSH and does not require any software or daemons to be installed
             : on remote nodes. Extension modules can be written in any language and
             : are transferred to managed machines automatically.
             : 
             : This package provides a curated set of Ansible collections included in addition
             : to ansible-core.




# 安装 ansible
$ dnf install ansible -y


# 查看版本
$ ansible --version
ansible [core 2.14.2]
  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.11/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.11.2 (main, Feb 16 2023, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True

pip 安装(core 2.14.2)

安装常用工具

$ dnf -y install vim wget

验证 python 版本

$ python --version
Python 3.9.16

安装 setuptools

也可以 epel 下载安装,此处使用 python 离线安装

setuptools 下载地址:https://pypi.org/project/setuptools/#files

$ SETUPTOOLS_VERSION=67.8.0

# 下载
$ wget https://files.pythonhosted.org/packages/03/20/630783571e76e5fa5f3e9f29398ca3ace377207b8196b54e0ffdf09f12c1/setuptools-67.8.0.tar.gz

# 解压 
$ tar -zxf setuptools-${SETUPTOOLS_VERSION}.tar.gz -C /usr/local/src/


# 安装
$ cd /usr/local/src/setuptools-${SETUPTOOLS_VERSION}
$ python setup.py install

安装 pip

pip 下载地址:pip · PyPI

下载解压

$ PIP_VERSION=23.1.2

#  下载
$ wget https://files.pythonhosted.org/packages/fa/ee/74ff76da0ab649eec7581233daeb43d8aa35383d8f75317b2ab3b80c922f/pip-${PIP_VERSION}.tar.gz

# 解压
$ tar -zxf pip-${PIP_VERSION}.tar.gz -C /usr/local/src/

安装

$ cd /usr/local/src/pip-${PIP_VERSION}

$ python setup.py install

检查安装情况

$ python3 -m pip -V
pip 23.1.2 from /usr/local/lib/python3.9/site-packages/pip-23.1.2-py3.9.egg/pip (python 3.9)

安装 ansible

下载

# 创建存放 whl 软件包路径
$ mkdir /root/ansible

# 下载软件包及其依赖
$ python3 -m pip download ansible -d  /root/ansible

# 查看下载的软件包
$ ls /root/ansible
ansible-7.5.0-py3-none-any.whl
ansible_core-2.14.5-py3-none-any.whl
cffi-1.15.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
cryptography-40.0.2-cp36-abi3-manylinux_2_28_x86_64.whl
Jinja2-3.1.2-py3-none-any.whl
MarkupSafe-2.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
packaging-23.1-py3-none-any.whl
pycparser-2.21-py2.py3-none-any.whl
PyYAML-6.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
resolvelib-0.8.1-py2.py3-none-any.whl

安装

$ python3 -m pip install ansible --user --find-links=/root/ansible

# 删除 plugin_filters 文件,默认不过滤任何插件
$ rm -rf /etc/ansible/plugin_filters.yml

检查版本

$ ansible --version
[WARNING]: Unable to parse the plugin filter file /etc/ansible/plugin_filters.yml as module_blacklist is not a list. Skipping.
ansible [core 2.14.5]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /root/.local/lib/python3.9/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /root/.local/bin/ansible
  python version = 3.9.16 (main, Dec  8 2022, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True