文章目录
Ansible
常见的自动化运维工具
目前常见的运维自动化管理工具主要是Puppet、SaltStack、Ansible等。
- Puppet是早期的自动化运维工具,是最典型的C/S结构,可以批量管理远程服务器,模块丰富,配置复杂,基于Ruby语言编写,需要安装服务端和客户端,适用于服务器管理的整个过程,比如初始安装、配置、更新等。
- SaltStack是基于python开发,也是C/S模式,需要安装服务端和客户端,加入了MQ消息同步,可以使执行命令和执行结果高效返回,但其执行过程需要等待客户端全部返回,如果客户端没有及时返回或者没有响应的话,可能会导致部分机器没有执行结果。适用于中大型公司
- Ansible也是基于python开发,Ansible只需要在一台普通的服务器上运行即可,不需要在客户端安装,因为Ansible是基于SSH远程管理,而Linux服务器大部分都离不开SSH,所以Ansible不需要为配置添加额外的支持。
Ansible简介
Ansible是一款自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1) 连接插件connection plugins:负责和被监控端实现通信;
(2) host inventory(主机清单):指定操作的主机,是一个配置文件里面定义监控的主机;
(3) 各种模块核心模块(Core Modules)、command模块、自定义模块;
(4) 借助于插件完成记录日志邮件等功能;
(5) playbooks(剧本):剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
Ansible架构图:
Ansible 方式
ansible的设计宗旨是工具易用,自动化易写易读。所以在创建自动化时我们应追求简单化。
Ansible 是一种要求状态引擎。它通过表达你所希望系统处于何种状态来解决如何自动化IT部署的问题。
Ansible 的目标是通过仅执行必要的更改,使系统处于所需的状态。试图将 Ansible 视为脚本语言并非正确的做法。
Ansible 具有多个重要的优点:
- 跨平台支持:Ansible 提供Linux、Windows、UNIX和网络设备的无代理支持,适用于物理、虚拟、云和容器环境。
- 人类可读的自动化:Ansible Playbook采用YAML文本文件编写,易于阅读,有助于确保所有人都能理解它们的用途。
- 完美描述应用:可以通过 Ansible Playbook进行每种更改,并描述和记录应用环境的每一个方面。
- 轻松管理版本控制:Ansible Playbook和项目是纯文本。它们可以视作源代码,放在现有版本控制系统中。
- 支持动态清单:可以从外部来源动态更新 Ansible 管理的计算机列表,随时获取所有受管服务器的当前正确列表,不受基础架构或位置的影响。
- 编排可与其他系统轻松集成:能够利用环境中现有的 HP SA、Puppet、Jenkins、红帽卫星和其他系统,并且集成到 Ansible 工作流中
Ansible 的安装
控制节点安装
Ansible 易于安装。 Ansible 软件只需要安装到要运行它的一个(或多个)控制节点上。由 Ansible管理的主机不需要安装 Ansible。
对控制节点的要求:
- 控制节点应是Linux或UNIX系统。不支持将Windows用作控制节点,但Windows系统可以是受管主机。
- 控制节点需要安装Python3(版本3.5或以上)。
如果操作系统是红帽8.0,Ansible 2.9可以自动使用 platform-python 软件包,该软件包支持使用Python的系统实用程序。你不需要从 AppStream安装python37或python27软件包。
红帽8安装ansible步骤:
首先前往阿里云官网下载依托源
https://developer.aliyun.com/mirror/epel?spm=a2c6h.13651102.0.0.3e221b11GJn1SM
在虚拟机里面用 yum -y install … 命令下载红帽8 的依托源 epel(RHEL 8)
然后按照阿里云依托源步骤替换相关位置
生成缓存 yum makecache
安装ansible yum -y install ansible
centos8安装ansible步骤:
[root@localhost ~]# yum list all platform-python
上次元数据过期检查:0:09:10 前,执行于 2021年07月13日 星期二 07时48分05秒。
已安装的软件包
platform-python.x86_64 3.6.8-33.el8 @anaconda
[root@localhost ~]# cd /etc/yum.repos.d
[root@localhost yum.repos.d]# ls #检查是否有epel源包
CentOS-Stream-AppStream.repo CentOS-Stream-Debuginfo.repo CentOS-Stream-HighAvailability.repo CentOS-Stream-PowerTools.repo liu.repo
CentOS-Stream-BaseOS.repo CentOS-Stream-Extras.repo CentOS-Stream-Media.repo CentOS-Stream-RealTime.repo
[root@localhost yum.repos.d]# yum -y install epel-release #下载epel源包
Repository baseos is listed more than once in the configuration
Repository appstream is listed more than once in the configuration
上次元数据过期检查:1:30:41 前,执行于 2021年07月13日 星期二 02时30分05秒。
依赖关系解决。
==================================================================================
软件包 架构 版本 仓库 大小
==================================================================================
安装:
epel-release noarch 8-11.el8 extras 24 k
安装弱的依赖:
epel-next-release noarch 8-11.el8 extras 11 k
事务概要
==================================================================================
安装 2 软件包
总下载:35 k
安装大小:38 k
下载软件包:
(1/2): epel-next-release-8-11.el8.noarch.rpm 2.1 kB/s | 11 kB 00:05
(2/2): epel-release-8-11.el8.noarch.rpm 4.4 kB/s | 24 kB 00:05
[root@localhost yum.repos.d]# ls
CentOS-Stream-AppStream.repo CentOS-Stream-Extras.repo CentOS-Stream-PowerTools.repo epel-next.repo epel.repo liu.repo
CentOS-Stream-BaseOS.repo CentOS-Stream-HighAvailability.repo CentOS-Stream-RealTime.repo epel-next-testing.repo epel-testing-modular.repo
CentOS-Stream-Debuginfo.repo CentOS-Stream-Media.repo epel-modular.repo epel-playground.repo epel-testing.repo
[root@localhost yum.repos.d]# yum makecache #验证yum源
Repository baseos is listed more than once in the configuration
Repository appstream is listed more than once in the configuration
CentOS Stream 8 - AppStream 4.4 kB/s | 4.4 kB 00:00
CentOS Stream 8 - BaseOS 543 B/s | 3.9 kB 00:07
CentOS Stream 8 - Extras 497 B/s | 3.0 kB 00:06
Extra Packages for Enterprise Linux Modular 8 - x 6.5 kB/s | 663 kB 01:42
Extra Packages for Enterprise Linux 8 - Next - x8 13 kB/s | 1.1 MB 01:28
Extra Packages for Enterprise Linux 8 - x86_64 645 kB/s | 10 MB 00:16
元数据缓存已建立。
[root@localhost yum.repos.d]# yum -y install ansible #安装ansible
[root@localhost yum.repos.d]# ansible --version #查看版本号
ansible 2.9.23
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.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Dec 3 2020, 18:11:24) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
[root@localhost yum.repos.d]# ansible -m setup localhost|grep ansible_python_version
"ansible_python_version": "3.6.8",
#通过使用setup模块验证localhost上的ansible_python_version
受管主机
Ansible的一大优点是受管主机不需要安装特殊代理。Ansible控制节点使用标准的网络协议连接受管主机,从而确保系统处于指定的状态。
受管主机可能要满足一些要求,具体取决于控制节点连接它们的方式以及它们要运行的模块。
Linux和UNIX受管主机需要安装有Python2(版本2.6或以上)或Python3(版本3.5或以上),这样才能运行大部分的模块。
对于红帽8,可以启用并安装python36应用流(或python27应用流)
yum module -y install python36
如果受管主机上启用了SELinux,还需要确保安装python3-libselinux软件包,然后才能使用与任何复制、文件或模板功能相关的模块。所以在工作的时候,应当把SELinux功能关闭。
基于windows的受管主机
Ansible有许多专门为Windows系统设计的模块。这些模块列在https://docs.ansible.com/ansible/latest/modules/list_of_windows_modules.html部分中。
大部分专门为Windows受管主机设计的模块需要在受管主机上安装PowerShell 3.0或更高版本,而不是安装Python。此外,受管主机也需要配置PowerShell远程连接。Ansible还要求至少将.NET Framework 4.0或更高版本安装在Windows受管主机上。
本课程不对Windows受管主机做过多说明,未来在工作当中需要用到时请查阅以上官方文档链接。
受管网络设备
Ansible还可以配置受管网络设备,例如路由器和交换机。Ansible包含大量专门为此目的而设计的模块。其中包括对Cisco IOS、IOS XR和NX-OS的支持;Juniper Junos;Arsta EOS;以及基于VyOS的网络设备等。
我们可以使用为服务器编写playbook时使用的相同基本技术为网络设备编写Ansible Playbook。由于大多数网络设备无法运行Python,因此Ansible在控制节点上运行网络模块,而不是在受管主机上运行。特殊连接方法也用于与网络设备通信,通常使用SSH上的CLI、SSH上的XML或HTTP(S)上的API。
本课程不对受管网络设备做过多说明,未来在工作当中需要用到时请查阅官方文档链接https://docs.ansible.com/ansible/latest/modules/list_of_network_modules.html。
构建Ansible清单
清单定义Ansible将要管理的一批主机。这些主机也可以分配到组中,以进行集中管理。组可以包含子组,主机也可以是多个组的成员。清单还可以设置应用到它所定义的主机和组的变量。
可以通过两种方式定义主机清单。静态主机清单可以通过文本文件定义。动态主机清单可以根据需要使用外部信息提供程序通过脚本或其他程序来生成。
清单作用
定义了Ansible管理的一批主机名单,通过执行Ansible模块,在调用过程中直接对清单中的主机进行批量管理
默认的清单位置
默认位置: /etc/ansible/hosts
使用规则:对于默认清单我们一般不使用,通常为了方便每一位管理者的管理,管理者会在自己的文件夹目录中设置自己的清单目录。
使用静态清单指定受管主机
静态清单文件是指定Ansible目标受管主机的文本文件。可以使用多种不同的格式编写此文件,包括INI样式或YAML。
[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# ls
ansible.cfg hosts roles
//'hosts'文件里说明了清单的写法,在配置文件ansible.cfg中重新写一个inventory = /etc/ansible/inventory,touch inventory文件,在里面写清单
[root@localhost ~]# touch inventory
[root@localhost ~]# vi inventory
在最简单的形式中。INI样式的静态清单文件是受管主机的主机名或IP地址的列表,每行一个:
192.168.220.8
test.exampel.org
bate.example.org
但通常而言,可以将受管主机组织为主机组。通过主机组,可以更加有效的对一系列系统运行Ansible。这时,以每一部分的开头为中括号括起来的主机组名称。其后为该组中每一受管主机的主机名或IP地址,每行一个。
[client]
192.168.220.8
test.exampel.org
bate.example.org
www[1:6].example.com
[dbservers]
db01.intranet.mydomain.net
db02.intranet.mydomain.net
11.10.1.50
db-[11:20]-node.example.com
验证清单
查看目录清单
列出当前目录下的清单 cat inventory
列出所有受管主机 ansible all --list-hosts
列出不属于某个组的主机 ansible ungrouped --list-hosts
列出属于client组的主机 ansible client --list-hosts
列出指定清单所有主机 ansible all -i inventory --list-hosts
列出指定清单不属于某个组的主机:ansible ungrouped -i inventory --list-hosts
列出指定清单www组中的主机:ansible www -i inventory --list-hosts
列出指定清单中group组包含www组和bbs组:ansible group-i inventory --list-hosts
注:如果清单中含有名称相同的主机和主机组,ansible 命令将显示警告并以主机作为其目标。主机组则被忽略。
列出受管主机 192.168.220.11
[root@localhost ansible]# ansible 192.168.220.11 --list-hosts
hosts (1):
192.168.220.11
列出属于client组的主机 ansible client --list-hosts
[root@localhost ansible]# ansible client --list-hosts
hosts (3):
192.168.220.8
192.168.220.9
192.168.220.10
[root@localhost ansible]#
列出所有受管主机 ansible all --list-hosts
[root@localhost ansible]# ansible all --list-hosts
hosts (5):
192.168.220.4
192.168.220.11
192.168.220.8
192.168.220.9
192.168.220.10
...
列出不属于某个组的主机 ansible ungrouped --list-hosts
[root@localhost ansible]# ansible ungrouped --list-hosts
hosts (2):
192.168.220.4
192.168.220.11
自定义清单文件
在/etc/ansible/目录中,创建一个名为inventory的自定义静态清单文件
[root@localhost ansible]# touch inventory
[root@localhost ansible]# ls
ansible.cfg hosts inventory inventory roles
[root@localhost ansible]# vi inventory
[servers]
192.168.220.8 ansible_user=root ansible_password=1 //受管主机的root用户和密码
[webservers]
192.168.220.[11:15] ansible_user=root ansible_password=passwd
Ansible连接受管主机
ssh 受管主机的IP //先ssh连一下
[root@localhost ansible]# ansible all -m ping //检查是否连通
192.168.220.8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
免密登录
先删除inventory文件里的用户名和密码再来免密登录
[root@localhost ansible]# 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:ziqdNX7oyh+miTsM1sBhbQcj6nP6g/ERDPiqGY/PEwc root@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|. ..o. |
|.oo.o.. |
|.+oo . |
|. Eo |
| + =. S |
|o.B.o oo |
|.B+=.. +=o |
|+o+o+oo=o.. |
| .oo++*+o. |
+----[SHA256]-----+
[root@localhost ansible]# ssh-copy-id root@192.168.220.8 //受管主机的ip
/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@192.168.220.8's password: //受管主机的密码
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.220.8'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ansible]# ansible all -m ping //检查是否连通
192.168.220.8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}