一,工具與環境介紹
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
make && make install
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是沒有配置文件的,需要我們自己創建一個
mkdir -p /etc/ansible
vim /etc/ansible/hosts
cat /etc/ansible/hosts
[nginx] #被管理的主機組名稱
webA ansible_ssh_host=192.168.200.132 ansible_ssh_port=22 ansible_ssh_user=root #第一台主機
webB ansible_ssh_host=192.168.200.138 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=666666 #第二台主機
特別提示:
WebA ===> 主機名
ansible_ssh_host ===>主機IP
ansible_ssh_port ===>ssh的默認端口
ansible_ssh_user ===>ssh的用戶名
ansible_ssh_pass ===>ssh的用戶的連接密碼
如果設置了ssh免密鑰,就不需要寫密碼,例如:webA .沒有設置免密鑰,那么就需要安裝sshpass工具,並在/etc/ansible/hosts文件里寫上主機的連接密碼。例如webB
下載epel源安裝sshpass
2.[root@ansible python]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
3.[root@ansible python]# yum -y install sshpass
4.[root@ansible python]# which sshpass
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.#進行ping模塊的連接測試
2.[root@ansible python]# ansible nginx -m ping
3.webB | FAILED! => { #我們發現webB還是沒鏈接成功,這是因為本機的known_hosts文件還沒有記錄對方主機的信息。
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.}
6.webA | SUCCESS => { #webA成功
7. "changed": false,
8. "ping": "pong"
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
操作測試
#A
[root@ansible .ssh]# ansible webA -m ping
webA | SUCCESS => {
"changed": false,
"ping": "pong"
}
# all
[root@ansible .ssh]# ansible all -m ping
webA | SUCCESS => {
"changed": false,
"ping": "pong"
}
webB | SUCCESS => {
"changed": false,
"ping": "pong"
}
#A:B
[root@ansible .ssh]# ansible webA:webB -m ping
webA | SUCCESS => {
"changed": false,
"ping": "pong"
}
webB | SUCCESS => {
"changed": false,
"ping": "pong"
}
#\!A
[root@ansible .ssh]# ansible all:\!webA -m ping
webB | SUCCESS => {
"changed": false,
"ping": "pong"
}
五,Ansible的三個命令模塊
5.1 ansible模塊command
1.command支持直接回顯命令的執行結果
[root@ansible ~]# ansible all -m command -a "pwd"
webA | SUCCESS | rc=0 >>
/root
webB | SUCCESS | rc=0 >>
/root
注:command不支持管道符不支持重定向,不建議使用
5.2 Ansible模塊shell
1.shell模塊支持管道符
[root@ansible ~]# ansible all -m shell -a "echo testansible | grep a"
webA | SUCCESS | rc=0 >>
testansible
webB | SUCCESS | rc=0 >>
testansible
2.shell支持重定向
[root@ansible ~]# ansible all -m shell -a "echo bb >> /tmp/testansible"
webA | SUCCESS | rc=0 >>
webB | SUCCESS | rc=0 >>
注:如果遇到特殊符號需要加入\轉義,ansible才能正常運行
5.3 Ansible模塊raw,最原始的方式運行命令(不依賴python,僅通過ssh實現)
1.清除yum緩存
[root@ansible ~]# ansible all -m raw -a "yum -y clean all"
webB | SUCCESS | rc=0 >>
Loaded plugins: fastestmirror
Cleaning repos: c7-media
Cleaning up everything
Shared connection to 192.168.200.63 closed.
webA | SUCCESS | rc=0 >>
Loaded plugins: fastestmirror
Cleaning repos: c7-media epel
Cleaning up everything
Cleaning up list of fastest mirrors
Shared connection to 192.168.200.132 closed.
4.建立yum緩存
[root@ansible ~]# ansible all -m raw -a "yum makecache"
webA | SUCCESS | rc=0 >>
Loaded plugins: fastestmirror
c7-media | 3.6 kB 00:00
Loading mirror speeds from cached hostfile
* c7-media:
Metadata Cache Created
Shared connection to 192.168.200.132 closed.
webB | SUCCESS | rc=0 >>
Loaded plugins: fastestmirror
c7-media | 3.6 kB 00:00
Loading mirror speeds from cached hostfile
* c7-media:
Metadata Cache Created
Shared connection to 192.168.200.138 closed.
3.yum裝nmap包
ansible all -m raw -a "yum -y install nmap"
5.4Ansible的copy模塊批量下發文件或文件夾
1. copy模塊概述
**copy模塊的參數:**ansible 主機組 -m 模塊 -a 命令
osrc:指定源文件或目錄
odest:指定目標服務器的文件或目錄
obackup:是否要備份
oowner:拷貝到目標服務器后,文件或目錄的所屬用戶
ogroup:拷貝到目標服務器后,文件或目錄的所屬群組
omode:文件或目錄的權限
准備工作
[root@ansible ~]# mkdir -p /service/scripts
[root@ansible ~]# echo "aaa" > /service/scripts/test.txt
[root@ansible ~]# echo "bbb" > /service/scripts/test2.tx
所有被管理端節點必須安裝libselinux-python包
yum -y install libselinux-python
2. copy模塊拷貝文件
特別提示:如果目標路徑不存在會自動創建 : src===>源文件路徑 dest=目標路徑位置
[root@ansible ~]# ansible all -m copy -a "src=/service/scripts/test.txt dest=/service/scripts/"
webB | FAILED! => { #節點未安裝libselinux-python
"changed": false,
"checksum": "972a1a11f19934401291cc99117ec614933374ce",
"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
}
webA | SUCCESS => {
"changed": true,
"checksum": "972a1a11f19934401291cc99117ec614933374ce",
"dest": "/service/scripts/test.txt",
"gid": 0,
"group": "root",
"md5sum": "5c9597f3c8245907ea71a89d9d39d08e",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:svc_svc_t:s0",
"size": 4,
"src": "/root/.ansible/tmp/ansible-tmp-1529035954.8010113-22928023490467/source",
"state": "file",
"uid": 0
}
節點安裝libselinux-python后在進行發送測試
[root@ansible ~]# ansible webB -m copy -a "src=/service/scripts/test.txt dest=/service/scripts/"
webB | SUCCESS => { #發送成功
"changed": true,
"checksum": "972a1a11f19934401291cc99117ec614933374ce",
"dest": "/service/scripts/test.txt",
"gid": 0,
"group": "root",
"md5sum": "5c9597f3c8245907ea71a89d9d39d08e",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:svc_svc_t:s0",
"size": 4,
"src": "/root/.ansible/tmp/ansible-tmp-1529036146.1609693-94270890826089/source",
"state": "file",
"uid": 0
}
3. copy模塊拷貝文件夾
特別提示: 如果目標路徑里有與我拷貝的文件同名文件的話,會直接覆蓋目標路徑下的文件
1.拷貝/service/scripts/ 目錄下所有內容到dest的路徑下
[root@ansible ~]# ansible webA -m copy -a "src=/service/scripts/ dest=/service/scripts/"
webA | SUCCESS => {
"changed": true,
"dest": "/service/scripts/",
"src": "/service/scripts/"
}
2.拷貝/service/scripts目錄本身及其內部的所有內容到dest的路徑下
[root@ansible ~]# ansible webA -m copy -a "src=/service/scripts dest=/service/scripts/"
webA | SUCCESS => {
"changed": true,
"dest": "/service/scripts/",
"src": "/service/scripts"
}
4 copy模塊自動備份
特別提示:
參數:backup=yes ===>意思是,如果目標路徑下,有與我同名但不同內容的文件時,在覆蓋前,對目標文件先進行備份
[root@ansible ~]# ansible webB -m copy -a "src=/service/scripts/ dest=/service/scripts/ backup=yes"
webB | SUCCESS => {
"changed": true,
"dest": "/service/scripts/",
"src": "/service/scripts/"
}
5 copy模塊指定用戶和屬主屬組權限
[root@ansible ~]# ansible webA -m copy -a "src=/service/scripts/ dest=/service/scripts/ owner=nobody group=nobody mode=0600"
webA | SUCCESS => {
"changed": true,
"dest": "/service/scripts/",
"src": "/service/scripts/"
}
5.5 Ansible的script模塊批量運行腳本
能夠實現遠程服務器批量運行本地的shell腳本
1.遠程批量分發並自動部署nginx
所有被管理端需要掛載光盤,並創建本地yum配置文件
scripts目錄下需要兩個腳本和解壓包:
1,自動分安裝腳本
2,分發腳本
3.nginx-1.10.2.tar.gz #nginx源碼包
1.自動安裝腳本
#!/bin/sh
#nginx install shell scripts
test -d /media/cdrom || mkdir -p /media/cdrom #有沒有目錄
mount /dev/sr0 /media/cdrom &>/dev/null #掛在光盤
yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel &>/dev/null #安裝軟件包
test -d /service/scripts || mkdir -p /service/scripts #有沒有目錄沒有創建
cd /service/scripts/ #進入目錄
tar xf nginx-1.10.2.tar.gz -C /usr/src/ #解壓包
cd /usr/src/nginx-1.10.2/ #進入Nginx目錄
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module &>/dev/null #編譯
make &>/dev/null
make install &>/dev/null #安裝
exit 0
2.分發腳本
#!/bin/sh
Group=$1 #對方服務器位置(主機名或主機組明)
ansible $Group -m copy -a "src=/server/scripts/ dest=/service/scripts/ mode=0755" #分發范圍
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的執行模板**
--- #開頭三個小-開頭
- hosts: webB
tasks: #任務,標注
- name: test #任務名字
shell: echo "welcome to yunjisaun" >> /tmp/username #模塊名:命令
- name: test2
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.做劇本
vim test_copy.yaml
---
- hosts: all
tasks:
- name: test copy
copy: src=/tmp/copy_test dest=/tmp/
3.劇本激活
ansible-playbook /service/scripts/test_copy.yaml
6.3 playbook使用register輸出命令運行結果
我們在用playbook進行ansible模塊操作的時候,並沒有命令的執行結果輸出,默認被隱藏了,可以通過register模塊追加輸出命令的執行結果
1.寫劇本
---
- hosts: all
tasks:
- name: test register
shell: echo "welcome to yunjisuan"
register: print_result #將之前命令的輸出結果保存在變量print_result里
- debug: var=print_result #將變量的值作為debug輸出出來
2.執行劇本
ansible-playbook test_register.yaml
6.4 nginx配置下發並檢測
1.寫腳本
vim test_nginx_conf.yaml
---
- hosts: all
tasks:
- name: copy nginx.conf
copy: src=/tmp/nginx.conf dest=/usr/local/nginx/conf/ backup=yes
- name:
shell: /usr/local/nginx/sbin/nginx -t
register: nginx_result
- debug: var=nginx_result
2.執行腳本
ansible-playbook test_nginx_conf.yaml
七,playbook的自定義變量和內置變量
7.1 在Playbook中使用自定義變量
1.寫劇本
---
- hosts: all
vars: #定義變量
- name: "yunjisuan" #第一個name變量
age: "3" #第二個age變量
tasks:
- name: "{{ name }}" #{{}}兩對大括號引用變量,變量名兩頭空格
shell: echo "myname {{ name }},myage {{ age }}"
register: var_result
- debug: var=var_result
特別提示:
引用變量需要在雙引號中引用
注:會有一個警告:因為自定義變量的名字和自定義變量名字沖突[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
1. 寫劇本
vim est_setupvars.yaml
---
- hosts: all
gather_facts: True #使用ansible內置變量
tasks:
- name: setup var
shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}"
register: var_result
- debug: var=var_result
2. 執行腳本
ansible-playbook test_setupvars.yaml
八,Playbook下發可變配置文件
8.1 利用template模塊下發可變的配置文件
1.寫一個含有變量的文件
vim /tmp/test
my name is {{ myname }} #自定義變量
my name is {{ ansible_all_ipv4_addresses[0] }} #系統變量
2.寫一個文件的含有變量
vim test_filevars.yaml
---
- hosts: all
gather_facts: True #開啟系統變量
vars:
- myname: "yunjisuan" #自定義變量
tasks:
- name: template test
template: src=/tmp/test dest=/root/test #使用template下發可變配置文件
執行劇本
ansible-playbook test_filevars.yaml
8.2 下發配置文件里面使用判斷語法
1.寫一個半段語法的文件,以.j2結尾
vim /tmp/if.j2
{% if PORT %} #if PORT存在
ip=0.0.0.0:{{ PORT }}
{% else %} #不為真
ip=0.0.0.0:80
{% endif %} #結尾
測試:
vim test_ifvars.yaml ---
- hosts: all
gather_facts: True #開啟系統內置變量
vars:
- PORT: 90 #自定義變量
tasks:
- name: jinja2 if test
template: src=/tmp/if.j2 dest=/root/test
2.執行劇本
九, Playbook的notify通知和下發nginx配置
1.實戰下發可執行動作的可變的nginx配置文件
# cat test_nginxvars.yaml
---
- hosts: all
gather_facts: True #開啟系統內置變量
tasks:
- name: nginx conf
template: src=/tmp/nginx.j2 dest=/usr/local/nginx/conf/nginx.conf
notify:
- reload nginx #下發通知給handlers模塊執行名字叫做reload nginx的動作
handlers: #定義動作
- name: reload nginx #動作的名字
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