一,工具與環境介紹
1.1 ansible簡介
批量管理服務器的工具
無需部署agent,通過ssh進行管理
流行的自動化運維工具:https://github.com/ansible/ansible
1.2 jenkins簡介
可視化運維(主要用在可視化部署)
持續構建,可以和git,svn結合
可結合ssh實現可視化運維
可結合ansible實現可視化運維
1.3 環境說明
Centos7.5(yum -y install net-tools vim)
關閉防火牆(systemctl stop firewalld,systemctl disable firewalld)
關閉selinux
二,Python3與ansible的安裝
2.1 使用源碼安裝Python3.5
安裝支持包
[root@ansibel ~]# yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses-d
evel unzip zlib-devel zlib openssl-devel openssl
源碼編譯Python3.5
源碼編譯Python3.5[root@ansibel ~]# tar xf Python-3.5.2.tgz -C /usr/src/
[root@ansibel ~]# cd /usr/src/Python-3.5.2/
[root@ansibel Python-3.5.2]# ./configure --prefix=/usr/local/python/
[root@ansibel Python-3.5.2]# make && make install
[root@ansibel Python-3.5.2]# ln -s /usr/local/python/bin/python3 /usr/bin/python3
[root@ansibel Python-3.5.2]# which python3
/usr/bin/python3
[root@ansibel Python-3.5.2]# python3 -V
Python 3.5.2
2.2 使用pip3安裝ansible
安裝ansible最新版本
[root@ansibel Python-3.5.2]# /usr/local/python/bin/pip3 install --upgrade pip
[root@ansibel Python-3.5.2]# /usr/local/python/bin/pip3 install ansible
靜心等待ansible安裝完畢后
[root@ansibel Python-3.5.2]# ln -s /usr/local/python/bin/ansible /usr/local/bin/
[root@ansibel Python-3.5.2]# which ansible
/usr/local/bin/ansible
[root@ansibel Python-3.5.2]# ansible --version
ansible 2.7.5
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/a
nsible/plugins/modules'] ansible python module location = /usr/local/python/lib/python3.5/site-packages/a
nsible executable location = /usr/local/bin/ansible
python version = 3.5.2 (default, Dec 25 2018, 19:07:10) [GCC 4.8.5 20150623 (Red
Hat 4.8.5-36)]
2.3 ansible查看幫助
[root@ansible ~]# /usr/local/python/bin/ansible-doc -l 查看總幫助
[root@ansible ~]# /usr/local/python/bin/ansible-doc -s shell 查看shell模塊的幫助
[root@ansible ~]# /usr/local/python/bin/ansible-doc -s raw 查看raw模塊的幫助
三,使用公私鑰實現ssh無密碼登陸
ansible是無agent的,無agent是怎么批量管理服務器的?主要是借用ssh來批量管理服務器。
ssh默認登陸是需要密碼的,所以管理起來比較麻煩,本次實驗主要是介紹ssh的無密碼登陸。
ssh無密碼登陸實現以后,使用ansible批量管理服務器就變得簡單了。
| Host | IP |
|---|---|
| ansible | 10.1.1.131 |
| web01 | 10.1.1.132 |
| web02 | 10.1.1.133 |
生成秘鑰對
[root@ansibel python]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ""
Generating public/private rsa key pair.
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:
SHA256:xxnEp0IW6w0wuI6oPA5TOp/m0pRo/X7TTHXRl9EkoeY root@ansibel
The key's randomart image is:
+---[RSA 2048]----+
| .o .o. ++=|
| . ooo. .o +o|
| . oo .oo . .|
| . ..+.* . |
| oo+ S.* E |
|o++.. o |
|Oo . + |
|+*o. . o o |
|.== ... . |
+----[SHA256]-----+
分發秘鑰
[root@ansibel python]# ssh-copy-id -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=
no" 10.1.1.132
進行免密碼登錄測試
[root@ansibel python]# ssh 10.1.1.132
Last login: Tue Dec 25 18:52:58 2018 from 10.1.1.1
[root@web01 ~]# hostname -I
10.1.1.132
[root@web01 ~]# exit
logout
Connection to 10.1.1.132 closed.
[root@ansibel python]# hostname -I
10.1.1.131
四,ansible的簡單配置和ping模塊
4.1 ansible的配置文件
通過pip安裝的ansible是沒有配置文件的。我們需要創建一個
特別提示:Web01 ===> 主機名ansible_ssh_host ===>主機IPansible_ssh_port ===>ssh的默認端口ansible_ssh_user ===>ssh的用戶名ansible_ssh_pass ===>ssh的用戶的連接密碼
[root@ansibel python]# mkdir -p /etc/ansible
[root@ansibel python]# > /etc/ansible/hosts
[root@ansibel python]# vim /etc/ansible/hosts
[root@ansibel python]# cat /etc/ansible/hosts #ansible主機管理配置文件
[nginx] #被管理的主機組名稱
web01 ansible_ssh_host=10.1.1.132 #第一台主機
web02 ansible_ssh_host=10.1.1.133 ansible_ssh_port=22 ansible_ssh_user=root ansibl
e_ssh_pass=666666 #第二台主機
如果我們已經設置了ssh免密鑰了。那么就不需要寫密碼了。例如:web01
我們要是沒有設置免密鑰,那么就需要安裝sshpass工具,並在/etc/ansible/hosts文件里寫上主機的連接密碼。例如web02
#下載epel源安裝sshpass
[root@ansible python]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@ansible python]# yum -y install sshpass
[root@ansible python]# which sshpass
/usr/bin/sshpass
4.2 進行ansible遠程執行命令測試
語法:
ansible chensiqi -m command -a 'uptime'
ansible 主機組 -m ansible內置功能模塊名 -a 命令
進行命令測試:
[root@ansibel ansible]# ansible web01 -m command -a 'uptime'
web01 | CHANGED | rc=0 >>
19:53:13 up 1:04, 3 users, load average: 0.00, 0.01, 0.05
[root@ansibel ansible]# ansible web02 -m command -a 'uptime'
web02 | CHANGED | rc=0 >>
19:53:19 up 1:04, 3 users, load average: 0.00, 0.01, 0.05
[root@ansibel ansible]# ansible web01 -m command -a 'hostname -I'
web01 | CHANGED | rc=0 >>
10.1.1.132
[root@ansibel ansible]# ansible web02 -m command -a 'hostname -I'
web02 | CHANGED | rc=0 >>
10.1.1.133
[root@ansibel ansible]# ansible nginx -m ping
web01 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web02 | SUCCESS => {
"changed": false,
"ping": "pong"
}
4.3 ansible的簡單使用方式
ansible -i /etc/ansible/hosts 主機或主機組 -m 指定模塊 -a 命令
不用-i指定配置文件默認為/etc/ansible/hosts
4.4 使用ping模塊用來查看服務器是否連接正常,ping模塊不需要-a指定參數
ansible all -m ping
主機組,主機,all代表所有
主機和主機組注意事項:
| 主機組范圍 |
解釋 |
| all |
代表所有主機 |
| Web01:web02 |
可以指定多台主機 |
| all:\!web01 |
指定all但不包含web01,注意!前需要加轉意符號\ |
[root@ansibel ansible]# ansible all -m ping
web01 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web02 | SUCCESS => {
"changed": false,
"ping": "pong"
}
五,ansible的三個命令模塊
5.1 ansible模塊command(不支持管道,不建議使用)
#command支持直接回顯命令的執行結果
[root@ansibel ansible]# ansible all -m command -a "pwd"
web01 | CHANGED | rc=0 >>
/root
web02 | CHANGED | rc=0 >>
/root
#command模塊不支持管道符操作
[root@ansibel ansible]# ansible all -m command -a "echo test | grep t"
web01 | CHANGED | rc=0 >>
test | grep t
web02 | CHANGED | rc=0 >>
test | grep t
#command模塊不支持重定向操作
[root@ansibel ansible]# ansible all -m command -a "echo bb >> /tmp/testansible"
web01 | CHANGED | rc=0 >>
bb >> /tmp/testansible
web02 | CHANGED | rc=0 >>
bb >> /tmp/testansible
5.2 ansible模塊shell(支持管道,支持重定向)
#shell模塊支持管道符
[root@ansibel ansible]# ansible all -m shell -a "echo test | grep t"
web02 | CHANGED | rc=0 >>
test
web01 | CHANGED | rc=0 >>
test
#shell支持重定向
[root@ansibel ansible]# ansible all -m shell -a "echo bb >> /tmp/testansible"
web01 | CHANGED | rc=0 >>
web02 | CHANGED | rc=0 >>
[root@web01 tmp]# ls
testansible
[root@web01 tmp]# cat testansible
bb
[root@web02 tmp]# ls
testansible
[root@web02 tmp]# cat testansible
bb
如果遇到特殊符號需要加入\轉義,這樣子ansible才能正常運行
[root@ansibel ansible]# ansible web01 -m shell -a "cat /etc/passwd | awk -F":" '{p
rint \$1}' "
web01 | CHANGED | rc=0 >>
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
sshd
postfix
rpc
rpcuser
nfsnobody
5.3 ansible模塊raw,最原始的方式運行命令(不依賴python,僅通過ssh實現)
兩邊都沒有掛光盤,用ansible批量管理給他們一起掛光盤。
[root@ansibel ansible]# ansible all -m raw -a 'mount /dev/sr0 /media/cdrom'
web01 | CHANGED | rc=0 >>
mount: /dev/sr0 is write-protected, mounting read-only
Shared connection to 10.1.1.132 closed.
web02 | CHANGED | rc=0 >>
mount: /dev/sr0 is write-protected, mounting read-only
Shared connection to 10.1.1.133 closed.

用ansible批量搭建yum倉庫
[root@ansibel ansible]# ansible all -m raw -a 'mv /etc/yum.repos.d/* /tmp/'
web01 | CHANGED | rc=0 >>
Shared connection to 10.1.1.132 closed.
web02 | CHANGED | rc=0 >>
Shared connection to 10.1.1.133 closed.
[root@ansibel ansible]# ansible all -m copy -a 'src=/etc/yum.repos.d/CentOS-Media.
repo dest=/etc/yum.repos.d/'
.............中間信息略..................................
測試安裝nmap


[root@ansibel ansible]# ansible all -m shell -a 'yum -y install nmap'


六,ansible的copy模塊批量下發文件或文件夾
6.1 copy模塊概述
copy模塊的參數,ansible 主機組 -m 模塊 -a 命令
- src:指定源文件或目錄
- dest:指定目標服務器的文件或目錄
- backup:是否要備份
- owner:拷貝到目標服務器后,文件或目錄的所屬用戶
- group:拷貝到目標服務器后,文件或目錄的所屬群組
- mode:文件或目錄的權限
6.2 copy模塊拷貝文件
特別提示:如果目標路徑不存在會自動創建
src===>源文件路徑 dest=目標路徑位置
[root@ansibel ansible]# cd /tmp
[root@ansibel tmp]# ls
[root@ansibel tmp]# echo "aaa" >> test
[root@ansibel tmp]# cat test
aaa
[root@ansibel ansible]# ansible all -m copy -a 'src=/tmp/test dest=/tmp/'


6.3 copy模塊拷貝文件夾
特別提示:
如果目標路徑里有與我拷貝的文件同名文件的話,會直接覆蓋目標路徑下的文件
[root@ansible ~]# mkdir -p /service/scripts
[root@ansible ~]# echo "aaa" > /service/scripts/test.txt
[root@ansible ~]# echo "bbb" > /service/scripts/test2.txt
#拷貝/service/scripts/ 目錄下所有內容到dest的路徑下(注意兩條命令的對比)
[root@ansible ~]# ansible web01 -m copy -a "src=/service/scripts/ dest=/service/sc
ripts/"
web01 | CHANGED => {
"changed": true,
"dest": "/service/scripts/",
"src": "/service/scripts/"
}

#拷貝/service/scripts目錄本身及其內部的所有內容到dest的路徑下(注意兩條命令的對比)
[root@ansible ~]# ansible web02 -m copy -a "src=/service/scripts dest=/service/scr
ipts/"
web02 | CHANGED => {
"changed": true,
"dest": "/service/scripts/",
"src": "/service/scripts"
}

6.4 copy模塊自動備份
特別提示:
參數:backup=yes ===>意思是,如果目標路徑下,有與我同名但不同內容的文件時,在覆蓋前,對目標文件先進行備份。
[root@ansible ansible]# echo "bbb" >> /tmp/test
[root@ansible ansible]# cat /tmp/test
aaa
bbb
[root@ansible ansible]# ansible all -m copy -a 'src=/tmp/test dest=/tmp/ backup=ye
s'


6.5 copy模塊指定用戶和屬主,權限。
[root@ansible ansible]# ansible web01 -m copy -a 'src=/tmp/test dest=/tmp/ owner=n
obody group=nobody mode=0600'

七,ansible的script模塊批量運行腳本
ansible的script模塊能夠實現遠程服務器批量運行本地的shell腳本
操作示例-->遠程批量分發並自動部署nginx,所有被管理端需要掛載光盤,並創建本地yum配置文件
[root@ansible scripts]# pwd
/service/scripts
[root@ansible scripts]# vim auto_nginx.sh
[root@ansible scripts]# cat auto_nginx.sh
#!/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-d
evel &>/dev/nulltest -d /service/scripts || exit 3
cd /service/scripts/
tar xf nginx-1.10.2.tar.gz -C /usr/src/
cd /usr/src/nginx-1.10.2/
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_stat
us_module &>/dev/nullmake &>/dev/null
make install &>/dev/null
exit 0
[root@ansible scripts]# vim fenfa.sh
[root@ansible scripts]# cat fenfa.sh
#!/bin/sh
Group=$1
ansible $Group -m copy -a "src=/service/scripts/ dest=/service/scripts/"
ansible $Group -m script -a "/service/scripts/auto_nginx.sh"
[root@ansible scripts]# ls
auto_nginx.sh fenfa.sh nginx-1.10.2.tar.gz
注:auto_nginx.sh #自動安裝nginx腳本
fenfa.sh #批量分發腳本
nginx-1.10.2.tar.gz #nginx源碼包
激活腳本
[root@ansible scripts]# sh fenfa.sh all


此腳本只是個演示示例,工作中需要寫的盡量嚴謹一些。
八,ansible-playbook的初步使用
playbook的使用,playbook可以把ansible的模塊進行組合
[root@ansible scripts]# ln -s /usr/local/python/bin/ansible-playbook /usr/local/bin
[root@ansible scripts]# which ansible-playbook
/usr/local/bin/ansible-playbook
ansible-playbook劇本,可以像拍戲一樣把各個模塊編成一個故事,設定一個劇本,什么情況下就去執行什么樣的,甚至還可以吧if或者else的判斷加進去。還能對他的某些結果進行判斷。劇本用yaml結尾
用playbook執行shell模塊
[root@ansible scripts]# mkdir bak
[root@ansible scripts]# mv *.sh bak/
[root@ansible scripts]# mv *.gz bak/
[root@ansible scripts]# ls
bak
8.1 playbook的簡單shell模塊的使用
黃色執行成功對對方電腦做出改變,綠色執行成功,不作出改變,紅色失敗。
模板說明:--- #開頭必須有三個小-,頂格寫- hosts: #正文配置代碼的第一級,必須有兩個空格(-占一個空格位)- host: web01 #web01是host參數的值,值和hosts:之間要有一個空格tasks: #tasks:表示接下來要執行的具體任務- name: #相對於tasks再多縮進兩個格(-占一個空格位),表示屬於tasks的下一級- name: test #test只是要執行的具體命令的名字可以隨便寫。name:后還是有一個空格要注意shell: #表示調用shell模塊執行命令相對於tasks仍舊要多縮進兩個空格shell: echo "xxx" >> xxx #shell:后邊還是要有個空格,需要注意。
[root@ansible scripts]# vim test_shell.yaml
[root@ansible scripts]# cat test_shell.yaml
---
- hosts: web01
tasks:
- name: test
shell: echo "welcome to yunjisaun" >> /tmp/username
- name: test2
shell: echo "welcome to yunjisuan" >> /tmp/username
執行playbook配置文件
[root@ansible scripts]# ansible-playbook test_shell.yaml

8.2 playbook的簡單copy模塊的使用


[root@ansible scripts]# echo "welcom to yunjisuan" >> /tmp/test_copy
[root@ansible scripts]# vim test_copy.yaml
[root@ansible scripts]# cat test_copy.yaml
---
- hosts: all
tasks:
- name: test copy
copy: src=/tmp/test_copy dest=/tmp/
[root@ansible scripts]# ansible-playbook test_copy.yaml

8.3 playbook使用register輸出命令運行結果
我們在用playbook進行ansible模塊操作的時候,並沒有命令的執行結果輸出,默認被隱藏了。
我們可以通過register模塊最加輸出命令的執行結果
[root@ansible scripts]# vim test_register.yaml
[root@ansible scripts]# cat test_register.yaml
---
- hosts: all
tasks:
- name: test register
shell: echo "welcome to yunjisuan"
register: print_result
- debug: var=print_result
register: print_result #將之前命令的輸出結果保存在變量print_result里,變量名隨便取。
- debug: var=print_result #將變量的值作為debug輸出出來。var是固定的。調用debug模塊·
Stdout標准屏幕輸出。
[root@ansible scripts]# ansible-playbook test_register.yaml
PLAY [all] ***********************************************************************
TASK [Gathering Facts] ***********************************************************
ok: [web01]
ok: [web02]
TASK [test register] *************************************************************
changed: [web02]
changed: [web01]
TASK [debug] *********************************************************************
ok: [web01] => {
"print_result": {
"changed": true,
"cmd": "echo \"welcome to yunjisuan\"",
"delta": "0:00:00.004567",
"end": "2018-12-26 01:00:34.160829",
"failed": false,
"rc": 0,
"start": "2018-12-26 01:00:34.156262",
"stderr": "",
"stderr_lines": [],
"stdout": "welcome to yunjisuan",
"stdout_lines": [
"welcome to yunjisuan" #命令的執行結果有輸出了
]
}
}
ok: [web02] => {
"print_result": {
"changed": true,
"cmd": "echo \"welcome to yunjisuan\"",
"delta": "0:00:00.004002",
"end": "2018-12-26 01:00:34.146846",
"failed": false,
"rc": 0,
"start": "2018-12-26 01:00:34.142844",
"stderr": "",
"stderr_lines": [],
"stdout": "welcome to yunjisuan",
"stdout_lines": [
"welcome to yunjisuan" #命令的執行結果有輸出了
]
}
}
PLAY RECAP ***********************************************************************
web01 : ok=3 changed=1 unreachable=0 failed=0
web02 : ok=3 changed=1 unreachable=0 failed=0
8.4 nginx配置下發並檢測
[root@ansible scripts]# vim test_nginx_conf.yaml
[root@ansible scripts]# cat 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
九,playbook的自定義變量和內置變量
9.1 在Playbook中使用自定義變量
[root@ansible scripts]# vim test_vars.yaml
[root@ansible scripts]# cat test_vars.yaml
---
- 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
特別提示:引用變量需要在雙引號中引用。
[root@ansible scripts]# ansible-playbook test_vars.yaml
[WARNING]: Found variable using reserved name: name #這里提示,name是一個保留的內置變量,我們在自定義時不能用
................................中間信息略...........................................
有警告是因為自定義變量和系統的內置保留變量同名了,在使用自定義變量時,我們要特別注意不要和系統的內置保留變量同名,容易引發問題。
修改一下name這個變量再發送,就不會出警告了。
在使用自定義變量時,我們要特別注意不要和系統的內置保留變量同名,容易引發問題。
[root@ansible scripts]# vim test_vars.yaml
[root@ansible scripts]# cat test_vars.yaml
---
- hosts: all
vars:
- Name: "yunjisuan"
age: "3"
tasks:
- name: "{{ Name }}"
shell: echo "myname {{ Name }},myage {{ age }}"
register: var_result
- debug: var=var_result
[root@ansible scripts]# ansible-playbook test_vars.yaml 修改過之后就沒有警告了
9.2 在playbook中使用ansible內置變量
我們可以使用ansible all -m setup | less查看ansible內置變量
ansible 127.0.0.1 -m setup | less 看自己的ansible內置變量。
[root@ansible scripts]# vim test_setupvars.yaml
[root@ansible scripts]# cat test_setupvars.yaml
---
- hosts: all
gather_facts: True #使用ansible內置變量
tasks:
- name: setup var
shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_c
ount }}" register: var_result
- debug: var=var_result
通過使用ansible的內置變量可以批量取服務器的許多內置信息
[root@ansible scripts]# ansible-playbook test_setupvars.yaml
PLAY [all] ***********************************************************************
TASK [Gathering Facts] ***********************************************************
ok: [web01]
ok: [web02]
TASK [setup var] *****************************************************************
changed: [web02]
changed: [web01]
TASK [debug] *********************************************************************
ok: [web01] => {
"var_result": {
"changed": true,
"cmd": "echo \"ip 10.1.1.132 cpu 1\"",
"delta": "0:00:00.004011",
"end": "2018-12-26 01:16:06.857398",
"failed": false,
"rc": 0,
"start": "2018-12-26 01:16:06.853387",
"stderr": "",
"stderr_lines": [],
"stdout": "ip 10.1.1.132 cpu 1",
"stdout_lines": [
"ip 10.1.1.132 cpu 1" #信息
]
}
}
ok: [web02] => {
"var_result": {
"changed": true,
"cmd": "echo \"ip 10.1.1.133 cpu 1\"",
"delta": "0:00:00.003761",
"end": "2018-12-26 01:16:06.835669",
"failed": false,
"rc": 0,
"start": "2018-12-26 01:16:06.831908",
"stderr": "",
"stderr_lines": [],
"stdout": "ip 10.1.1.133 cpu 1",
"stdout_lines": [
"ip 10.1.1.133 cpu 1" #信息
]
}
}
PLAY RECAP ***********************************************************************
web01 : ok=3 changed=1 unreachable=0 failed=0
web02 : ok=3 changed=1 unreachable=0 failed=0
簡單演示一下ansible內置變量的取用方法ansible all -m setup | less
[root@ansible scripts]# vim test_setupvars.yaml
[root@ansible scripts]# cat test_setupvars.yaml
---
- hosts: all
gather_facts: True
tasks:
- name: setup var
shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_c
ount }}" >> /tmp/test - name: setup var2
shell: echo "time {{ ansible_date_time["date"] }}" >> /tmp/test
register: var_result
- debug: var=var_result
[root@ansible scripts]# vim test_setupvars.yaml
[root@ansible scripts]# cat test_setupvars.yaml
---
- hosts: all
gather_facts: True
tasks:
- name: setup var
shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_c
ount }}" >> /tmp/test - name: setup var2
shell: echo "time {{ ansible_date_time["date"] }}" >> /tmp/test
register: var_result
- debug: var=var_result
[root@ansible scripts]# vim test_setupvars.yaml
[root@ansible scripts]# cat test_setupvars.yaml
---
- hosts: all
gather_facts: True
tasks:
- name: setup var
shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_c
ount }}" >> /tmp/test - name: setup var2
shell: echo "time {{ ansible_date_time["date"] }}" >> /tmp/test
register: var_result
- debug: var=var_result
[root@ansible scripts]# ansible-playbook test_setupvars.yaml

十,Playbook下發可變配置文件
配置文件如果使用copy模塊去下發的話,那配置都是一樣的;
如果下發的配置文件里有可變的配置,需要用到template模塊。
10.1 利用template模塊下發可變的配置文件
copy模塊分發的是不能變的模塊
eg:變量原封不動沒有改變。copy模塊識別不了變量。
[root@ansible scripts]# vim /tmp/test
[root@ansible scripts]# cat /tmp/test
my name is {{ myname }} #自定義變量
my name is {{ ansible_all_ipv4_addresses[0] }} #系統變量
[root@ansible scripts]# ansible web01 -m copy -a 'src=/tmp/test dest=/tmp/'

利用template模塊下發可變的配置文件
[root@ansible scripts]# vim test_filevars.yaml
[root@ansible scripts]# cat test_filevars.yaml
---
- hosts: all
gather_facts: True #開啟系統變量
vars:
- myname: "yunjisuan" #自定義變量
tasks:
- name: template test
template: src=/tmp/test dest=/root/test #使用template下發可變配置文件
[root@ansible scripts]# ansible-playbook test_filevars.yaml 

10.2 下發配置文件里面使用判斷語法
[root@ansible scripts]# vim /tmp/if.j2
[root@ansible scripts]# cat /tmp/if.j2
{% if PORT %} #if PORT存在
ip=0.0.0.0:{{ PORT }}
{% else %} #否則的話
ip=0.0.0.0:80
{% endif %} #結尾
[root@ansible scripts]# vim test_ifvars.yaml
[root@ansible scripts]# cat test_ifvars.yaml
---
- hosts: all
gather_facts: True #開啟系統內置變量
vars:
- PORT: 90 #自定義變量
tasks:
- name: jinja2 if test
template: src=/tmp/if.j2 dest=/root/test
[root@ansible scripts]# ansible-playbook test_ifvars.yaml

如果我們將變量PORT值為空的話,就會是另外的結果
[root@ansible scripts]# vim test_ifvars.yaml
[root@ansible scripts]# cat test_ifvars.yaml
---
- hosts: all
gather_facts: True
vars:
- PORT:
tasks:
- name: jinja2 if test
template: src=/tmp/if.j2 dest=/root/test
[root@ansible scripts]# ansible-playbook test_ifvars.yaml


十一,Playbook的notify通知和下發nginx配置
#實戰下發可執行動作的可變的nginx配置文件
先把兩邊nginx服務開啟


[root@ansible scripts]# vim test_nginxvars.yaml
[root@ansible scripts]# cat test_nginxvars.yaml
---
- hosts: all
gather_facts: True
vars:
- myname: yunjisuan
tasks:
- name: nginx conf
template: src=/tmp/test dest=/root/test
notify:
- stop nginx
handlers:
- name: reload nginx
shell: /usr/local/nginx/sbin/nginx -s reload
- name: stop nginx
shell: /usr/local/nginx/sbin/nginx -s stop
[root@ansible scripts]# ansible-playbook test_nginxvars.yaml
服務發生改變了。


把服務再次啟動,再發就不會發生改變了。


[root@ansible scripts]# ansible-playbook test_nginxvars.yaml


