1. 程式人生 > >ansible批量管理服務工具。

ansible批量管理服務工具。

ansible批量管理服務工具

ansible批量管理服務工具

 

 

批量管理伺服器的工具

 
  1. 無須部署agent,通過ssh進行管理
  2. 流行的自動化運維工具:https://github.con/ansible/ansible
 

三種批量管理工具

 
  1. ansible (so easy) 500以下伺服器
  2. saltstack (比較複雜) 10004萬伺服器
  3. puppet (超級複雜) 只有很老企業在用
 

jkenkins簡介

 
  1. 視覺化運維(主要用在視覺化部署)
  2. 持續構建,可以和git,svn結合(存放開發程式碼的倉庫)
  3. 可結合ssh實現視覺化運維
  4. 可結合ansible實現視覺化運維
 

Ansible伺服器簡單的綜合安全管理策略

 
  1. #禁止非root使用者檢視Ansible管理伺服器端/etc/hosts檔案
  2. [[email protected] ~]# ll /etc/hosts
  3. -rw-r--r--. 1 root root 180 9 9 00:38 /etc/hosts
  4. [[email protected] ~]# chmod 600 /etc/hosts
  5. #禁止非root使用者檢視Ansible的主機清單配置檔案
  6. [[email protected] ~]# ll /etc/ansible/hosts
  7. -rw-r--r-- 1 root root 87 9 9 21:59 /etc/ansible/hosts
  8. [[email protected] ~]# chmod 600 /etc/ansible/hosts
 

ansible檢視幫助

 
  1. /usr/local/python/bin/ansible-doc -l(檢視總幫助)
  2. /usr/local/python/bin/ansible-doc -s shell(檢視shell模組的幫助)
  3. /usr/local/python/bin/ansible-doc -s raw
 

安裝ansible流程

 

如果Centos7版本

需要安裝yum -y install net-toolsvim

關閉防火牆:systemctl stop firewalld 關閉防火牆開機啟動:systemctl disable fierwalld

關閉selinux

 

7.5yum安裝ansible

7.5yum倉庫全可以用,本地的需要自己手動開啟

yum -y install epel-release

yum -y install ansible (自動安裝sshpass軟體包)

 

安裝支援包

yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses-devel unzip zlib-devel zlib openssl-devel openssl libffi-devel

image_1cpuccfs01qshm4113hs1p4etkj9.png-56.5kB

 

下載python原始碼包

連結:https://pan.baidu.com/s/1nE0v2fN6dq4FHfhSTbBnsg 
提取碼:ud04

需要雲yum

wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

image_1cpuciu9n1051r9q1rie7fu165jm.png-60.8kB

 

原始碼編譯Python3.5

tar xf Python-3.5.2.tgz -C /usr/src

cd /usr/src/Python-3.5.2

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

make && make install

ln -s /usr/local/python/bin/python3 /usr/bin/python3(製作軟連結)

which python3(檢視命令是否存在)

python3 -V(查詢python版本)

image_1cpucputdi7e1kfrdio18sl5np20.png-11.5kB

 

靜心等待ansible安裝完畢後

ln -s /usr/local/python/bin/ansible /usr/local/bin(製作軟連結)

which ansible(檢視命令是否存在)

ansible --version(檢視ansible版本)

image_1cpuclvd7174d9k45ho1pl8m5q13.png-34.5kB

 

ansible的簡單配置

通過pip安裝的ansible是沒有配置檔案的

mkdir -p /etc/ansible(預設沒有,需要手動建立)

vim /etc/ansible/hosts(預設沒有,需要手動建立)

image_1cpuej1g8gvsanj1u8tilk15f02d.png-16.8kB

 

ansible命令使用格式

ansible -i 主機或主機組 -m 指定模組 -a 命令 (-i指定配置檔案,不寫就預設路徑下/etc/ansible/hosts,-m指定模組,-a釋出命令)

 

command模組釋出命令使用格式

(對方需要有python包,釋出命令)

ansible nginx -m command -a 'hostname -I' 
(分發模組內容格式,nginx是模組名,-a是條件,-m command是呼叫ansible裡面的模組釋出命令用)

ansible client2 -m command -a 'hostname -I' 
(分發單個主機格式,client2是主機名,-a是條件,-m command是呼叫ansible裡面的模組釋出命令用)

ansible client1 -m command -a 'hostname -I' 
(分發單個主機格式,client1是主機名,-a是條件,-m command是呼叫ansible裡面的模組釋出命令用)

ansible client1:client2 -m command -a 'hostname -I' 
(分發多個主機格式,client1:client2是主機名,-a是條件,-m command是呼叫ansible裡面的模組釋出命令用)

ansible all -m command -a 'hostname -I' 
(all是分發所有主機格式,-a是條件,-m command是呼叫ansible裡面的模組釋出命令用)

image_1cpuhnnvu7uk1q4c10qmuarsdadh.png-8.9kB 
image_1cpuhnafp1cmsi2t1dpb12j1t40d4.png-8.5kB 
image_1cpugtmg3rjg12kt1oi5111m1kbj23.png-12.8kB
image_1cpughcpg1r101ffd1ambnv71l5c19.png-62.3kB

 

ping模組釋出命令使用格式

(對方需要有python包)

ping模組檢查伺服器是否連線正常,ping模組不需要-a指定引數

ansible all -m ping (ansible的ping模組格式)

image_1cpuhde631ru5ducsv5tcd765g.png-16.3kB

 

shell模組釋出命令使用格式

(對方需要有python包)

shell模組支援管道符格式 
ansible all -m shell -a 'echo test | grep t'

shell模組支援重定向格式 
ansible all -m shell -a "echo bb >> /tmp/testansible"

shell模組支援awk格式 
ansible all -m shell -a "cat /etc/passwd | awk -F":" '{print $1}'" (如果遇到特殊符號需要加入\轉義)

image_1cpuhhpa8116vmed1i539pc50r5t.png-46.8kB
image_1cpuhkcjddac8tunid1oqo81f6a.png-27.7kB
image_1cpuhlo491o4j6e2mp5jnr1saqc7.png-20.8kB

 

raw模組使用格式僅通過ssh實現

(不依賴python包)

image_1cpuhtka7tir1dthe68582r2g8.png-24.1kB 
image_1cpuhtsprnfcmc51rns1tql35jgl.png-39.2kB 
image_1cpuhuavg1e3712a01b1qi8sgbai2.png-37kB

 

copy模組注意事項

yum -y install libselinux-python(傳送失敗的話說明對方沒有這個支援包)

 

copy模組拷貝檔案目錄使用格式

ansible all -m copy -a 'src=/root/xin dest=/tmp' 
(src原始檔,dest目標位置,對方沒有目錄模組自動建立)

image_1cpuitgi11bmn36h1lv7e9m154pif.png-85.8kB
image_1cpuitpn1hu11bdr1n0m2td13n2is.png-7.1kB 
image_1cpuitvbopga1huj1s99ko5j3vj9.png-7.3kB

ansible all -m copy -a 'src=/root/xin dest=/tmp backup=yes' 
(src原始檔,dest目標位置,backup=yes覆蓋同時是否備份原始檔)

image_1cpuj2a891d9am9g193mjdbjlpjm.png-89kB
image_1cpuj857v6gut3d8jjnrie3mk3.png-10kB 
image_1cpuj8dvv13ho1qggo3d1gt8op5kg.png-11.2kB

ansible all -m copy -a 'src=/root/xin dest=/tmp owner=nobody group=nobody mode=0600' 
(owner=屬主是誰,group=屬組是誰,mode=它許可權)

image_1cpujil351hviphgft21fto27vkt.png-86.2kB
image_1cpujjumbu2414e81erjqvj1vsla.png-12.8kB 
image_1cpujke903mrl101qel58o1o54ln.png-11.7kB

 

script模組批量執行指令碼使用格式

ansible all -m script -a "/service/scripts/auto_nginx.sh"

 
  1. #操作示例-->遠端批量分發並自動部署nginx
  2. #所有被管理端需要掛載光碟,並建立本地yum配置檔案
  3. [[email protected] scripts]# pwd
  4. /service/scripts
  5. [[email protected] scripts]# ls | xargs -n1
  6. auto_nginx.sh #自動安裝nginx指令碼
  7. fenfa.sh #批量分發指令碼
  8. nginx-1.10.2.tar.gz #nginx原始碼包
  9. [[email protected] scripts]# cat auto_nginx.sh #nginx安裝指令碼
  10. #!/bin/sh
  11. #nginx install shell scripts
  12. test -d /media/cdrom || mkdir -p /media/cdrom
  13. mount /dev/sr0 /media/cdrom &>/dev/null
  14. yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel &>/dev/null
  15. test -d /service/scripts || exit 3
  16. cd /service/scripts/
  17. tar xf nginx-1.10.2.tar.gz -C /usr/src/
  18. cd /usr/src/nginx-1.10.2/
  19. ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module &>/dev/null
  20. make &>/dev/null
  21. make install &>/dev/null
  22. exit 0
  23. [[email protected] scripts]# cat fenfa.sh #原始碼包和安裝指令碼的批量分發指令碼
  24. #!/bin/sh
  25. #批量分發指令碼
  26. Group=$1
  27. ansible $Group -m copy -a "src=/service/scripts/ dest=/service/scripts/"
  28. ansible $Group -m script -a "/service/scripts/auto_nginx.sh"
  29. [[email protected] scripts]# sh fenfa.sh all #啟用指令碼
 

cron定時任務模組

Ansible中的cron模組用於定義任務計劃。主要包括兩種狀態(state)

image_1cq03is0tutncsk2gful3hme9.png-43.2kB

 
  1. #新增定時任務計劃,在所有被管理的主機裡每十分鐘輸出hello字串,定時任務描述為test cron job
  2. [[email protected] ~]# ansible all -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job"'
  3. Web02 | SUCCESS => {
  4. "changed": true,
  5. "envs": [],
  6. "jobs": [
  7. "test cron job"
  8. ]
  9. }
  10. Web01 | SUCCESS => {
  11. "changed": true,
  12. "envs": [],
  13. "jobs": [
  14. "test cron job"
  15. ]
  16. }
  17. [[email protected] ~]# ansible all -m shell -a 'crontab -l'
  18. Web01 | SUCCESS | rc=0 >>
  19. #Ansible: test cron job
  20. */10 * * * * /bin/echo hello
  21. Web02 | SUCCESS | rc=0 >>
  22. #Ansible: test cron job
  23. */10 * * * * /bin/echo hello
  24. #刪除描述為test cron job的定時任務
  25. [[email protected] ~]# ansible all -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state=absent'
  26. Web02 | SUCCESS => {
  27. "changed": true,
  28. "envs": [],
  29. "jobs": []
  30. }
  31. Web01 | SUCCESS => {
  32. "changed": true,
  33. "envs": [],
  34. "jobs": []
  35. }
  36. [[email protected] ~]# ansible all -m shell -a 'crontab -l'
  37. Web02 | SUCCESS | rc=0 >>
  38. Web01 | SUCCESS | rc=0 >>
  39. #給Web01伺服器上的普通使用者yunjisuan新增一個定時任務
  40. [[email protected] ~]# ansible Web01 -m shell -a 'id yunjisuan'
  41. Web01 | SUCCESS | rc=0 >>
  42. uid=1000(yunjisuan) gid=1000(yunjisuan) 組=1000(yunjisuan)
  43. [[email protected] ~]# ansible Web01 -m cron -a 'minute="*/10" job="/bin/echo hello" name="yunjisuan cron job" user="yunjisuan"'
  44. Web01 | SUCCESS => {
  45. "changed": true,
  46. "envs": [],
  47. "jobs": [
  48. "yunjisuan cron job"
  49. ]
  50. }
  51. [[email protected] ~]# ansible Web01 -m shell -a 'crontab -u yunjisuan -l'
  52. Web01 | SUCCESS | rc=0 >>
  53. #Ansible: yunjisuan cron job
  54. */10 * * * * /bin/echo hello
  55. [[email protected] ~]# ansible Web01 -m cron -a 'minute="*/10" job="/bin/echo hello" name="yunjisuan cron job" user="yunjisuan" state="absent"'
  56. Web01 | SUCCESS => {
  57. "changed": true,
  58. "envs": [],
  59. "jobs": []
  60. }
  61. [[email protected] ~]# ansible Web01 -m shell -a 'crontab -u yunjisuan -l'
  62. Web01 | SUCCESS | rc=0 >>
 

yum模組批量安裝軟體包

利用yum模組安裝軟體包,雖然能被shell模組替代,但是用yum模組更顯專業一些

image_1cq03lsq3ttmiin8lvf461bp3m.png-34.1kB

 

user模組批量建立使用者

使用者管理模組。管理使用者賬號

image_1cq03nl19108b1a0811ng1iaqhea23.png-64.3kB

 
  1. #在Web02上建立一個普通使用者yunjisuan,並設定使用者的密碼為123123
  2. [[email protected] ~]# ansible Web02 -m user -a 'name=yunjisuan comment="welcom to yunjisuan" uid=1066 groups=wheel password=123123 shell=/bin/bash home=/home/yunjisuan'
  3. Web02 | SUCCESS => {
  4. "changed": true,
  5. "comment": "welcom to yunjisuan",
  6. "create_home": true,
  7. "group": 1066,
  8. "groups": "wheel",
  9. "home": "/home/yunjisuan",
  10. "name": "yunjisuan",
  11. "password": "NOT_LOGGING_PASSWORD",
  12. "shell": "/bin/bash",
  13. "state": "present",
  14. "system": false,
  15. "uid": 1066
  16. }
  17. [[email protected] ~]# ansible Web02 -m shell -a 'tail -1 /etc/passwd'
  18. Web02 | SUCCESS | rc=0 >>
  19. yunjisuan:x:1066:1066:welcom to yunjisuan:/home/yunjisuan:/bin/bash
  20. [[email protected] ~]# ansible Web02 -m shell -a 'tail -1 /etc/shadow'
  21. Web02 | SUCCESS | rc=0 >>
  22. yunjisuan:123123:17783:0:99999:7::: #密碼居然是明文!!!

利用ansible的user模組狀態使用者時要注意在password引數的後邊新增密文,否則不能登陸使用者 
通過Python的pip程式安裝passlib即可為密碼加密

 
  1. #安裝Python2的pip工具,並通過pip工具安裝Python的加密模組來給密碼加密
  2. [[email protected] ~]# yum -y install epel-release
  3. [[email protected] ~]# yum -y install python2-pip
  4. [[email protected] ~]# pip install passlib
  5. #生成密文密碼
  6. [[email protected] ~]# python -c "from passlib.hash import sha512_crypt;import getpass;print sha512_crypt.encrypt(getpass.getpass())"
  7. Password: #輸入你想要加密的密碼
  8. $6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1 #加密後的密碼
  9. #刪除之前建立的yunjisuan使用者,並刪除它的家目錄
  10. [[email protected] ~]# ansible Web02 -m user -a 'name=yunjisuan state=absent remove=true'
  11. Web02 | SUCCESS => {
  12. "changed": true,
  13. "force": false,
  14. "name": "yunjisuan",
  15. "remove": true,
  16. "state": "absent"
  17. }
  18. #繼續在Web02上建立yunjisuan使用者
  19. [[email protected] ~]# ansible Web02 -m user -a 'name=yunjisuan comment="welcom to yunjisuan" uid=1066 groups=wheel password=$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1 shell=/bin/bash' home=/home/yunjisuan'
  20. [[email protected] ~]# ansible Web02 -m shell -a 'tail -1 /etc/shadow'
  21. Web02 | SUCCESS | rc=0 >>
  22. yunjisuan:$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1:17783:0:99999:7::: #終於密文了
 

setup模組批量檢視服務的所有屬性

Ansible中使用setup模組收集,檢視被管理主機的facts(facts是Ansible採集被管理主機裝置資訊的一個功能)。每個被管理主機在接收並執行管理命令之前,都會將自己的相關資訊(作業系統版本,IP地址等)傳送給控制主機

 
  1. #檢視遠端主機的facts資訊
  2. [[email protected] ~]# ansible Web01 -m setup | head
  3. Web01 | SUCCESS => {
  4. "ansible_facts": {
  5. "ansible_all_ipv4_addresses": [
  6. "192.168.200.184"
  7. ],
  8. "ansible_all_ipv6_addresses": [
  9. "fe80::20c:29ff:fe77:16ad"
  10. ],
  11. "ansible_apparmor": {
  12. "status": "disabled"
 

ansible-playbook的初步使用

playbook可以把ansible的模組進行組合

 
  1. ln -s /usr/local/python/bin/ansible-playbook /usr/local/bin (優先製作軟連結)
  2. shell模組支援很多模式,copy模組分發檔案或目錄,register模組輸出命令執行結果,nginx_conf配置下發並檢測,vars自定義變數,setupvars內建變數,filevars可變配置檔案
  3. vim test_shell[copy,register,nginx_conf,vars,setupvars,filevars].yaml (執行模板)
  4. ansible-playbook test_shell[copy,register,nginx_conf,vars,setupvars,filevars].yaml (執行配置檔案)
  5. 我們可以使用ansible all -m setup | less (檢視ansible內建變數)
  6. vim if.j2 (下發配置檔案模板樣式)
 

下發配置檔案裡面使用判斷語法

 
  1. vim /tmp/if.j2
  2. {% if PORT %} #if PORT存在
  3. ip=0.0.0.0:{{ PORT }}
  4. {% else %} #否則的話
  5. ip=0.0.