自動化運維之 安裝部署 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地址組名
控制主機    ansible192.168.66.138
被管理主機 node1192.168.66.139webserver
被管理主機 node2192.168.66.136mysql

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:                     //對方root 登錄密碼                

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章