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

Ansible 介绍及安装

简介

Ansible 是一款开源的自动化工具,广泛应用于配置管理、应用部署、任务自动化以及多节点管理等领域。它由 Michael DeHaan 于 2012 年创建,ansible 目前已经已经被红帽官方收购,是自动化运维工具中大家认可度最高的,并且上手容易,学习简单。是每位运维工程师必须掌握的技能之一。

官网文档地址:Ansible Documentation — Ansible Community Documentation

特点:

  • 无代理架构

不需要再被控节点上安装任何专门的代理软件。通过 SSH 与被控节点通信,实现任务的执行和数据的传输

    优点

  1. 简化部署:无需在每个被控节点上安装和维护代理程序,减少运维工作量。

  2. 提高安全性:减少了潜在的攻击面,因为不需要开放额外的端口或运行额外的服务。

  3. 节省资源:避免了代理软件占用系统资源的问题。

  • 基于SSH

  • 通过 SSH 协议与被控节点通信,能都在大多数 Unix 系统上无缝运行。且支持Windows 主机,使用 PowerShell 或 WinRM 进行管理

   优点

  1. 广泛兼容性:支持多种操作系统,无需额外配置。

  2. 安全性高:利用现有的 SSH 安全机制,确保数据传输的机密性和完整性。

  • 声明式任务

  • 使用声明式语言(YAML)定义任务,描述的是目标状态而非具体的执行步骤。这是的配置更加直观易读,同时减少了错误和不一致性

    优点

  1. 可读性强:YAML 格式简洁明了,易于理解和编写。

  2. 可重用性高:通过剧本(Playbook) 和角色(Roles)的复用,实现复杂任务的模块化管理。

  3. 幂等性:确保多次运行相同的任务不会导致不一致的状态。

各个板块的概念

主机清单

Ansible 中用于管理目标主机的配置文件。它是一个文本文件,其中列出了要在其上执行 Ansible 任务的远程主机。
可以包含 IP 地址、域名或主机别名等信息,且可以将主机分组以便更好地组织和管理。文件默认位于 /etc/ansible/hosts 文件中,但也可以使用 -i 参数指定其他位置的清单文件。
#这是一个示例:
[web-servers]
webserver1.example.com
webserver2.example.com[database-servers]
dbserver1.example.com
dbserver2.example.com

 连接插件

Ansible 中的组件,用建立与远程主机的连接。Ansible 支持多种连接插件,包括 SSH、WinRM 等。连接插件的选择取决于目标主机的操作系统和配置。
通过连接插件,Ansible 可以与目标主机进行通信,并在其上执行任务。在主机清单中,可以通过在主机名后连接插件名称来指定连接插件。如果不指定,默认使用 SSH 连接插件。
这是一个示例
[web-servers]
webserver1.example.com ansible_connection=ssh
webserver2.example.com ansible_connection=ssh[database-servers]
dbserver1.example.com ansible_connection=ssh
dbserver2.example.com ansible_connection=winrm

模块

是 Ansible 的核心组件,用于在远程主机上执行任务。
Ansible 提供了丰富的内置模块,涵盖了各种任务,如文件操作、软件包管理、服务管理、用户管理等。还能通过在剧本中调用模块,可以实现自动化任务的执行。以下是一些常见的模块:
  • ping:检测远程主机的连通性。
  • command/shel:在目标主机上执行命令或命令字符串。
  • copy:将文件从控制节点复制到远程主机。
  • file:创建、修改或删除文件和目录。
  • template:使用 jinja2 模板生成文件,并将其复制到远程主机。
  • apt/yum:在基于 Debian/RedHat 的系统上安装、升级或移除软件包。
  • service:启动、停止、重新启动或重载系统服务。
  • user/group:创建、修改或删除用户和用户组。
  • lineinfile:在文中添加、修改或删除一行文本。
  • raw:在目标主机上执行原始命令,绕过模块系统。
  • wait_for:等待一定时间或直到某个条件为真。
  • script:在在目标主机上执行本地脚本。
  • git:克隆或更新 Git 代码库。
  • debug:打印调试信息。

Playbook【剧本】

是 Ansible 的核心组件之一,是一种以 YAML 格式编写的自动化任务描述文件。
每个 Playbook 由一个或多个 Play 组成。在每个 Play 下面,通过 tasks 关键字来定义一组任务。每个任务由一个或多个模块组成,用于在远程主机上实现自动化部署、配置和管理等操作。
# 这是一个示例
- name: Install and start Nginx # 描述 Playbook 或任务的简短名称hosts: web_servers # 指定要执行任务的目标主机或主机组become: yes # 可选参数,用于指定是否以管理员权限执行任务及执行任务的用户。tasks:- name: Install Nginxapt:name: nginxstate: present- name: Start Nginx serviceservice:name: nginxstate: started

安装操作

主机规划

主机IP说明
ansible-controller192.168.72.63安装ansible
ansible-node1192.168.72.64
ansible-node2192.168.72.65

环境准备【在对应的主机上执行命令】

配置主机名

# hostnamectl hostname ansible-controller# hostnamectl hostname ansible-node1# hostnamectl hostname ansible-node2

配置IP地址【改成自己主机所在的网段】

[root@ansible-controller ~]# nmcli c m ens160 ipv4.method manual ipv4.addresses 172.25.250.63/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
[root@ansible-controller ~]# nmcli c up ens160[root@ansible-node1 ~]# nmcli c m ens160 ipv4.method manual ipv4.addresses 172.25.250.64/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
[root@ansible-node1 ~]# nmcli c up ens160[root@ansible-node2 ~]# nmcli c m ens160 ipv4.method manual ipv4.addresses 172.25.250.65/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
[root@ansible-node2 ~]# nmcli c up ens160

关闭防火墙【三台主机均要执行】

systemctl disable --now firewalld

关闭Selinux

setenforece 0sed -i "s/SELINUX=enforcing/SELINUX=permissive/" /etc/selinux/config

挂载仓库

临时挂载
mount /dev/sr0 /mnt
永久挂载
vim /etc/fstab
....
/dev/sr0	/mnt	iso9660		defaults	0	0# 检查挂载是否出错
mount -a

安装方法:

nsible 有三种安装方式,源码安装发行版安装Python 安装

使用发行版安装Python 安装两种方式时,Ansible 的安装包有两个,区别如下:

  • ansible-core:一种极简语言和运行时包,包含一组内置模块和插件。

  • ansible:一个更大的“包含电池”的软件包,它添加了社区精选的 Ansible 集合选择,用于自动化各种设备。

在用源码或者 Python 安装 Ansible 时,默认不会安装 sshpass 软件包,该软件包用来给 Ansible 提供密码验证被控端,因此如果在执行 Ansible 的命令时需要输入 ssh 的密码,则需要该软件包,该软件包通过 dnf install -y sshpass

源码安装【它只能安装 Ansible-core 且只需要在主节点上安装即可】

[root@ansible-controller ~]# dnf install python3.12 python3.12-pip sshpass
[root@ansible-controller ~]# tar xf ansible-2.16.3.tar.gz
[root@ansible-controller ~]# cd ansible-2.16.3/
[root@ansible-controller ansible-2.16.3]# python3 -m pip install -r ./requirements.txt
[root@ansible-controller ansible-2.16.3]# python3 setup.py install

发行版安装【只需要在主节点上安装即可】

安装epel源
[root@ansible-controller ~]# dnf install https://mirrors.aliyun.com/epel/epel-release-latest-9.noarch.rpm -y[root@ansible-controller ~]# ls /etc/yum.repos.d/
base.repo  epel-cisco-openh264.repo  epel.repo  epel-testing.repo  redhat.repo
安装ansible
# 安装包含常用模块的 Ansible
[root@ansible-controller ~]# dnf install ansible -y
# 或
# 安装最简洁的 Ansible
[root@ansible-controller ~]# dnf install ansible-core -y
安装验证
[root@ansible-controller ~]# ansible --version
ansible [core 2.14.9]config file = /etc/ansible/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.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.2libyaml = True

Python安装

安装Python
# 安装 Python3 和 pip
[root@ansible-controller ~]# dnf install python3.12 python3.12-pip sshpass
安装ansible
# 安装 Ansible-core
[root@ansible-controller ~]# python3.12 -m pip install ansible-core==2.16.3# 安装 Ansible
[root@ansible-controller ~]# python3.12 -m pip install ansible
Requirement already satisfied: ansible in /usr/lib/python3.9/site-packages (7.7.0)
Requirement already satisfied: ansible-core>=2.14.7 in /usr/lib/python3.9/site-packages (from ansible) (2.14.17)
Requirement already satisfied: PyYAML>=5.1 in /usr/lib64/python3.9/site-packages (from ansible-core>=2.14.7->ansible) (5.4.1)
Requirement already satisfied: cryptography in /usr/lib64/python3.9/site-packages (from ansible-core>=2.14.7->ansible) (36.0.1)
Requirement already satisfied: packaging in /usr/lib/python3.9/site-packages (from ansible-core>=2.14.7->ansible) (20.9)
Requirement already satisfied: resolvelib<0.9.0,>=0.5.3 in /usr/lib/python3.9/site-packages (from ansible-core>=2.14.7->ansible) (0.5.4)
Requirement already satisfied: cffi>=1.12 in /usr/lib64/python3.9/site-packages (from cryptography->ansible-core>=2.14.7->ansible) (1.14.5)
Requirement already satisfied: pyparsing>=2.0.2 in /usr/lib/python3.9/site-packages (from packaging->ansible-core>=2.14.7->ansible) (2.4.7)
Requirement already satisfied: pycparser in /usr/lib/python3.9/site-packages (from cffi>=1.12->cryptography->ansible-core>=2.14.7->ansible) (2.20)
Requirement already satisfied: ply==3.11 in /usr/lib/python3.9/site-packages (from pycparser->cffi>=1.12->cryptography->ansible-core>=2.14.7->ansible) (3.11)
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
安装验证
# 查看版本
[root@ansible-controller ~]# ansible --version
ansible [core 2.14.17]config file = /etc/ansible/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.19 (main, Aug 23 2024, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-2)] (/usr/bin/python3)jinja version = 3.1.2libyaml = True
设置参数自动补全
[root@ansible-controller ~]# python3.12 -m pip install argcomplete
[root@ansible-controller ~]# activate-global-python-argcomplete --user

配置主机映射

[root@ansible-controller ~]# cat >> /etc/hosts <<EOF
192.168.72.63	ansible-controller
192.168.72.64	ansible-node1
192.168.72.65	ansible-node2
EOF
# 后面可以跟个简短名,后续使用n1和n2
[root@ansible-controller .ssh]# vim /etc/hosts
192.168.23.63   ansible-controller
192.168.23.64   ansible-node1 n1
192.168.23.65   ansible-node2 n2
主机映射执行成功之后将文件复制到另外两个被控节点
[root@ansible-controller ~]# scp /etc/hosts ansible-node1:/etc
[root@ansible-controller ~]# scp /etc/hosts ansible-node2:/etc

免密登录

控制节点是运行 Ansible 的主机,负责发送任务并收集结果。被控节点是被 Ansible 管理的主机,无需安装任何额外软件,仅需确保 SSH 服务正常运行,并具备必要的访问权限。

只需要在主控节点上创建密钥对就好

[root@ansible-controller ~]# ssh-keygen
# -t可以指定加密算法
[root@ansible-controller ~]# ssh-keygen -t rsa 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 	# 一直按回车键
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub	# 公钥位置
The key fingerprint is:
SHA256:mnbdYPHKXn9SSKSSAgpSiZ20tnkrAJcbXmKM03P6c/Q root@ansible-controller
The key's randomart image is:
+---[RSA 3072]----+
| =oo             |
|o+=o  .       .  |
|+.%.o. .  .. o   |
|.* @.   . oo. .  |
|. * . . S.o... . |
| . o o + + +  . .|
|  . + = E + o  . |
|   . + . . . .. .|
|          .   .o |
+----[SHA256]-----+

拷贝公钥

创建成功后,执行以下命令将公钥拷贝到两台被控节点
# 查看隐藏目录
[root@ansible-controller ~]# ls -a
[root@ansible-controller ~]# cd .ssh/
# .pub即为公钥,使用rsa算法
[root@ansible-controller .ssh]# ls
id_rsa  id_rsa.pub  known_hosts  known_hosts.old
# 复制本机公钥到其它被控节点
[root@ansible-controller ~]# ssh-copy-id ansible-node1
# 或
[root@ansible-controller ~]# ssh-copy-id n1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@ansible-node1's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh 'ansible-node1'"
and check to make sure that only the key(s) you wanted were added.[root@ansible-controller ~]# ssh-copy-id ansible-node2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@ansible-node2's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh 'ansible-node2'"
and check to make sure that only the key(s) you wanted were added.# 或
# 执行命令后,输入正确密码即可
[root@ansible-controller ~]# ssh-copy-id root@192.168.72.64
[root@ansible-controller ~]# ssh-copy-id root@192.168.72.65
配置好之后做测试连接
# 如果免密做成功则无需密码即可登录
[root@ansible-controller ~]# ssh root@192.168.72.64
# exit可退回原来的主机
[root@ansible-node1 ~]# exit

注意:ssh-copy-id 命令格式有两种:

  1. ssh-copy-id 远程用户@远程IP 或 仅IP

  2. ssh-copy-id -i /root/.ssh/id_rsa.pub 远程用户@远程IP 或 仅IP

  3. 如果在生成密钥时指定了密钥的名称,此处需要通过 ssh-copy-id -i 指定的名称 远程用户@远程IP 或 仅IP

快速使用

创建项目目录

[root@ansible-controller ~]# mkdir ansible_ping
[root@ansible-controller ~]# cd ansible_ping/

创建清单文件

# 名称任意,直接命名inventory也可以
[root@ansible-controller ansible_ping]# vim inventory.ini
[root@ansible-controller ansible_ping]# cat inventory.ini 
[server]
192.168.72.64
n2

执行Ansible 

# 这里server是配置文件中括号内的名称,-m指模块  概念在前面有所提及
[root@ansible-controller ansible_ping]# ansible server -i inventory.ini -m ping
ansible-node1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"
}
# 此时node2节点报错,因为使用了缩写名n2
192.168.23.65 | UNREACHABLE! => {"changed": false,"msg": "Failed to connect to the host via ssh: Host key verification failed.","unreachable": true
}
# 只需改为节点对应IP或者全称即可(ansible-node2)
[root@ansible-controller ~]# cat inventory 
[server]
192.168.23.64
192.168.23.65
[root@ansible-controller ~]# ansible server -i inventory -m ping
192.168.23.64 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"
}
192.168.23.65 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"
}

文档就写到这里,若有需要请查看附加文件,里面有具体的综合示例

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

相关文章:

  • ubuntu24.04(vmware workstation 17.6pro)无法安装vmtools的问题解决
  • mini-program01の系统认识微信小程序开发
  • 云原生详解:构建现代化应用的未来
  • 【读论文】GLM-4.1V-Thinking 解读:用强化学习解锁 VLM 的通用推理能力
  • Tensor数据转换
  • 模型训练篇 | 如何用YOLOv13训练自己的数据集(以明火烟雾检测举例)
  • 记录一种 Java 自定义快速读的方式,解决牛客中运行超时问题
  • 数与运算-埃氏筛 P1835 素数密度
  • go入门 - day1 - 环境搭建
  • Rust 中字符串类型区别解析
  • 10倍处理效率提升!阿里云大数据AI平台发布智能驾驶数据预处理解决方案
  • Tomcat:启用https(Windows)
  • AR/VR 显示画质失真?OAS百叶窗波导案例破难题
  • Spring Cloud 企业项目技术选型
  • Fiddler-关于抓取Android手机包,安装证书后页面加载失败,提示当前证书不可信存在安全风险的问题
  • 力扣-287.寻找重复数
  • Flutter基础(前端教程①-容器和控件位置)
  • 7月5号和6号复习和预习(C++)
  • 初识mysql(一)
  • 论文略读:UniPELT: A Unified Framework for Parameter-Efficient Language Model Tuning
  • 无人机报警器探测模块技术解析
  • HDLBits刷题笔记和一些拓展知识(十一)
  • 中文编程开发工具构件系列介绍——数值比较构件
  • 视频网站弹幕系统简易实现
  • Python语言+pytest框架+allure报告+log日志+yaml文件+mysql断言实现接口自动化框架
  • Android Handler机制与底层原理详解
  • RHA《Unity兼容AndroidStudio打Apk包》
  • 什么是2.5G交换机?
  • 【如何下载网页中的视频】
  • 【HarmonyOS】鸿蒙端云一体化开发入门详解 (一)