1. 程式人生 > >自動化運維工具Ansible實戰(一)簡介和部署

自動化運維工具Ansible實戰(一)簡介和部署

Ansible 自動化運維

一、Ansible的介紹

Ansible是新出現的自動化運維工具,基於Python開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點。實現了批量系統配置、批量程序部署、批量運行命令等功能。Ansible是基於模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是Ansible所運行的模塊,Ansible只是提供一種框架。

1、Ansible框架

  • 連接插件connection plugins:負責和被監控端實現通信;
  • host inventory:指定操作的主機,是一個配置文件裏面定義監控的主機;
  • 各種模塊核心模塊、command模塊、自定義模塊;
  • 借助於插件完成記錄日誌郵件等功能;
  • playbook:劇本執行多個任務時,非必需可以讓節點一次性運行多個任務。
    技術分享圖片

2、Ansible架構圖

技術分享圖片

  • Ansible核心組件說明
  • Ansible:Ansible的核心程序;
  • Host Lnventory:記錄了每一個由Ansible管理的主機信息,信息包括ssh端口,root帳號密碼,ip地址等等。可以通過file來加載,可以通過CMDB加載;
  • Playbooks:YAML格式文件,多個任務定義在一個文件中,使用時可以統一調用,“劇本”用來定義那些主機需要調用那些模塊來完成的功能;
  • Core Modules:Ansible執行任何管理任務都不是由Ansible自己完成,而是由核心模塊完成;Ansible管理主機之前,先調用core Modules中的模塊,然後指明管理Host Lnventory中的主機,就可以完成管理主機;
  • Custom Modules:自定義模塊,完成Ansible核心模塊無法完成的功能,此模塊支持任何語言編寫;
  • Connection Plugins:連接插件,Ansible和Host通信使用。

3、Ansible基本特性

  • no agents:不需要在被管控主機上安裝任何客戶端;
  • no server:無服務器端,使用時直接運行命令即可;
  • modules in any languages:基於模塊工作,可使用任意語言開發模塊;
  • yaml,not code:使用yaml語言定制劇本playbook;
  • ssh by default:基於SSH工作;
  • strong multi-tier solution:可實現多級指揮。
  • 4、Ansible的優點

  • 輕量級,無需在客戶端安裝agent,更新時,只需在操作機上進行一次更新即可;
  • 批量任務執行可以寫成腳本,而且不用分發到遠程就可以執行;
  • 使用python編寫,維護更簡單,ruby語法過於復雜;
  • 支持sudo。

5、Ansible任務執行流程

技術分享圖片
說明:以上內容大多是基於他人分享的基礎上總結而來,學習借鑒之用;

三、Ansible的安裝

1、系統環境

系統平臺: CentOS 7.3
Ansible Server: 192.168.8.55
Ansible Client: 192.168.8.66

(1)寫hosts記錄

[root@Ansible ~]# echo "192.168.8.55 Ansible" >> /etc/hosts
[root@Ansible ~]# echo "192.168.8.66 Client" >> /etc/hosts

(2)關閉firewalld和selinux

[root@Ansible ~]# systemctl stop firewalld && systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

(3)關閉selinux

[root@Ansible ~]# setenforce 0
[root@Ansible ~]# sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g‘ /etc/selinux/config

2、yum源安裝

(1)配置好epel yum源安裝相應的基礎模塊,可以使用yum直接安裝

[root@Ansible ~]# yum -y install python PyYAML python-paramiko python-jinja2 python-simplejson

(2)安裝Ansible

[root@Ansible ~]# yum -y install ansible

3、源碼包安裝(推薦使用)

Ansible安裝所依賴插件較多,所以首先安裝插件
[root@Ansible ~]# yum -y install gcc zlib zlib-devel openssl openssl-devel libffi-devel
(1)python3.6安裝
Ansible是用Python開發的,使用Ansible需要操作系統有Python,建議Python版本2.6以上。

[root@Ansible ~]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
[root@Ansible ~]# tar xvzf Python-3.6.4.tgz -C /usr/src/
[root@Ansible ~]# cd /usr/src/Python-3.6.4/
[root@Ansible Python-3.6.4]# ./configure --prefix=/usr/local
[root@Ansible Python-3.6.4]# make
[root@Ansible Python-3.6.4]# make install

將python頭文件拷貝到標準目錄,以避免編譯ansible時,找不到所需的頭文件

[root@Ansible Python-3.6.4]# cd /usr/local/include/python3.6m/
[root@Ansible python3.6m]# cp -a ./* /usr/local/include/

備份舊版本的python,並符號鏈接新版本的python

[root@Ansible python3.6m]# cd /usr/bin/
[root@Ansible bin]# mv python python.old
[root@Ansible bin]# ln -s /usr/local/bin/python3.6 /usr/local/bin/python
[root@Ansible bin]# rm -rf /usr/bin/python
[root@Ansible bin]# cp /usr/local/bin/python3.6 /usr/bin/python

修改yum腳本,使其指向舊版本的python,已避免其無法運行

[root@Ansible bin]# vim /usr/bin/yum

將#!/usr/bin/python修改為#!/usr/bin/python2.7
技術分享圖片

[root@Ansible bin]# vim /usr/libexec/urlgrabber-ext-down

將#!/usr/bin/python修改為#!/usr/bin/python2.7
技術分享圖片

[root@Ansible bin]# python   --測試安裝版本是否為Python 3.6.4
Python 3.6.4 (default, Apr 17 2018, 11:03:21) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()   --退出python

(2)setuptools模塊安裝

[root@Ansible ~]# wget https://files.pythonhosted.org/packages/72/c2/c09362ab29338413ab687b47dab03bab4a792e2bbb727a1eb5e0a88e3b86/setuptools-39.0.1.zip
[root@Ansible ~]# unzip setuptools-39.0.1.zip -d /usr/src/
[root@Ansible ~]# cd /usr/src/setuptools-39.0.1/
[root@Ansible setuptools-39.0.1]# python setup.py install

安裝好setuptools後就可以利用easy_install這個工具安裝下面的python模塊了,但我的電腦是虛擬機,配置太低了,所以基本無法安裝,所以只好一個一個下載下來再安裝了。

(3)pycrypto模塊安裝

[root@Ansible ~]# wget https://pypi.python.org/packages/source/p/pycrypto/pycrypto-2.6.1.tar.gz
[root@Ansible ~]# tar xvzf pycrypto-2.6.1.tar.gz -C /usr/src/
[root@Ansible ~]# cd /usr/src/pycrypto-2.6.1/
[root@Ansible pycrypto-2.6.1]# python setup.py install

(4) PyYAML模塊安裝

[root@Ansible ~]# wget http://pyyaml.org/download/libyaml/yaml-0.1.7.tar.gz
[root@Ansible ~]# tar xvzf yaml-0.1.7.tar.gz -C /usr/src/
[root@Ansible ~]# cd /usr/src/yaml-0.1.7/
[root@Ansible yaml-0.1.7]# ./configure --prefix=/usr/local
[root@Ansible yaml-0.1.7]# make --jobs=`grep processor /proc/cpuinfo | wc -l`
[root@Ansible yaml-0.1.7]# make install

[root@Ansible ~]# wget http://pyyaml.org/download/pyyaml/PyYAML-3.12.tar.gz
[root@Ansible ~]# tar xvzf PyYAML-3.12.tar.gz -C /usr/src/
[root@Ansible ~]# cd /usr/src/PyYAML-3.12/
[root@Ansible PyYAML-3.12]# python setup.py install

(5)Jinja2模塊安裝

[root@Ansible ~]# wget https://pypi.python.org/packages/source/M/MarkupSafe/MarkupSafe-0.9.3.tar.gz
[root@Ansible ~]# tar xvzf MarkupSafe-0.9.3.tar.gz -C /usr/src/
[root@Ansible ~]# cd /usr/src/MarkupSafe-0.9.3/
[root@Ansible MarkupSafe-0.9.3]# python setup.py install

[root@Ansible ~]# wget https://files.pythonhosted.org/packages/56/e6/332789f295cf22308386cf5bbd1f4e00ed11484299c5d7383378cf48ba47/Jinja2-2.10.tar.gz
[root@Ansible ~]# tar xvzf Jinja2-2.10.tar.gz -C /usr/src/
[root@Ansible ~]# cd /usr/src/Jinja2-2.10/
[root@Ansible Jinja2-2.10]# python setup.py install

(6)paramiko模塊安裝

[root@Ansible ~]# wget https://files.pythonhosted.org/packages/f9/e5/99ebb176e47f150ac115ffeda5fedb6a3dbb3c00c74a59fd84ddf12f5857/ecdsa-0.13.tar.gz
[root@Ansible ~]# tar xvzf ecdsa-0.13.tar.gz -C /usr/src/
[root@Ansible ~]# cd /usr/src/ecdsa-0.13/
[root@Ansible ecdsa-0.13]# python setup.py install

[root@Ansible ~]# https://files.pythonhosted.org/packages/29/65/83181630befb17cd1370a6abb9a87957947a43c2332216e5975353f61d64/paramiko-2.4.1.tar.gz
[root@Ansible ~]# tar xvzf paramiko-2.4.1.tar.gz -C /usr/src/
[root@Ansible ~]# cd /usr/src/paramiko-2.4.1/
[root@Ansible paramiko-2.4.1]# python setup.py install

(7)simplejson模塊安裝

[root@Ansible ~]# wget https://files.pythonhosted.org/packages/0d/3f/3a16847fe5c010110a8f54dd8fe7b091b4e22922def374fe1cce9c1cb7e9/simplejson-3.13.2.tar.gz
[root@Ansible ~]# tar xvff simplejson-3.13.2.tar.gz -C /usr/src/
[root@Ansible src]# cd /usr/src/simplejson-3.13.2/
[root@Ansible simplejson-3.13.2]# python setup.py install

(8) ansible安裝

[root@Ansible ~]# wget https://files.pythonhosted.org/packages/4a/3b/9d98e132074bb6a3f18fd811db2819fbde6fc8a26fad9a40b49e53cb2455/ansible-2.5.0.tar.gz
[root@Ansible ~]# tar xf ansible-2.5.0.tar.gz -C /usr/src/
[root@Ansible ~]# cd /usr/src/ansible-2.5.0/
[root@Ansible ansible-2.5.0]# python setup.py install

三、Ansible的配置

1、Ansible配置

註:centos7.0安裝Ansible後發現找不到ansible.cfg,配置文件的路徑如下圖,並將配置文件拷貝過去
技術分享圖片

[root@Ansible ansible-2.5.0]# cd examples/
[root@Ansible examples]# pwd        --註意路徑
/usr/src/ansible-2.5.0/examples
[root@Ansible examples]# ls
ansible.cfg  hosts
[root@Ansible examples]# mkdir /etc/ansible        --創建ansible目錄
[root@Ansible examples]# cp ansible.cfg hosts /etc/ansible/      --拷貝文件
[root@Ansible examples]# ls -l /etc/ansible/
總用量 24
-rw-r--r-- 1 root root 19315 4月  17 14:25 ansible.cfg
-rw-r--r-- 1 root root  1016  4月  17 14:25 hosts

2、配置ssh免密登錄(只在控制端使用)

[root@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:
bc:4e:4e:5f:61:a3:37:08:b0:c4:00:98:90:5b:c6:9f [email protected]
The key‘s randomart image is:
+--[ RSA 2048]----+
|o=...            |
|+ +  o           |
| + . .+          |
|.   E. +         |
|      . S   +    |
|         o + o   |
|        + o +    |
|       = . o .   |
|        o .      |
+-----------------+

[root@Ansible ~]# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
[root@Ansible ~]# chmod 600 /root/.ssh/authorized_keys

將公鑰傳給被管理的機器節點

[root@Ansible ~]# scp /root/.ssh/authorized_keys 192.168.8.66:/root/.ssh/
[email protected]‘s password: 
authorized_keys                      100%  401     0.4KB/s   00:00  

3、遠程連接測試

[root@Ansible ~]# vim /etc/ansible/hosts       --文件末尾添加以下內容
[web]
192.168.8.55

[root@Ansible ~]# ansible web -m command -a ‘uptime‘
192.168.8.55 | SUCCESS | rc=0 >>
14:56:03 up 12:53,  4 users,  load average: 0.00, 0.01, 0.05

至此,Ansible的安裝配置已經完成,接下來進行模塊的講解。

自動化運維工具Ansible實戰(一)簡介和部署