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. [root@ansible ~]# ll /etc/hosts
  3. -rw-r--r--. 1 root root 180 9 9 00:38 /etc/hosts
  4. [root@ansible ~]# chmod 600 /etc/hosts
  5. #禁止非root用戶查看Ansible的主機清單配置文件
  6. [root@ansible ~]# ll /etc/ansible/hosts
  7. -rw-r--r-- 1 root root 87 9 9 21:59 /etc/ansible/hosts
  8. [root@ansible ~]# 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. [root@ansible scripts]# pwd
  4. /service/scripts
  5. [root@ansible scripts]# ls | xargs -n1
  6. auto_nginx.sh #自動安裝nginx腳本
  7. fenfa.sh #批量分發腳本
  8. nginx-1.10.2.tar.gz #nginx源碼包
  9. [root@ansible 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. [root@ansible 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. [root@ansible scripts]# sh fenfa.sh all #激活腳本
 

cron定時任務模塊

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

image_1cq03is0tutncsk2gful3hme9.png-43.2kB

 
  1. #添加定時任務計划,在所有被管理的主機里每十分鍾輸出hello字符串,定時任務描述為test cron job
  2. [root@ansible ~]# 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. [root@ansible ~]# 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. [root@ansible ~]# 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. [root@ansible ~]# ansible all -m shell -a 'crontab -l'
  37. Web02 | SUCCESS | rc=0 >>
  38. Web01 | SUCCESS | rc=0 >>
  39. #給Web01服務器上的普通用戶yunjisuan添加一個定時任務
  40. [root@ansible ~]# ansible Web01 -m shell -a 'id yunjisuan'
  41. Web01 | SUCCESS | rc=0 >>
  42. uid=1000(yunjisuan) gid=1000(yunjisuan) 組=1000(yunjisuan)
  43. [root@ansible ~]# 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. [root@ansible ~]# 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. [root@ansible ~]# 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. [root@ansible ~]# 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. [root@ansible ~]# 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. [root@ansible ~]# 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. [root@ansible ~]# 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. [root@ansible ~]# yum -y install epel-release
  3. [root@ansible ~]# yum -y install python2-pip
  4. [root@ansible ~]# pip install passlib
  5. #生成密文密碼
  6. [root@ansible ~]# 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. [root@ansible ~]# 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. [root@ansible ~]# 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. [root@ansible ~]# 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. [root@ansible ~]# 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.0.0:80
  6. {% endif %} #結尾
  7. vim test_ifvars.yaml
  8. ---
  9. - hosts: all
  10. gather_facts: True #開啟系統內置變量
  11. vars:
  12. - PORT: 90 #自定義變量
  13. tasks:
  14. - name: jinja2 if test
  15. template: src=/tmp/if.j2 dest=/root/test
  16. ansible-playbook test_ifvars.yaml (下達分發命令)
  17. 如果我們將變量PORT值為空的話,就會進入else否則的80端口
  18. ---
  19. - hosts: all
  20. gather_facts: True
  21. vars:
  22. - PORT: #置空
  23. tasks:
  24. - name: jinja2 if test
  25. template: src=/tmp/if.j2 dest=/root/test
 

Playbook下發可變配置文件

 
  1. #利用template模塊下發可變的配置文件
  2. ---
  3. - hosts: all
  4. gather_facts: True #開啟系統變量
  5. vars:
  6. - myname: "yunjisuan" #自定義變量
  7. tasks:
  8. - name: template test
  9. template: src=/tmp/test dest=/root/test #使用template下發可變配置文件

配置文件如果使用copy模塊去下發的話,那配置都是一樣的; 
如果下發的配置文件里有可變的配置,需要用到template模塊。

 

playbook使用register輸出命令運行結果

 
  1. ---
  2. - hosts: all
  3. tasks:
  4. - name: test register
  5. shell: echo "welcome to yunjisuan"
  6. register: print_result #將之前命令的輸出結果保存在變量print_result里
  7. - debug: var=print_result #將變量的值作為debug輸出出來。
  8. 我們在用playbook進行ansible模塊操作的時候,並沒有命令的執行結果輸出,默認被隱藏了
  9. 我們可以通過register模塊追加輸出命令的執行結果
 

playbook的自定義變量和內置變量

我們可以使用ansible all -m setup | less查看ansible內置變量

 
  1. ---
  2. - hosts: all
  3. vars: #定義變量
  4. - name: "yunjisuan" #第一個name變量
  5. age: "3" #第二個age變量
  6. tasks:
  7. - name: "{{ name }}" #{{}}兩對大括號引用變量,變量名兩頭空格
  8. shell: echo "myname {{ name }},myage {{ age }}"
  9. register: var_result
  10. - debug: var=var_result
 
  1. ---
  2. - hosts: all
  3. gather_facts: True
  4. tasks:
  5. - name: setup var
  6. shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}" >> /tmp/test
  7. - name: setup var2
  8. shell: echo "time {{ ansible_date_time["date"] }}" >> /tmp/test
  9. register: var_result
  10. - debug: var=var_result
  11. 特別提示:
  12. 引用變量需要在雙引號中引用。
  13. 在使用自定義變量時,我們要特別注意不要和系統的內置保留變量同名,容易引發問題。
  14. Found variable using reserved name: name #name是一個保留的內置變量,我們在自定義時不能用,會有警告
 
  1. ---
  2. - hosts: all
  3. gather_facts: True #使用ansible內置變量
  4. tasks:
  5. - name: setup var
  6. shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}" >> /tmp/test
  7. - name: setup var2
  8. shell: echo "time {{ ansible_date_time["date"] }}" >> /tmp/test
  9. register: var_result
  10. - debug: var=var_result
  11. #python取變量方法
  12. a = [1,3,5] --> a[0]=1 #這樣取值就不會帶[]
  13. a = {"sl":123123,"sl1"123321} --> a[sl]=123123 #這樣取值就不會帶[]
 

playbook的簡單shell模塊的使用

 
  1. --- #開頭三個小-開頭
  2. - hosts: webB
  3. tasks:
  4. - name: test
  5. shell: echo "welcome to yunjisaun" >> /tmp/username
  6. - name: test2
  7. shell: echo "welcome to yunjisuan" >> /tmp/username
  8. 模板說明:
  9. --- #開頭必須有三個小-,頂格寫
  10. - hosts #正文配置代碼的第一級,必須有兩個空格(-占一個空格位)
  11. - host: webB #webB是host參數的值,值和hosts:之間要有一個空格
  12. tasks: #tasks:表示接下來要執行的具體任務
  13. - name: #相對於tasks再多縮進兩個格(-占一個空格位),表示屬於tasks的下一級
  14. - name: test #test只是要執行的具體命令的名字可以隨便寫。name:后還是有一個空格要注意
  15. shell: #表示調用shell模塊執行命令相對於tasks仍舊要多縮進兩個空格
  16. shell: echo "xxx" >> xxx #shell:后邊還是要有個空格,需要注意。
 

playbook的簡單copy模塊的使用

 
  1. ---
  2. - hosts: all
  3. tasks:
  4. - name: test copy
  5. copy: src=/tmp/copy_test dest=/tmp/
  6. 模板說明:
  7. --- #開頭必須有三個小-,頂格寫
  8. - hosts #正文配置代碼的第一級,必須有兩個空格(-占一個空格位)
  9. tasks: #tasks:表示接下來要執行的具體任務
  10. - name: test #test只是要執行的具體命令的名字可以隨便寫。name:后還是有一個空格要注意
  11. copy: src=/tmp/copy_test dest=/tmp/ #把源文件拷貝到對方的位置
 

Playbook的notify通知和下發nginx配置

 
  1. vim nginx.j2
  2. worker_processes {{ ansible_processor_count }}; #可變的參數
  3. #實戰下發可執行動作的可變的nginx配置文件
  4. ---------------------------------------------------------------------------
  5. vim test_nginxvars.yaml
  6. ---
  7. - hosts: all
  8. gather_facts: True #開啟系統內置變量
  9. tasks:
  10. - name: nginx conf
  11. template: src=/tmp/nginx.j2 dest=/usr/local/nginx/conf/nginx.conf
  12. notify:
  13. - reload nginx #下發通知給handlers模塊執行名字叫做reload nginx的動作
  14. handlers: #定義動作
  15. - name: reload nginx #動作的名字
  16. shell: /usr/local/nginx/sbin/nginx -s reload
  17. ansible-playbook test_nginxvars.yaml (執行分發命令)
 

nginx配置下發並檢測

 
  1. ---
  2. - hosts: all
  3. tasks:
  4. - name: copy nginx.conf
  5. copy: src=/tmp/nginx.conf dest=/usr/local/nginx/conf/ backup=yes
  6. - name:
  7. shell: /usr/local/nginx/sbin/nginx -t
  8. register: nginx_result
  9. - debug: var=nginx_result
 

ansible的script模塊批量運行腳本

 
  1. #操作示例-->遠程批量分發並自動部署nginx
  2. #所有被管理端需要掛載光盤,並創建本地yum配置文件
  3. auto_nginx.sh #自動安裝nginx腳本
  4. fenfa.sh #批量分發腳本
  5. nginx-1.10.2.tar.gz #nginx源碼包
  6. #!/bin/sh
  7. #nginx install shell scripts
  8. test -d /media/cdrom || mkdir -p /media/cdrom
  9. mount /dev/sr0 /media/cdrom &>/dev/null
  10. yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel &>/dev/null
  11. test -d /service/scripts || exit 3
  12. cd /service/scripts/
  13. tar xf nginx-1.10.2.tar.gz -C /usr/src/
  14. cd /usr/src/nginx-1.10.2/
  15. ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module &>/dev/null
  16. make &>/dev/null
  17. make install &>/dev/null
  18. exit 0
  19. #源碼包和安裝腳本的批量分發腳本
  20. #!/bin/sh
  21. #批量分發腳本
  22. Group=$1
  23. ansible $Group -m copy -a "src=/service/scripts/ dest=/service/scripts/"
  24. ansible $Group -m script -a "/service/scripts/auto_nginx.sh"
 

使用roles標准化Playbook

roles功能可以用來規范playbook的編寫

 

創建所需要的roles原型目錄結構

 
  1. #創建roles基本原型的目錄結構
  2. [root@ansible myroles]# tree /myroles/
  3. /myroles/
  4. ├── nginx.yaml #入口觸發配置文件
  5. └── roles #playbook的原型配置目錄
  6. └── nginx #nginx相關模組配置目錄
  7. ├── files #copy模塊和script模塊的參數src默認會從這個文件夾查找
  8. ├── handlers #用來存放notify的
  9. ├── tasks #用來存放ansible模塊任務的
  10. ├── templates #用來存放j2的
  11. └── vars #用來存放變量的
  12. 7 directories, 1 file
  13. #入口觸發配置文件
  14. [root@ansible myroles]# cat /myroles/nginx.yaml
  15. ---
  16. - hosts: all #執行的主機范圍
  17. gather_facts: True #開啟系統內置變量
  18. roles: #啟用roles原型配置
  19. - nginx #執行nginx原型模組
 

roles中tasks任務編排模組的使用

 
  1. #在nginx模組添加tasks任務配置文件
  2. [root@ansible myroles]# cat roles/nginx/tasks/main.yaml
  3. ---
  4. - name: check alived #任務1的名字
  5. ping: #執行ping模塊
  6. - name: #任務2的名字
  7. shell: ls / #執行shell模塊
  8. register: ls_result #將執行結果保存給變量
  9. - debug: var=ls_result #變量的值賦值給debug進行輸出
  10. #完成后的目錄結構如下所示
  11. [root@ansible myroles]# tree /myroles/
  12. /myroles/
  13. ├── nginx.yaml #nginx模組入口配置文件
  14. └── roles
  15. └── nginx #nginx原型模組目錄
  16. ├── files
  17. ├── handlers
  18. ├── tasks
  19. └── main.yaml #nginx模組的tasks任務配置文件
  20. ├── templates
  21. └── vars
  22. 7 directories, 2 files
 

執行簡單的roles任務模型

 
  1. #執行nginx入口配置文件
  2. [root@ansible myroles]# ansible-playbook nginx.yaml
  3. PLAY [all] ****************************************************************************************************
  4. TASK [Gathering Facts] ****************************************************************************************
  5. ok: [webA]
  6. ok: [webB]
  7. TASK [nginx : check alived] ***********************************************************************************
  8. ok: [webA]
  9. ok: [webB]
  10. TASK [nginx : shell] ******************************************************************************************
  11. changed: [webA]
  12. changed: [webB]
  13. TASK [nginx : debug] ******************************************************************************************
  14. ok: [webA] => {
  15. "ls_result": {
  16. "changed": true,
  17. "cmd": "ls /",
  18. "delta": "0:00:00.002805",
  19. "end": "2018-06-21 11:52:29.343592",
  20. "failed": false,
  21. "rc": 0,
  22. "start": "2018-06-21 11:52:29.340787",
  23. "stderr": "",
  24. "stderr_lines": [],
  25. "stdout": "bin\nboot\ndev\netc\nhome\nlib\nlib64\nmedia\nmnt\nopt\nproc\nroo\nroot\nrun\nsbin\nservice\nsrv\nsys\ntmp\nusr\nvar",
  26. "stdout_lines": [
  27. "bin",
  28. "boot",
  29. "dev",
  30. "etc",
  31. "home",
  32. "lib",
  33. "lib64",
  34. "media",
  35. "mnt",
  36. "opt",
  37. "proc",
  38. "roo",
  39. "root",
  40. "run",
  41. "sbin",
  42. "service",
  43. "srv",
  44. "sys",
  45. "tmp",
  46. "usr",
  47. "var"
  48. ]
  49. }
  50. }
  51. ok: [webB] => {
  52. "ls_result": {
  53. "changed": true,
  54. "cmd": "ls /",
  55. "delta": "0:00:00.002708",
  56. "end": "2018-06-21 11:52:29.359754",
  57. "failed": false,
  58. "rc": 0,
  59. "start": "2018-06-21 11:52:29.357046",
  60. "stderr": "",
  61. "stderr_lines": [],
  62. "stdout": "bin\nboot\ndev\netc\nhome\nlib\nlib64\nmedia\nmnt\nopt\nproc\nroo\nroot\nrun\nsbin\nservice\nsrv\nsys\ntmp\nusr\nvar",
  63. "stdout_lines": [
  64. "bin",
  65. "boot",
  66. "dev",
  67. "etc",
  68. "home",
  69. "lib",
  70. "lib64",
  71. "media",
  72. "mnt",
  73. "opt",
  74. "proc",
  75. "roo",
  76. "root",
  77. "run",
  78. "sbin",
  79. "service",
  80. "srv",
  81. "sys",
  82. "tmp",
  83. "usr",
  84. "var"
  85. ]
  86. }
  87. }
  88. PLAY RECAP ****************************************************************************************************
  89. webA : ok=4 changed=1 unreachable=0 failed=0
  90. webB : ok=4 changed=1 unreachable=0 failed=0

ansible-playbook執行入口配置文件nginx.yaml后,它會自動在roles目錄下查找nginx目錄並進入后查找tasks任務目錄並執行main.yaml的任務配置文件。

其實,這個roles的操作等效於以下配置

 
  1. #本配置和之前的roles配置等效
  2. [root@ansible myroles]# cat /service/scripts/test.yaml
  3. ---
  4. - hosts: all
  5. gather_facts: True
  6. tasks: #其實roles的本質就是將tasks任務單獨寫了。
  7. - name: check alived #並在入口文件里追加了roles去查找tasks配置文件路徑
  8. ping:
  9. - name:
  10. shell: ls /
  11. register: ls_result
  12. - debug: var=ls_result
 

roles中vars自定義變量模組的使用

 
  1. #創建自定義變量vars模組的配置文件
  2. [root@ansible myroles]# cat roles/nginx/vars/main.yaml
  3. ---
  4. my_name: yunjisuan
  5. phone: 1800000000
  6. [root@ansible myroles]# cat roles/nginx/tasks/main.yaml
  7. ---
  8. - name: check alived
  9. ping:
  10. - name:
  11. shell: ls /
  12. register: ls_result
  13. - debug: var=ls_result
  14. - name: #添加對變量引用的任務編排
  15. shell: echo my phone is {{ phone }}
  16. register: echo_result
  17. - debug: var=echo_result
  18. [root@ansible myroles]# ansible-playbook nginx.yaml #執行入口配置文件
 

使用copy,script模塊的標准化

roles模型里使用copy,script模塊,默認從roles/nginx/files這里面找

 
  1. [root@ansible myroles]# cat roles/nginx/files/test
  2. welcome to yunjisuan
  3. [root@ansible myroles]# cat roles/nginx/files/test.sh
  4. echo "aaa" >> /tmp/test
  5. [root@ansible myroles]# chmod +x roles/nginx/files/test.sh
  6. [root@ansible myroles]# cat roles/nginx/tasks/main.yaml
  7. ---
  8. - name: check alived
  9. ping:
  10. - name:
  11. shell: ls /
  12. register: ls_result
  13. - debug: var=ls_result
  14. - name:
  15. shell: echo my phone is {{ phone }}
  16. register: echo_result
  17. - debug: var=echo_result
  18. - name: #添加copy模塊
  19. copy: src=test dest=/root/
  20. - name: #添加script模塊(自動在目標IP機器上執行腳本)
  21. script: test.sh
  22. [root@ansible myroles]# ansible-playbook nginx.yaml
 

roles中template模塊的使用

roles模型里使用template模塊,默認從roles/nginx/template里面找

 
  1. [root@ansible myroles]# cat roles/nginx/templates/test.j2
  2. myname is {{ my_name }},my phone is {{ phone }} #引用自定義變量
  3. my ipaddress is {{ansible_all_ipv4_addresses[0]}} #引用內置變量
  4. [root@ansible myroles]# cat roles/nginx/tasks/main.yaml
  5. ---
  6. - name: check alived
  7. ping:
  8. - name:
  9. shell: ls /
  10. register: ls_result
  11. - debug: var=ls_result
  12. - name:
  13. shell: echo my phone is {{ phone }}
  14. register: echo_result
  15. - debug: var=echo_result
  16. - name:
  17. copy: src=test dest=/root/
  18. - name:
  19. script: test.sh
  20. - name:
  21. template: src=test.j2 dest=/root/test2 #下發可變配置文件
  22. [root@ansible myroles]# ansible-playbook nginx.yaml
 

roles中notify模塊的使用

roles使用notify模塊,默認從roles/nginx/handles里面找

 
  1. [root@ansible myroles]# cat roles/nginx/handlers/main.yaml
  2. ---
  3. - name: start_nginx #定義handlers的動作類型
  4. shell: /usr/local/nginx/sbin/nginx
  5. - name: stop_nginx #定義handlers的動作類型
  6. shell: /usr/local/nginx/sbin/nginx -s stop
  7. - name: reload_nginx #定義handlers的動作類型
  8. shell: /usr/local/nginx/sbin/nginx -s reload
  9. [root@ansible myroles]# cat roles/nginx/tasks/main.yaml
  10. ---
  11. - name: check alived
  12. ping:
  13. - name:
  14. shell: ls /
  15. register: ls_result
  16. - debug: var=ls_result
  17. - name:
  18. shell: echo my phone is {{ phone }}
  19. register: echo_result
  20. - debug: var=echo_result
  21. - name:
  22. copy: src=test dest=/root/
  23. - name:
  24. script: test.sh
  25. - name:
  26. template: src=test.j2 dest=/root/test2
  27. notify: start_nginx #執行template任務后下發通知給handlers執行start_nginx
  28. [root@ansible myroles]# ansible-playbook nginx.yaml
 

特別提示:

notify下發通知只有當之前的任務造成了變化那么才會被執行,如果沒有發生任何改變,則notify不會被執行。例如:

 
  1. #tasks任務造成改變,觸發notify
  2. [root@ansible myroles]# cat /tmp/test.yaml
  3. ---
  4. - hosts: webA
  5. gather_facts: True
  6. tasks:
  7. - name:
  8. copy: src=/tmp/test dest=/root/ #這步造成目標改變才能出發notify
  9. notify: start_nginx
  10. handlers:
  11. - name: start_nginx
  12. shell: /usr/local/nginx/sbin/nginx
  13. [root@ansible myroles]# ansible-playbook /tmp/test.yaml
  14. PLAY [webA] ***************************************************************************************************
  15. TASK [Gathering Facts] ****************************************************************************************
  16. ok: [webA]
  17. TASK [copy] ***************************************************************************************************
  18. changed: [webA] #發生了改變
  19. RUNNING HANDLER [start_nginx] #觸發notify *********************************************************************************
  20. changed: [webA]
  21. PLAY RECAP ****************************************************************************************************
  22. webA : ok=3 changed=2 unreachable=0 failed=0
  23. #我們再次執行/tmp/test.yaml
  24. [root@ansible myroles]# ansible-playbook /tmp/test.yaml
  25. PLAY [webA] ***************************************************************************************************
  26. TASK [Gathering Facts] ****************************************************************************************
  27. ok: [webA]
  28. TASK [copy] ***************************************************************************************************
  29. ok: [webA] #沒有造成任務改變,未觸發notify通知
  30. PLAY RECAP ****************************************************************************************************
  31. webA : ok=2 changed=0 unreachable=0 failed=0
 

ansible批量管理企業案例

4.1 ansible all -m copy -a 'src=/root/xin.sh dest=/root'

4.2 ansible all -m user -a 'name=yunjisuan shell=/sbin/nologin createhome=no'

4.3 ansible all -m shell -a 'echo "123123" | pass --stdin root'

4.4 ansible all -m user -a 'name=benet password=321321 shell=/bin/bash'

4.5 ansible all -m cron -a 'job="/bin/echo xin > /tmp/sl" name="test cron job"'

4.6 ansible all -m shell -a 'vmstat'

image_1cqeabap11fkl18uk14c6m5618qs9.png-227.6kB


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM