Ansible運維自動化


一,工具與環境介紹

 

1.1 Ansible簡介

  • 批量管理服務器的工具。
  • 優點:無需部署agent,沒客戶端,客戶端只要支持Python即可。
  • 通過ssh進行管理,遠程登錄管理。
  • 目前github上最流行的自動化運維工具,沒有之一。
 

1.2 Jenkins簡介

  • 可視化運維(主要用在可視化部署):能夠把腳本或者管理工具的一些信息輸出放在Web界面讓你看,同時還可以看一些歷史的信息輸出。
  • 可持續構建,和git,svn結合,可以發出指令把開發新寫的代碼從代碼倉庫里直接tar打包發送到對面服務器里同時啟動自動安裝程序。 
    git,svn:一種開發可以存放代碼的倉庫
  • 與ssh和ansible結合均可實現可視化運維
 

1.3 環境說明

  • 關閉防火牆
  • 關閉sekinux
 

二, Python3與Ansible的安裝

 

2.1 使用源碼安裝Python3.5

 

2.1.1 安裝支持包

裝兩個雲yum源: 
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
安裝支持包: 
yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses-devel unzip zlib-devel zlib openssl-devel openssl libffi-devel epel-release libselinux-python

 

2.1.2 源碼包編譯 Python3.5

雲yum下載地址: 
wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

tar xf Python-3.5.2.tgz -C /usr/src/ 
cd /usr/src/Python-3.5.2/ 
./configure --prefix=/usr/local/python 
image_1csiuqp4v44eane12mh7ga47b9.png-5kB

make && make install 
image_1csj3mavq118iamb2em1idp1350m.png-8.2kB

 

2.1.2 做軟連接

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

 

2.1.3 查版本號

python3 -V

 

2.2 使用pip3安裝ansible

 

2.2.1 安裝ansible最新版本

/usr/local/python/bin/pip3 install ansible

 

2.2.2 做軟連接

ln -s /usr/local/python/bin/ansible /usr/local/bin 
which ansible 
ansible --version(查看版本號)

 

2.3 Ansible查看幫助

/usr/local/python/bin/ansible-doc -l 查看總幫助 
/usr/local/python/bin/ansible-doc -s shell 查看Shell模塊的幫助

 

三,使用公鑰實現ssh無密碼登陸

  • 下載SCP
 

3.1 生成密鑰對

ssh-keygen -t rsa -f ~/.ssh/id_rsa -P "" 
``

 

3.2 分發密鑰

  sshpass -p 對方電腦登陸密碼 ssh-copy-id -i ~/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no 192.168.200.63"
-o StrictHostKeyChecking=no 不記錄

 

四,Ansible的簡單配置和ping模塊

 

4.1 Ansible的配置文件

通過pip3安裝的ansible是沒有配置文件的,需要我們自己創建一個

 
  1. mkdir -p /etc/ansible
  2. vim /etc/ansible/hosts
 
  1. cat /etc/ansible/hosts
  2. [nginx] #被管理的主機組名稱
  3. webA ansible_ssh_host=192.168.200.132 ansible_ssh_port=22 ansible_ssh_user=root #第一台主機
  4. webB ansible_ssh_host=192.168.200.138 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=666666 #第二台主機
  5. 特別提示:
  6. WebA ===> 主機名
  7. ansible_ssh_host ===>主機IP
  8. ansible_ssh_port ===>ssh的默認端口
  9. ansible_ssh_user ===>ssh的用戶名
  10. ansible_ssh_pass ===>ssh的用戶的連接密碼

如果設置了ssh免密鑰,就不需要寫密碼,例如:webA .沒有設置免密鑰,那么就需要安裝sshpass工具,並在/etc/ansible/hosts文件里寫上主機的連接密碼。例如webB

 

下載epel源安裝sshpass

 
  1. 2.[root@ansible python]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  2. 3.[root@ansible python]# yum -y install sshpass
  3. 4.[root@ansible python]# which sshpass
  4. 5./usr/bin/sshpass
 

4.2 進行ansible遠程執行命令測試

 

1.語法:

ansible Web1 -m command -a 'uptime'-->獲取Web1服務器的平均負載值 
ansible 主機組 -m ansible內置功能模塊名 -a 命令

all:代表所有主機 
nginx:模塊名,只發nginx模塊下的主機 
單發:Web1=主機名 
多發:Web1:Web2 
指定all但不包含web2:all:\ !web1,注意!前需要加轉意符號(\)

 

2.進行命令測試:

 
  1. 1.#進行ping模塊的連接測試
  2. 2.[root@ansible python]# ansible nginx -m ping
  3. 3.webB | FAILED! => { #我們發現webB還是沒鏈接成功,這是因為本機的known_hosts文件還沒有記錄對方主機的信息。
  4. 4. "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
  5. 5.}
  6. 6.webA | SUCCESS => { #webA成功
  7. 7. "changed": false,
  8. 8. "ping": "pong"
  9. 9.}
 

4.3 ansible的簡單使用方式

ansible -i /etc/ansible/hosts 主機或主機組 -m 指定模塊 -a 命令 
-i:指定配置文件位置,不指定默認去/etc/ansible/hosts 里找 
-m : 指定操作模塊,后跟模塊名 
-a :發布,后面跟命令

 

4.4 使用ping模塊用來查看服務器是否連接正常,ping模塊不需要-a指定參數

ansible all -m ping

 

操作測試

 
  1. #A
  2. [root@ansible .ssh]# ansible webA -m ping
  3. webA | SUCCESS => {
  4. "changed": false,
  5. "ping": "pong"
  6. }
 
  1. # all
  2. [root@ansible .ssh]# ansible all -m ping
  3. webA | SUCCESS => {
  4. "changed": false,
  5. "ping": "pong"
  6. }
  7. webB | SUCCESS => {
  8. "changed": false,
  9. "ping": "pong"
  10. }
 
  1. #A:B
  2. [root@ansible .ssh]# ansible webA:webB -m ping
  3. webA | SUCCESS => {
  4. "changed": false,
  5. "ping": "pong"
  6. }
  7. webB | SUCCESS => {
  8. "changed": false,
  9. "ping": "pong"
  10. }
 
  1. #\!A
  2. [root@ansible .ssh]# ansible all:\!webA -m ping
  3. webB | SUCCESS => {
  4. "changed": false,
  5. "ping": "pong"
  6. }
 

五,Ansible的三個命令模塊

 

5.1 ansible模塊command

 

1.command支持直接回顯命令的執行結果

 
  1. [root@ansible ~]# ansible all -m command -a "pwd"
  2. webA | SUCCESS | rc=0 >>
  3. /root
  4. webB | SUCCESS | rc=0 >>
  5. /root

注:command不支持管道符不支持重定向,不建議使用

 

5.2 Ansible模塊shell

 

1.shell模塊支持管道符

 
  1. [root@ansible ~]# ansible all -m shell -a "echo testansible | grep a"
  2. webA | SUCCESS | rc=0 >>
  3. testansible
  4. webB | SUCCESS | rc=0 >>
  5. testansible
 

2.shell支持重定向

 
  1. [root@ansible ~]# ansible all -m shell -a "echo bb >> /tmp/testansible"
  2. webA | SUCCESS | rc=0 >>
  3. webB | SUCCESS | rc=0 >>

注:如果遇到特殊符號需要加入\轉義,ansible才能正常運行

 

5.3 Ansible模塊raw,最原始的方式運行命令(不依賴python,僅通過ssh實現)

 

1.清除yum緩存

 
  1. [root@ansible ~]# ansible all -m raw -a "yum -y clean all"
  2. webB | SUCCESS | rc=0 >>
  3. Loaded plugins: fastestmirror
  4. Cleaning repos: c7-media
  5. Cleaning up everything
  6. Shared connection to 192.168.200.63 closed.
  7. webA | SUCCESS | rc=0 >>
  8. Loaded plugins: fastestmirror
  9. Cleaning repos: c7-media epel
  10. Cleaning up everything
  11. Cleaning up list of fastest mirrors
  12. Shared connection to 192.168.200.132 closed.
 

4.建立yum緩存

 
  1. [root@ansible ~]# ansible all -m raw -a "yum makecache"
  2. webA | SUCCESS | rc=0 >>
  3. Loaded plugins: fastestmirror
  4. c7-media | 3.6 kB 00:00
  5. Loading mirror speeds from cached hostfile
  6. * c7-media:
  7. Metadata Cache Created
  8. Shared connection to 192.168.200.132 closed.
  9. webB | SUCCESS | rc=0 >>
  10. Loaded plugins: fastestmirror
  11. c7-media | 3.6 kB 00:00
  12. Loading mirror speeds from cached hostfile
  13. * c7-media:
  14. Metadata Cache Created
  15. Shared connection to 192.168.200.138 closed.
 

3.yum裝nmap包

 
  1. ansible all -m raw -a "yum -y install nmap"
 

5.4Ansible的copy模塊批量下發文件或文件夾

 

1. copy模塊概述

**copy模塊的參數:**ansible 主機組 -m 模塊 -a 命令

osrc:指定源文件或目錄 
odest:指定目標服務器的文件或目錄 
obackup:是否要備份 
oowner:拷貝到目標服務器后,文件或目錄的所屬用戶 
ogroup:拷貝到目標服務器后,文件或目錄的所屬群組 
omode:文件或目錄的權限

 

准備工作

 
  1. [root@ansible ~]# mkdir -p /service/scripts
  2. [root@ansible ~]# echo "aaa" > /service/scripts/test.txt
  3. [root@ansible ~]# echo "bbb" > /service/scripts/test2.tx
  4. 所有被管理端節點必須安裝libselinux-python
  5. yum -y install libselinux-python
 

2. copy模塊拷貝文件

特別提示:如果目標路徑不存在會自動創建 : src===>源文件路徑 dest=目標路徑位置

 
  1. [root@ansible ~]# ansible all -m copy -a "src=/service/scripts/test.txt dest=/service/scripts/"
  2. webB | FAILED! => { #節點未安裝libselinux-python
  3. "changed": false,
  4. "checksum": "972a1a11f19934401291cc99117ec614933374ce",
  5. "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
  6. }
  7. webA | SUCCESS => {
  8. "changed": true,
  9. "checksum": "972a1a11f19934401291cc99117ec614933374ce",
  10. "dest": "/service/scripts/test.txt",
  11. "gid": 0,
  12. "group": "root",
  13. "md5sum": "5c9597f3c8245907ea71a89d9d39d08e",
  14. "mode": "0644",
  15. "owner": "root",
  16. "secontext": "system_u:object_r:svc_svc_t:s0",
  17. "size": 4,
  18. "src": "/root/.ansible/tmp/ansible-tmp-1529035954.8010113-22928023490467/source",
  19. "state": "file",
  20. "uid": 0
  21. }
 
節點安裝libselinux-python后在進行發送測試
 
  1. [root@ansible ~]# ansible webB -m copy -a "src=/service/scripts/test.txt dest=/service/scripts/"
  2. webB | SUCCESS => { #發送成功
  3. "changed": true,
  4. "checksum": "972a1a11f19934401291cc99117ec614933374ce",
  5. "dest": "/service/scripts/test.txt",
  6. "gid": 0,
  7. "group": "root",
  8. "md5sum": "5c9597f3c8245907ea71a89d9d39d08e",
  9. "mode": "0644",
  10. "owner": "root",
  11. "secontext": "system_u:object_r:svc_svc_t:s0",
  12. "size": 4,
  13. "src": "/root/.ansible/tmp/ansible-tmp-1529036146.1609693-94270890826089/source",
  14. "state": "file",
  15. "uid": 0
  16. }
 

3. copy模塊拷貝文件夾

特別提示: 如果目標路徑里有與我拷貝的文件同名文件的話,會直接覆蓋目標路徑下的文件

 
1.拷貝/service/scripts/ 目錄下所有內容到dest的路徑下
 
  1. [root@ansible ~]# ansible webA -m copy -a "src=/service/scripts/ dest=/service/scripts/"
  2. webA | SUCCESS => {
  3. "changed": true,
  4. "dest": "/service/scripts/",
  5. "src": "/service/scripts/"
  6. }
 
2.拷貝/service/scripts目錄本身及其內部的所有內容到dest的路徑下
 
  1. [root@ansible ~]# ansible webA -m copy -a "src=/service/scripts dest=/service/scripts/"
  2. webA | SUCCESS => {
  3. "changed": true,
  4. "dest": "/service/scripts/",
  5. "src": "/service/scripts"
  6. }
 

4 copy模塊自動備份

特別提示:  
參數:backup=yes ===>意思是,如果目標路徑下,有與我同名但不同內容的文件時,在覆蓋前,對目標文件先進行備份

 
  1. [root@ansible ~]# ansible webB -m copy -a "src=/service/scripts/ dest=/service/scripts/ backup=yes"
  2. webB | SUCCESS => {
  3. "changed": true,
  4. "dest": "/service/scripts/",
  5. "src": "/service/scripts/"
  6. }
 

5 copy模塊指定用戶和屬主屬組權限

 
  1. [root@ansible ~]# ansible webA -m copy -a "src=/service/scripts/ dest=/service/scripts/ owner=nobody group=nobody mode=0600"
  2. webA | SUCCESS => {
  3. "changed": true,
  4. "dest": "/service/scripts/",
  5. "src": "/service/scripts/"
  6. }
 

5.5 Ansible的script模塊批量運行腳本

能夠實現遠程服務器批量運行本地的shell腳本

 

1.遠程批量分發並自動部署nginx

所有被管理端需要掛載光盤,並創建本地yum配置文件 
scripts目錄下需要兩個腳本和解壓包: 
1,自動分安裝腳本 
2,分發腳本 
3.nginx-1.10.2.tar.gz #nginx源碼包

 

1.自動安裝腳本

 
  1. #!/bin/sh
  2. #nginx install shell scripts
  3. test -d /media/cdrom || mkdir -p /media/cdrom #有沒有目錄
  4. mount /dev/sr0 /media/cdrom &>/dev/null #掛在光盤
  5. yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel &>/dev/null #安裝軟件包
  6. test -d /service/scripts || mkdir -p /service/scripts #有沒有目錄沒有創建
  7. cd /service/scripts/ #進入目錄
  8. tar xf nginx-1.10.2.tar.gz -C /usr/src/ #解壓包
  9. cd /usr/src/nginx-1.10.2/ #進入Nginx目錄
  10. ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module &>/dev/null #編譯
  11. make &>/dev/null
  12. make install &>/dev/null #安裝
  13. exit 0
 

2.分發腳本

 
  1. #!/bin/sh
  2. Group=$1 #對方服務器位置(主機名或主機組明)
  3. ansible $Group -m copy -a "src=/server/scripts/ dest=/service/scripts/ mode=0755" #分發范圍
  4. ansible $Group -m script -a "/service/scripts/auto_nginx.sh" #遠程激活
 

3.激活腳本

ansible web1:web2 -m script -a "/bin/sh /service/scripts/auto_nginx.sh"

 

六,Ansible-playbook的初步使用

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

 

6.1 作軟連接

ln -s /usr/local/python/bin/ansible-playbook /usr/local/bin/

 

6.2 playbook的簡單shell模塊的使用

 
1.playbook的執行模板**
 
  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

模板說明: 
--- #開頭必須有三個小-,頂格寫 
- hosts: #正文配置代碼的第一級,必須有兩個空格(-占一個空格位) 
- host: webB #webB是host參數的值,值和hosts:之間要有一個空格 
tasks: #tasks:表示接下來要執行的具體任務 
- name: #相對於tasks再多縮進兩個格(-占一個空格位),表示屬於tasks的下一級 
- name: test #test只是要執行的具體命令的名字可以隨便寫。name:后還是有一個空格要注意 
shell: #表示調用shell模塊執行命令相對於tasks仍舊要多縮進兩個空格 
shell: echo "xxx" >> xxx #shell:后邊還是要有個空格,需要注意。

 
2.激活劇本

ansible-playbook test_shell.yaml

 

6.3 playbook的簡單copy模塊的使用

 
1.做一個copy文件

ehco "welcome to yunjisuan" >> /tmp/test_copy

 
2.做劇本
 
  1. vim test_copy.yaml
  2. ---
  3. - hosts: all
  4. tasks:
  5. - name: test copy
  6. copy: src=/tmp/copy_test dest=/tmp/
 
3.劇本激活

ansible-playbook /service/scripts/test_copy.yaml

 

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

我們在用playbook進行ansible模塊操作的時候,並沒有命令的執行結果輸出,默認被隱藏了,可以通過register模塊追加輸出命令的執行結果

 
1.寫劇本
 
  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輸出出來
 
2.執行劇本

ansible-playbook test_register.yaml

 

6.4 nginx配置下發並檢測

 
1.寫腳本
 
  1. vim test_nginx_conf.yaml
  2. ---
  3. - hosts: all
  4. tasks:
  5. - name: copy nginx.conf
  6. copy: src=/tmp/nginx.conf dest=/usr/local/nginx/conf/ backup=yes
  7. - name:
  8. shell: /usr/local/nginx/sbin/nginx -t
  9. register: nginx_result
  10. - debug: var=nginx_result
 
2.執行腳本

ansible-playbook test_nginx_conf.yaml

 

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

 

7.1 在Playbook中使用自定義變量

 
1.寫劇本
 
  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
  11. 特別提示:
  12. 引用變量需要在雙引號中引用

注:會有一個警告:因為自定義變量的名字和自定義變量名字沖突[WARNING]: Found variable using reserved name: name 
把name修改為Name即可

 
2.執行劇本

ansible-playbook /service/scripts/test_vars.yaml

 

7.2 在playbook中使用ansible內置變量

查看內置變量:ansible 127.0.01 -m setup | less 
image_1csjpqd3q1iicd8hvd1gru1jiq9.png-39.7kB

 
1. 寫劇本
 
  1. vim est_setupvars.yaml
  2. ---
  3. - hosts: all
  4. gather_facts: True #使用ansible內置變量
  5. tasks:
  6. - name: setup var
  7. shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}"
  8. register: var_result
  9. - debug: var=var_result
 
2. 執行腳本

ansible-playbook test_setupvars.yaml

 

八,Playbook下發可變配置文件

 

8.1 利用template模塊下發可變的配置文件

 
1.寫一個含有變量的文件
 
  1. vim /tmp/test
  2. my name is {{ myname }} #自定義變量
  3. my name is {{ ansible_all_ipv4_addresses[0] }} #系統變量
 
2.寫一個文件的含有變量
 
  1. vim test_filevars.yaml
  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下發可變配置文件
 
執行劇本

ansible-playbook test_filevars.yaml

 

8.2 下發配置文件里面使用判斷語法

 
1.寫一個半段語法的文件,以.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 %} #結尾

測試:

 
  1. vim test_ifvars.yaml ---
  2. - hosts: all
  3. gather_facts: True #開啟系統內置變量
  4. vars:
  5. - PORT: 90 #自定義變量
  6. tasks:
  7. - name: jinja2 if test
  8. template: src=/tmp/if.j2 dest=/root/test
 
2.執行劇本
 

九, Playbook的notify通知和下發nginx配置

 

1.實戰下發可執行動作的可變的nginx配置文件

 
  1. # cat test_nginxvars.yaml
  2. ---
  3. - hosts: all
  4. gather_facts: True #開啟系統內置變量
  5. tasks:
  6. - name: nginx conf
  7. template: src=/tmp/nginx.j2 dest=/usr/local/nginx/conf/nginx.conf
  8. notify:
  9. - reload nginx #下發通知給handlers模塊執行名字叫做reload nginx的動作
  10. handlers: #定義動作
  11. - name: reload nginx #動作的名字
  12. shell: /usr/local/nginx/sbin/nginx -s reload
 
2.執行劇本

ansible-playbook test_nginxvars.yaml

注:當你的上一條使得對方發生了改變才會觸發notify通知

對新裝的操作系統部署服務: 
1.自動安裝服務 
2.把寫好的配置文件覆蓋到遠程主機服務目錄下 
3.啟動服務

已經部署好的服務,更新配置文件 
1.Nginx反向代理引流(發個配置文件重啟服務)

搭建LNMP步驟模塊划分 
1.搭建Nginx 
2.搭建PHP 
3.搭建MySQL 
4.分發Nginx,php,MySQL的配置文件 
4.啟動nginx,php,mysql


免責聲明!

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



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