自动化运维之 Ansible

Ansible 概述

         由于互联网的快速发展导致产品更新换代速度逐渐加快,运维人员每天都要进行大量的维护操作,仍旧按照传统方式进行维护使得工作效率低下。这是,部署自动化运维就可以尽可能的安全、高效地完成这些工作。

           一般会把自动化运维工具划分为两类:一类是需要使用代理工具的,也就是基于专用的 Agent 程序来完成管理功能,如:Puppet 、Func 、Zabbix 等;另外一类是不需要配置代理工具的,可以完全基于 SSH 服务来完成管理功能,如:Ansible 、Fabric 等。

          Ansible 基于 Python 开发,集合了众多优秀运维工具的特点,实现了批量运行命令、部署程序、配置系统等功能。默认通过 SSH 协议进行远程命令或下发配置,无需部署任何客户端代理软件,从而使得自动化环境部署变得更加简单。可同时支持多台主机并行管理,使得管理主机更加便捷。

Ansible 核心组件

         Ansible 可以看作是一种基于模块进行工作的框架结构,批量部署能力就是由 Ansible 所运行的模块实现的,简而言之 Ansible 是基于“模块”完成各种“任务”的。基本框架结构如图所示。

1

可以看出 Ansible 基本架构由六个部分构成:

(1)Ansible core :核心引擎

(2)Host inventory 主机清单:用来定义 Ansible 所管理的主机,默认是在 Ansible 的 hosts 配置文件中定义被管理主机,同时也支持自定义动态主机清单和指定其他配置文件位置。

(3)Connection plugins 连接插件:负责和被管理主机实现通信。出支持使用 SSH 连接被管理主机外,Ansible 还被支持其他的连接方式,所以需要有连接插件将各个主机用连接插件链接到 Ansible 。

(4)Playbooks (yaml , jinja2)剧本:用来集中定义 Ansible 任务的配置文件,即将多个任务定义在一个剧本中由 Ansible 自动执行,可以由控制主机针对多台被管理主机同时运行多个任务。

(5)Core modules 核心模块:是 Ansible 自带的模块,使用这些模块将资源分发到被管理主机,使其执行特定任务或匹配特定的状态。

(6)Custom modules 自定义模式:用于完成模块功能的补充,可借助相关插件完成记录日志,发送邮件等功能。

安装部署 Ansible 服务

        Ansible 自动化运维环境由控制主机与被管理主机组成,由于 Ansible 是基于 SSH 协议进行通信的,所以控制主机安装 Ansible 软件后不需要重启或运行任何程序,被管理主机也不需要安装和运行任何代理程序。案例环境如表所示

   角色    主机名 IP地址 组名
控制主机     ansible 192.168.66.138
被管理主机  node1 192.168.66.139 webserver
被管理主机  node2 192.168.66.136 mysql

1. 安装 Ansible

         可以使用源代码进行安装,也可以使用操作系统软件包管理工具进行安装。这里使用 Centos7.2 操作系统,通过 YUM 方式安装 Ansible ,需要依赖第三方的 EPLE 源,下面配置 EPLE源作为部署 Ansible 的 YUM 。

 首先关闭防火墙和安全功能

[root@ansible ~]# systemctl stop firewalld.service
[root@ansible ~]# setenforce 0

使用 yum 安装 EPLE 源和 Ansible

[root@ansible ~]# yum install -y epel-release

[root@ansible ~]# yum install ansible –y

[root@ansible ~]# yum install tree –y                 

安装好后可以查看 Ansible 软件的版本信息

[root@ansible ~]# ansible --version
ansible 2.7.0
   config file = /etc/ansible/ansible.cfg
   configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
   ansible python module location = /usr/lib/python2.7/site-packages/ansible
   executable location = /usr/bin/ansible
   python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

Ansible 主要相关配置文件在 /etc/ansible 目录下,通过树形结构展示文件夹

[root@ansible ~]# tree /etc/ansible
/etc/ansible
├── ansible.cfg                           //ansible 的配置文件
├── hosts                                    //ansible 的主仓库,管控主机文件
└── roles                                     //角色

1 directory, 2 files

2.配置主机清单

          Ansible 通过读取默认主机清单 /etc/hosts 文件,修改主机与组配置后,可同时连接到多个被管理主机执行任务。比如定义一个 webserver 组,包含一台主机的 IP 地址,再定义一个 mysql 组,包含另一个主机的 IP 地址,内容如下:

[root@ansible ~]# vim /etc/ansible/hosts

## 192.168.1.100
## 192.168.1.110
[webserver]
192.168.66.139                   //被管理主机
[mysql]
192.168.66.136

3.设置 SSH 无密码登录

     为了避免 Ansible 下发指令时输入被管理主机的密码,可以通过证书签名到达SSH 五密码登录的效果,使用 ssh-keygen 产生一对秘钥,使用 ssh-copy-id 来下发生成的公钥。

[root@ansible ~]# ssh-keygen -t rsa                                  //基于 SSH 的秘钥的连接
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):       //回车进入
Created directory '/root/.ssh'.
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:5oGZAYFDAVlRG4ic/KTRF9gVOKJkLWXIdxWGaajmNvU [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|=*@OOoB*o        |
|o@=O &.          |
|o X.* o          |
| = o   =         |
|o . . + S        |
| +   E o .       |
|. .     .        |
|                 |
|                 |
+----[SHA256]-----+

查看生成的秘钥对

[root@ansible ~]# ls -la
总用量 48

             ………
-rw-r--r--.  1 root root 1859 6月  12 22:18 initial-setup-ks.cfg
drwx------.  3 root root   19 6月  12 22:22 .local
drwxr-----.  3 root root   19 10月 18 11:24 .pki
drwx------.  2 root root   38 10月 18 11:40 .ssh                            //生成的秘钥文件
-rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc
-rw-------.  1 root root 1562 10月 18 11:38 .viminfo

[root@ansible ~]# cd .ssh
[root@localhost .ssh]# ls
id_rsa  id_rsa.pub                       //私钥和公钥

使用 SSH密码登录被管理的主机 node1

[root@ansible .ssh]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.66.139 (192.168.66.139)' can't be established.
ECDSA key fingerprint is SHA256:KM7QwLupjrfzZ2YQdMOoGKJtIUgtz2agvwTzZOPHu2k.
ECDSA key fingerprint is MD5:f1:32:f7:7f:b7:eb:4e:9e:2e:fa:7e:8a:56:88:fe:c1.
Are you sure you want to continue connecting (yes/no)? yes
/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
[email protected]'s password:                     

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

在被管理主机上可以查看到推送的公钥文件

[root@node1 ~]# cd .ssh/
[root@localhost .ssh]# ls
authorized_keys

在被管理的主机  node2 上也是相同操作

[root@ansible .ssh]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.66.136 (192.168.66.136)' can't be established.
ECDSA key fingerprint is SHA256:HtLFtvYxQF5ER0eA1uKE8VgRx038LWpDYBbp1S1CrJ8.
ECDSA key fingerprint is MD5:23:41:18:56:8e:ed:f3:65:b1:5f:96:11:e9:11:cb:29.
Are you sure you want to continue connecting (yes/no)? yes
/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
[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

[root@node2 ~]# cd .ssh/
[root@node2 .ssh]# ls
authorized_keys

设置 SSH 无密码登录被管理主机,免交互代理

[root@ansible ~]# ssh-agent bash
[root@ansible ~]# ssh-add
Enter passphrase for /root/.ssh/id_rsa:                                //登录密码
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

最后查看被管理主机当前系统时间,此过程就不在需要密码登录,免交互配置成功

[root@ansible ~]# ansible all -a 'date'
192.168.66.136 | CHANGED | rc=0 >>
2018年 10月 18日 星期四 12:15:49 CST

192.168.66.139 | CHANGED | rc=0 >>
2018年 10月 18日 星期四 12:15:47 CST

猜你喜欢

转载自blog.51cto.com/13706703/2304870