Ansible 概述node
因爲互聯網的快速發展致使產品更新換代速度逐漸加快,運維人員天天都要進行大量的維護操做,仍舊按照傳統方式進行維護使得工做效率低下。這是,部署自動化運維就能夠儘量的安全、高效地完成這些工做。python
通常會把自動化運維工具劃分爲兩類:一類是須要使用代理工具的,也就是基於專用的 Agent 程序來完成管理功能,如:Puppet 、Func 、Zabbix 等;另一類是不須要配置代理工具的,能夠徹底基於 SSH 服務來完成管理功能,如:Ansible 、Fabric 等。mysql
Ansible 基於 Python 開發,集合了衆多優秀運維工具的特色,實現了批量運行命令、部署程序、配置系統等功能。默認經過 SSH 協議進行遠程命令或下發配置,無需部署任何客戶端代理軟件,從而使得自動化環境部署變得更加簡單。可同時支持多臺主機並行管理,使得管理主機更加便捷。web
Ansible 核心組件sql
Ansible 能夠看做是一種基於模塊進行工做的框架結構,批量部署能力就是由 Ansible 所運行的模塊實現的,簡而言之 Ansible 是基於「模塊」完成各類「任務」的。基本框架結構如圖所示。vim
能夠看出 Ansible 基本架構由六個部分構成:安全
(1)Ansible core :核心引擎bash
(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 root@localhost.localdomain
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 root@192.168.66.139
/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
root@192.168.66.139's password: //對方root 登陸密碼Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.66.139'"
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 root@192.168.66.136
/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
root@192.168.66.136's password:Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.66.136'"
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 CST192.168.66.139 | CHANGED | rc=0 >>2018年 10月 18日 星期四 12:15:47 CST