1.介绍Ansible
什么是ansible?
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1)、连接插件connection plugins:负责和被监控端实现通信;
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
(3)、各种模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
Ansible架构图:
Ansible无需代理
Ansible 围绕无代理架构构建。通常而言,Ansible 通过 OpenSSH 或 WinRM 连接它所管理的主机并且运行任务,方法通常是将称为 Ansible 模块的小程序推送至这些主机。这些程序用于将系统置于需要的特定状态。在 Ansible 运行完其任务后,推送的所有模块都会被删除。
Ansible 不需要批准使用任何特殊代理,然后再部署到受管主机上。由于没有代理,也不需要额外的自定义安全基础架构,
Ansible 具有多个重要的优点:
- 跨平台支持:Ansible 提供Linux、Windows、UNIX和网络设备的无代理支持,适用于物理、虚拟、云和容器环境。
- 人类可读的自动化:Ansible Playbook采用YAML文本文件编写,易于阅读,有助于确保所有人都能理解它们的用途。
- 完美描述应用:可以通过 Ansible Playbook进行每种更改,并描述和记录应用环境的每一个方面。
- 轻松管理版本控制:Ansible Playbook和项目是纯文本。它们可以视作源代码,放在现有版本控制系统中。
- 支持动态清单:可以从外部来源动态更新 Ansible 管理的计算机列表,随时获取所有受管服务器的当前正确列表,不受基础架构或位置的影响。
- 编排可与其他系统轻松集成:能够利用环境中现有的 HP SA、Puppet、Jenkins、红帽卫星和其他系统,并且集成到 Ansible 工作流中。
Ansible方式
Ansible 的设计宗旨是工具易用,自动化易写易读。所以在创建自动化时我们应追求简单化。
Ansible 自动化语言围绕简单易读的声明性文本文件来构建。正确编写的 Ansible Playbook可以清楚地记录你的工作自动化。
Ansible 是一种要求状态引擎。它通过表达你所希望系统处于何种状态来解决如何自动化IT部署的问题。Ansible 的目标是通过仅执行必要的更改,使系统处于所需的状态。试图将 Ansible 视为脚本语言并非正确的做法。
2.安装Ansible
①控制节点
Ansible 易于安装。 Ansible 软件只需要安装到要运行它的一个(或多个)控制节点上。由 Ansible管理的主机不需要安装 Ansible。
对控制节点的要求:
- 控制节点应是Linux或UNIX系统。不支持将Windows用作控制节点,但Windows系统可以是受管主机。
- 控制节点需要安装Python3(版本3.5或以上)或Python2(版本2.7或以上)。
如果操作系统是红帽8.0,Ansible 2.9可以自动使用 platform-python 软件包,该软件包支持使用Python的系统实用程序。你不需要从 AppStream安装python37或python27软件包。
查看有没有安装python
[root@localhost ~]# yum list installed platform-python
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Installed Packages
platform-python.x86_64 3.6.8-1.el8.0.1
[root@localhost ~]# rpm -qa|grep platform-python
platform-python-pip-9.0.3-16.el8.noarch
platform-python-setuptools-39.2.0-5.el8.noarch
platform-python-3.6.8-23.el8.x86_64
②受管主机
Ansible的一大优点是受管主机不需要安装特殊代理。Ansible控制节点使用标准的网络协议连接受管主机,从而确保系统处于指定的状态。
受管主机可能要满足一些要求,具体取决于控制节点连接它们的方式以及它们要运行的模块。
Linux和UNIX受管主机需要安装有Python2(版本2.6或以上)或Python3(版本3.5或以上),这样才能运行大部分的模块。
对于红帽8,可以启用并安装python36应用流(或python27应用流)
yum module install python36
如果受管主机上启用了SELinux,还需要确保安装python3-libselinux软件包,然后才能使用与任何复制、文件或模板功能相关的模块。所以在工作的时候,应当把SELinux功能关闭。
控制和受管主机都需要关闭
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# vi /etc/sysconfig/selinux
[root@localhost ~]# setenforce 0
[root@localhost ~]# cat /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabl>
Active: inactive (dead)
Docs: man:firewalld(1)
Jan 04 12:42:13 localhost.localdomain systemd[1]: Starting firewalld>
Jan 04 12:42:14 localhost.localdomain systemd[1]: Started firewalld >
Jan 04 12:42:14 localhost.localdomain firewalld[1111]: WARNING: Allo>
Jan 04 14:34:02 localhost.localdomain systemd[1]: Stopping firewalld>
Jan 04 14:34:03 localhost.localdomain systemd[1]: Stopped firewalld >
3.基于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受管主机上。
4.受管网络设备
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。
5.安装Ansible
控制节点安装ansible
下载阿里云镜像
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--100 2595 100 2595 0 0 6938 0 --:--:-- --:--:-- --:--:-- 6938
[root@localhost ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo redhat.repo rhel.repo
[root@localhost yum.repos.d]# vi CentOS-Base.repo
把$releasever改为8(因为此时用的是rhel 8的镜像)
[root@localhost yum.repos.d]# rpm --reinstall https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
ed -i 's|$releasever|8|g' /etc/yum.repos.d/*warning: /var/tmp/rpm-tmp.y9Gi9g: Header V4 RSA/SHA256 Signature, key ID 2f86d6a1: NOKEY
[root@localhost yum.repos.d]# sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@localhost yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@localhost yum.repos.d]# sed -i 's|$releasever|8|g' /etc/yum.repos.d/*
重建源缓存
[root@localhost yum.repos.d]# yum makecache
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Repository AppStream is listed more than once in the configuration
CentOS-8 - Base - mirrors.aliyun.com 1.4 MB/s | 2.3 MB 00:01
CentOS-8 - Extras - mirrors.aliyun.c 20 kB/s | 8.6 kB 00:00
CentOS-8 - AppStream - mirrors.aliyu 4.4 MB/s | 6.3 MB 00:01
Extra Packages for Enterprise Linux 528 kB/s | 527 kB 00:00
Extra Packages for Enterprise Linux 1.8 MB/s | 8.7 MB 00:04
red hat BaseOS 2.7 MB/s | 2.8 kB 00:00
Metadata cache created.
安装ansible
dnf -y install ansible
查看ansible版本
Complete!
[root@localhost ~]# ansible --version
ansible 2.9.16
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 ansible的Python位置
executable location = /usr/bin/ansible ansible的执行路径
python version = 3.6.8 (default, Dec 5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]