Ansible基礎安裝與配置


==========================================================================================

二、Ansible基礎安裝與配置

==========================================================================================

1Ansible基礎安裝

(1)python2.7安裝

https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz

# tar xvzf Python-2.7.8.tgz

# cd Python-2.7.8

# ./configure --prefix=/usr/local

# make --jobs=`grep processor/proc/cpuinfo | wc -l`

# make install


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

# cd /usr/local/include/python2.7

# cp -a ./* /usr/local/include/


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

# cd /usr/bin

# mv python python2.6

# ln -s /usr/local/bin/python


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

# vim /usr/bin/yum

#!/usr/bin/python  -->  #!/usr/bin/python2.6


(2)setuptools模塊安裝

https://pypi.python.org/packages/source/s/setuptools/setuptools-7.0.tar.gz

# tar xvzf setuptools-7.0.tar.gz

# cd setuptools-7.0

# python setup.py install

安裝可能會出現如下報錯:

"Compression requires the (missing) zlib module"
RuntimeError: Compression requires the (missing) zlib module

出錯原因:

提示的很清楚,缺少 zlib模塊導致安裝失敗

# yum install zlib  

# yum install zlib-devel  

下載成功後,進入python2.7的目錄,重新執行  

#make  

#make install  


此時先前執行的 軟連接仍舊生效  

然後進入 setuptool目錄,  

# python setup.py install  重新安裝 


參考鏈接:http://chengjianxiaoxue.iteye.com/blog/2095012





(3)pycrypto模塊安裝

https://pypi.python.org/packages/source/p/pycrypto/pycrypto-2.6.1.tar.gz

# tar xvzf pycrypto-2.6.1.tar.gz

# cd pycrypto-2.6.1

# python setup.py install


(4)PyYAML模塊安裝

http://pyyaml.org/download/libyaml/yaml-0.1.5.tar.gz

# tar xvzf yaml-0.1.5.tar.gz

# cd yaml-0.1.5

# ./configure --prefix=/usr/local

# make --jobs=`grep processor/proc/cpuinfo | wc -l`

# make install


https://pypi.python.org/packages/source/P/PyYAML/PyYAML-3.11.tar.gz

# tar xvzf PyYAML-3.11.tar.gz

# cd PyYAML-3.11

# python setup.py install


(5)Jinja2模塊安裝

https://pypi.python.org/packages/source/M/MarkupSafe/MarkupSafe-0.9.3.tar.gz

# tar xvzf MarkupSafe-0.9.3.tar.gz

# cd MarkupSafe-0.9.3

# python setup.py install


https://pypi.python.org/packages/source/J/Jinja2/Jinja2-2.7.3.tar.gz

# tar xvzf Jinja2-2.7.3.tar.gz

# cd Jinja2-2.7.3

# python setup.py install


(6)paramiko模塊安裝

https://pypi.python.org/packages/source/e/ecdsa/ecdsa-0.11.tar.gz

# tar xvzf ecdsa-0.11.tar.gz

# cd ecdsa-0.11

# python setup.py install


https://pypi.python.org/packages/source/p/paramiko/paramiko-1.15.1.tar.gz

# tar xvzf paramiko-1.15.1.tar.gz

# cd paramiko-1.15.1

# python setup.py install


(7)simplejson模塊安裝

https://pypi.python.org/packages/source/s/simplejson/simplejson-3.6.5.tar.gz

# tar xvzf simplejson-3.6.5.tar.gz

# cd simplejson-3.6.5

# python setup.py install


(8)ansible安裝

https://github.com/ansible/ansible/archive/v1.7.2.tar.gz

# tar xvzf ansible-1.7.2.tar.gz

# cd ansible-1.7.2

# python setup.py install




2Ansible配置

(1)SSH免密鑰登錄設置

## 生成公鑰/私鑰

# ssh-keygen -t rsa -P ''

[root@node01 ansible-1.7.2]# ssh-keygen -t rsa -P ''

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): 

Created directory '/root/.ssh'.

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:

ed:c6:c5:bf:bf:56:39:53:5c:54:99:b4:a3:e0:9b:9e [email protected]

The key's randomart p_w_picpath is:

+--[ RSA 2048]----+

|              .o*|

|               oo|

|           .   +.|

|         .... . +|

|        S ..o.  o|

|         o .o. +.|

|          +o  . +|

|         .. .  o |

|           E  ooo|

+-----------------+




## 寫入信任文件(將/root/.ssh/id_rsa.pub分發到其他服務器,並在所有服務器上執行如下指令):

# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

# chmod 600 /root/.ssh/authorized_keys

或通過如下命令設置信任

ssh-copy-id  [email protected]

ssh-copy-id  [email protected]

ssh-copy-id  [email protected]



## 主機組定義

# vim /etc/ansible/hosts 

[node_test]

192.168.99.135

192.168.99.136

192.168.99.137


(2)ansible配置

# mkdir -p /etc/ansible

cp /data/ansible-1.7.2/examples/ansible.cfg /etc/ansible/

# mv ansible.cfg ansible.cfg.bak

# cat ansible.cfg.bak |grep -v '^#'|grep -v '^$' > ansible.cfg

# vim /etc/ansible/ansible.cfg

……

remote_port = 36000

private_key_file = /root/.ssh/id_rsa

……




(3)、簡單測試

# ansible node_test  -m command -a 'uptime'

192.168.99.135 | success | rc=0 >>

 00:46:23 up  1:40,  2 users,  load average: 0.00, 0.01, 0.03


192.168.99.137 | success | rc=0 >>

 00:43:43 up  1:44,  2 users,  load average: 0.00, 0.01, 0.00


192.168.99.136 | success | rc=0 >>

 00:46:24 up  1:40,  2 users,  load average: 0.00, 0.00, 0.00





3、常用模塊使用

(1)setup

## 用來查看遠程主機的一些基本信息

# ansible node_test -m setup

# ansible node_test -m setup

192.168.99.135 | success >> {

    "ansible_facts": {

        "ansible_all_ipv4_addresses": [

            "192.168.99.135"

        ], 

        "ansible_all_ipv6_addresses": [

            "fe80::a00:27ff:febb:8c4"

        ], 

        "ansible_architecture": "x86_64", 

        "ansible_bios_date": "12/01/2006", 

        "ansible_bios_version": "VirtualBox", 

        "ansible_cmdline": {

            "KEYBOARDTYPE": "pc", 

            "KEYTABLE": "us", 

            "LANG": "en_US.UTF-8", 

            "SYSFONT": "latarcyrheb-sun16", 

            "nomodeset": true, 

            "quiet": true, 

            "rd_LVM_LV": "vg_node01/lv_swap", 

            "rd_NO_DM": true, 

            "rd_NO_LUKS": true, 

            "rd_NO_MD": true, 

            "rhgb": true, 

            "ro": true, 

            "root": "/dev/mapper/vg_node01-lv_root"

        }, 

..............以下代碼省略........................




(2)ping

## 用來測試遠程主機的運行狀態

# ansible node_test -m ping

# ansible node_test -m ping

192.168.99.137 | success >> {

    "changed": false, 

    "ping": "pong"

}


192.168.99.136 | success >> {

    "changed": false, 

    "ping": "pong"

}


192.168.99.135 | success >> {

    "changed": false, 

    "ping": "pong"

}


(3)file

## 設置文件的屬性

相關選項如下:

force:需要在兩種情況下強制創建軟鏈接,一種是源文件不存在,但之後會建立的情況下;另一種是目標軟鏈接已存在,需要先取消之前的軟鏈,然後創建新的軟鏈,有兩個選項:yes|no

group:定義文件/目錄的屬組

mode:定義文件/目錄的權限

owner:定義文件/目錄的屬主

path:必選項,定義文件/目錄的路徑

recurse:遞歸設置文件的屬性,只對目錄有效

src:被鏈接的源文件路徑,只應用於state=link的情況

dest:被鏈接到的路徑,只應用於state=link的情況

state

       directory:如果目錄不存在,就創建目錄

       file:即使文件不存在,也不會被創建

       link:創建軟鏈接

       hard:創建硬鏈接

       touch:如果文件不存在,則會創建一個新的文件,如果文件或目錄已存在,則更新其最後修改時間

       absent:刪除目錄、文件或者取消鏈接文件


示例:

## 遠程文件信息查看

# ansible node_test  -m command -a "ls -al /etc/resolv.conf"

192.168.99.137 | success | rc=0 >>

-rw-r--r--. 2 root root 24 Dec 13 21:46 /etc/resolv.conf


192.168.99.136 | success | rc=0 >>

-rw-r--r--. 2 root root 24 Dec 13 22:53 /etc/resolv.conf


192.168.99.135 | success | rc=0 >>

-rw-r--r--. 2 root root 24 Dec 13 21:46 /etc/resolv.conf



(4)copy

## 複製文件到遠程主機

相關選項如下:

backup:在覆蓋之前,將源文件備份,備份文件包含時間信息。有兩個選項:yes|no

content:用於替代“src”,可以直接設定指定文件的值

dest:必選項。要將源文件複製到的遠程主機的絕對路徑,如果源文件是一個目錄,那麼該路徑也必須是個目錄

directory_mode:遞歸設定目錄的權限,默認爲系統默認權限

force:如果目標主機包含該文件,但內容不同,如果設置爲yes,則強制覆蓋,如果爲no,則只有當目標主機的目標位置不存在該文件時,才複製。默認爲yes

others:所有的file模塊裏的選項都可以在這裏使用

src:被複制到遠程主機的本地文件,可以是絕對路徑,也可以是相對路徑。如果路徑是一個目錄,它將遞歸複製。在這種情況下,如果路徑使用“/”來結尾,則只複製目錄裏的內容,如果沒有使用“/”來結尾,則包含目錄在內的整個內容全部複製,類似於rsync


示例:

## 將本地文件“/etc/ansible/ansible.cfg”複製到遠程服務器

# ansible node_test -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"

# ansible node_test -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"

192.168.99.136 | success >> {

    "changed": true, 

    "dest": "/tmp/ansible.cfg", 

    "gid": 0, 

    "group": "root", 

    "md5sum": "911794bbcff5972ab85453b3e908ae2c", 

    "mode": "0644", 

    "owner": "root", 

    "size": 985, 

    "src": "/root/.ansible/tmp/ansible-tmp-1481648746.59-118495159801866/source", 

    "state": "file", 

    "uid": 0

}


192.168.99.137 | success >> {

    "changed": true, 

    "dest": "/tmp/ansible.cfg", 

    "gid": 0, 

    "group": "root", 

    "md5sum": "911794bbcff5972ab85453b3e908ae2c", 

    "mode": "0644", 

    "owner": "root", 

    "size": 985, 

    "src": "/root/.ansible/tmp/ansible-tmp-1481648746.66-138434971921270/source", 

    "state": "file", 

    "uid": 0

}


192.168.99.135 | success >> {

    "changed": true, 

    "dest": "/tmp/ansible.cfg", 

    "gid": 0, 

    "group": "root", 

    "md5sum": "911794bbcff5972ab85453b3e908ae2c", 

    "mode": "0644", 

    "owner": "root", 

    "size": 985, 

    "src": "/root/.ansible/tmp/ansible-tmp-1481648746.62-80555080662498/source", 

    "state": "file", 

    "uid": 0

}


驗證:

# ansible node_test  -m command -a "ls -al /tmp/ansible.cfg"

192.168.99.136 | success | rc=0 >>

-rw-r--r-- 1 root root 985 Dec 14 01:05 /tmp/ansible.cfg


192.168.99.137 | success | rc=0 >>

-rw-r--r-- 1 root root 985 Dec 14 01:03 /tmp/ansible.cfg


192.168.99.135 | success | rc=0 >>

-rw-r--r-- 1 root root 985 Dec 14 01:05 /tmp/ansible.cfg




(5)command

## 在遠程主機上執行命令

相關選項如下:

creates:一個文件名,當該文件存在,則該命令不執行

free_form:要執行的linux指令

chdir:在執行指令之前,先切換到該目錄

removes:一個文件名,當該文件不存在,則該選項不執行

executable:切換shell來執行指令,該執行路徑必須是一個絕對路徑


示例:

# ansible node_test -m command -a "uptime"

# ansible node_test -m command -a "uptime"

192.168.99.136 | success | rc=0 >>

 01:09:24 up  2:03,  2 users,  load average: 0.00, 0.00, 0.00


192.168.99.137 | success | rc=0 >>

 01:06:43 up  2:07,  2 users,  load average: 0.00, 0.00, 0.00


192.168.99.135 | success | rc=0 >>

 01:09:23 up  2:03,  2 users,  load average: 0.32, 0.07, 0.02



(6)、shell

## 切換到某個shell執行指定的指令,參數與command相同。

與command不同的是,此模塊可以支持命令管道,同時還有另一個模塊也具備此功能:raw


示例:

## 先在本地創建一個SHELL腳本

# vim /tmp/test.sh

#!/bin/sh

date +%F_%H:%M:%S


#chmod +x /tmp/rocketzhang_test.sh


## 將創建的腳本文件分發到遠程

# ansible node_test -m copy -a "src=/tmp/rocketzhang_test.sh dest=/tmp/rocketzhang_test.sh owner=root group=root mode=0755"

# ansible node_test -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh owner=root group=root mode=0755"

192.168.99.136 | success >> {

    "changed": true, 

    "dest": "/tmp/test.sh", 

    "gid": 0, 

    "group": "root", 

    "md5sum": "6097884da6d269fc48b4f8110d8ed592", 

    "mode": "0755", 

    "owner": "root", 

    "size": 28, 

    "src": "/root/.ansible/tmp/ansible-tmp-1481649175.38-136149173793514/source", 

    "state": "file", 

    "uid": 0

}


192.168.99.137 | success >> {

    "changed": true, 

    "dest": "/tmp/test.sh", 

    "gid": 0, 

    "group": "root", 

    "md5sum": "6097884da6d269fc48b4f8110d8ed592", 

    "mode": "0755", 

    "owner": "root", 

    "size": 28, 

    "src": "/root/.ansible/tmp/ansible-tmp-1481649175.37-73834225757834/source", 

    "state": "file", 

    "uid": 0

}


192.168.99.135 | success >> {

    "changed": false, 

    "dest": "/tmp/test.sh", 

    "gid": 0, 

    "group": "root", 

    "md5sum": "6097884da6d269fc48b4f8110d8ed592", 

    "mode": "0755", 

    "owner": "root", 

    "path": "/tmp/test.sh", 

    "size": 28, 

    "state": "file", 

    "uid": 0

}


## 遠程執行

# ansible node_test -m shell -a "/tmp/rocketzhang_test.sh"

# ansible node_test -m shell -a "hostname;/tmp/test.sh"

192.168.99.136 | success | rc=0 >>

node02.example.com

2016-12-14_01:14:32


192.168.99.135 | success | rc=0 >>

node01.example.com

2016-12-14_01:14:32


192.168.99.137 | success | rc=0 >>

node03.example.com

2016-12-14_01:11:51



(7)、更多模塊

其他常用模塊,比如:servicecronyumsynchronize就不一一例舉,可以結合自身的系統環境進行測試。

service:系統服務管理

cron:計劃任務管理

yumyum軟件包安裝管理

synchronize:使用rsync同步文件

user:系統用戶管理

group:系統用戶組管理


更多模塊可以參考:

#ansible-doc –l

# ansible-doc  -l

acl                  Sets and retrieves file ACL information.                    

add_host             add a host (and alternatively a group) to the ansible-playbo

airbrake_deployment  Notify airbrake about app deployments                       

alternatives         Manages alternative programs for common commands            

apache2_module       enables/disables a module of the Apache2 webserver          

apt                  Manages apt-packages                                        

apt_key              Add or remove an apt key                                    

apt_repository       Add and remove APT repositories                             

apt_rpm              apt_rpm package manager                                     

arista_interface     Manage physical Ethernet interfaces                         

arista_l2interface   Manage layer 2 interfaces                                   

arista_lag           Manage port channel (lag) interfaces                        

arista_vlan          Manage VLAN resources                                       

assemble             Assembles a configuration file from fragments               

assert               Fail with custom message                                    

at                   Schedule the execution of a command or script file via the a

authorized_key       Adds or removes an SSH authorized key                       

azure                create or terminate a virtual machine in azure              

bigip_facts          Collect facts from F5 BIG-IP devices                        

bigip_monitor_http   Manages F5 BIG-IP LTM http monitors                         

bigip_monitor_tcp    Manages F5 BIG-IP LTM tcp monitors                          

bigip_node           Manages F5 BIG-IP LTM nodes                                 

bigip_pool           Manages F5 BIG-IP LTM pools             

http://docs.ansible.com/modules_by_category.html

http://www.ansible.cn/docs/


(8)、一些概念補充

playbook的組成:playbook是由一個或多個“play”組成的列表,可以讓它們聯同起來按事先編排的機制執行;所謂task無非是調用ansible的一個module,而在模塊參數中可以使用變量;模塊執行是冪等的,這意味着多次執行是安全的,因爲其結果均一致;


執行模型:task list中的各任務按次序逐個在hosts中指定的所有主機上執行,即在所有主機上完成第一個任務後再開始第二個。在順序運行某playbook時,如果中途發生錯誤,所有已執行任務都將回滾,因此,在修改playbook後重新執行一次即可;


task組成:每個task都應該有其name,用於playbook的執行結果輸出,建議其內容儘可能清晰地描述任務執行步驟。如果未提供name,則action的結果將用於輸出;


notify指定handler的執行機制:“notify”這個action可用於在每個play的最後被觸發,在notify中列出的操作稱爲handler,僅在所有的變化發生完成後一次性地執行指定操作。


==========================================================================================

三、後續工作

==========================================================================================

1、深入學習ansible的playbook以及擴展模塊;

2、 結合業務環境,初步實現基礎監控,以取代目前調用自動化部署平臺API的方式;

3、 嘗試自動化運維工具saltstack,並將其與ansible進行對比。


學習資料:

http://www.ansible.com.cn/docs/intro_installation.html

http://blog.xiaorui.cc/category/ansible/

http://lixcto.blog.51cto.com/4834175/d-4

https://github.com/ansible/ansible-examples

http://rfyiamcool.blog.51cto.com/1030776/d-51

http://dl528888.blog.51cto.com/2382721/d-4/p-1

http://edu.51cto.com/course/course_id-2220.html

http://edu.51cto.com/course/course_id-2032.html

http://www.shencan.net/index.php/category/%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4/ansible/


原文鏈接:http://sofar.blog.51cto.com/353572/1579894/


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