24.15 ansible介紹php
不須要安裝客戶端,經過 sshd 去通訊html
基於模塊工做,模塊能夠由任何語言開發python
不只支持命令行使用模塊,也支持編寫 yaml 格式的 playbook,易於編寫和閱讀mysql
安裝十分簡單,centos 上可直接 yum 安裝linux
有提供UI(瀏覽器圖形化)www.ansible.com/tower,收費的nginx
官方文檔 http://docs.ansible.com/ansible/latest/index.htmlgit
ansible 已經被 redhat 公司收購,它在 github 上是一個很是受歡迎的開源軟件,github 地址https://github.com/ansible/ansiblegithub
一本不錯的入門電子書https://ansible-book.gitbooks.io/ansible-first-book/sql
24.16 ansible安裝shell
一、準備兩臺機器,arslinux-01,arslinux-02
二、在 arslinux-01 上安裝 ansible
[root@arslinux-01 ~]# yum list|grep ansible
[root@arslinux-01 ~]# yum install -y ansible ansible-doc
三、在 arslinux-01 上生成密鑰對
[root@arslinux-01 ~]# ssh-keygen -t rsa
若是 /root/.ssh/ 下有 id_rsa.pub 則不須要生成密鑰對
四、將公鑰放到 arslinux-01,arslinux-02 上的 /root/.ssh/authorized_keys 中
五、驗證鏈接
[root@arslinux-01 ~]# ssh 192.168.194.132
Last login: Sun Aug 4 21:08:02 2019 from 192.168.194.1
六、配置主機組
[root@arslinux-01 ~]# vim /etc/ansible/hosts
[testhost]
127.0.0.1
192.168.194.132
說明: testhost 爲主機組名字,自定義的。 下面兩個 ip 爲組內的機器 ip
24.17 ansible 遠程執行命令
ansible testhost -m command -a '命令' 批量遠程命令
這裏的 testhost 爲主機組名,-m 後邊是模塊名字,-a 後面是命令。固然咱們也能夠直接寫一個 ip,針對某一臺機器來執行命令
[root@arslinux-01 ~]# ansible testhost -m command -a 'w'
127.0.0.1 | CHANGED | rc=0 >>
21:38:20 up 1:11, 3 users, load average: 0.25, 0.14, 0.15
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.194.1 117月19 24days 0.05s 0.05s -bash
root pts/1 192.168.194.1 21:07 4.00s 2.63s 0.00s ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/6220ae23ea -tt arslinux-02 /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1564925899.21-98869728293746/AnsiballZ_command.py && sleep 0'
root pts/4 localhost 21:38 0.00s 0.25s 0.01s w
arslinux-02 | CHANGED | rc=0 >>
21:38:21 up 3:17, 3 users, load average: 0.08, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.194.1 146月19 50days 0.04s 0.04s -bash
root pts/1 192.168.194.1 21:08 53.00s 0.04s 0.04s -bash
root pts/2 arslinux-01 21:38 1.00s 0.32s 0.01s w
[root@arslinux-01 ~]# ansible 192.168.194.132 -m command -a 'w'
arslinux-02 | CHANGED | rc=0 >>
21:38:52 up 3:18, 3 users, load average: 0.05, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.194.1 146月19 50days 0.04s 0.04s -bash
root pts/1 192.168.194.1 21:08 1:24 0.04s 0.04s -bash
root pts/2 arslinux-01 21:38 1.00s 0.43s 0.01s w
錯誤: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
解決: yum install -y libselinux-python
ansible testhost -m shell -a '命令' shell 模塊一樣能夠實現遠程執行的命令
[root@arslinux-01 ~]# ansible testhost -m shell -a 'hostname'
arslinux-02 | CHANGED | rc=0 >>
arslinux-02
127.0.0.1 | CHANGED | rc=0 >>
arslinux-01
24.18 ansible拷貝文件或目錄
ansible arslinux-02 -m copy -a 'src= dest= owner= group= mode= ' 拷貝目錄或文件
[root@arslinux-01 ~]# ansible 192.168.194.132 -m copy -a "src=/etc/ansible dest=/tmp/ansible_test owner=root group=root mode=0755"
192.168.194.132 | CHANGED => {
"changed": true,
"dest": "/tmp/ansible_test/",
"src": "/etc/ansible"
}
[root@arslinux-02 ~]# ll -d /tmp/ansible_test/
drwxr-xr-x 3 root root 21 8月 4 22:02 /tmp/ansible_test/
[root@arslinux-02 ~]# date
2019年 08月 04日 星期日 22:03:09 CST
注意:源目錄會放到目標目錄下面去,若是目標指定的目錄不存在,它會自動建立。
若是拷貝的是文件,dest 指定的名字和源若是不一樣,而且它不是已經存在的目錄,至關於拷貝過去後又重命名。但相反,若是 desc 是目標機器上已經存在的目錄,則會直接把文件拷貝到該目錄下面
針對文件操做
[root@arslinux-01 ~]# ansible 192.168.194.132 -m copy -a "src=/etc/passwd dest=/tmp/123 owner=root group=root mode=0755"
192.168.194.132 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "66cfbbd6ccbbfb5edb8b3d364df81d2d9ce9e619",
"dest": "/tmp/123",
"gid": 0,
"group": "root",
"md5sum": "d5a72a116f1f47476e3156915f62972e",
"mode": "0755",
"owner": "root",
"size": 1776,
"src": "/root/.ansible/tmp/ansible-tmp-1564927633.07-72798416414339/source",
"state": "file",
"uid": 0
}
[root@arslinux-02 ~]# ll /tmp/123
-rwxr-xr-x 1 root root 1776 8月 4 22:07 /tmp/123
[root@arslinux-02 ~]# tail -3 /tmp/123
pure-ftp:x:1020:1020::/home/pure-ftp:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
zabbix:x:997:994:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
這裏的/tmp/123和源機器上的/etc/passwd是一致的,但若是目標機器上已經有/tmp/123目錄,則會再/tmp/123目錄下面創建passwd文件
24.19 ansible遠程執行腳本
一、建立一個腳本
[root@arslinux-01 ~]# vim /tmp/test.sh
#!/bin/bash
echo `date` > /tmp/ansible_test.txt
二、分發腳本
[root@arslinux-01 ~]# ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
192.168.194.132 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "b70386033f7568a51de8209c2065dcbd917ca4b1",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"md5sum": "6da17d4e84617796e1b3c7bfdd083d93",
"mode": "0755",
"owner": "root",
"size": 49,
"src": "/root/.ansible/tmp/ansible-tmp-1564928697.25-67620899139563/source",
"state": "file",
"uid": 0
}
127.0.0.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "b70386033f7568a51de8209c2065dcbd917ca4b1",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/test.sh",
"size": 49,
"state": "file",
"uid": 0
}
三、執行腳本
[root@arslinux-01 ~]# ansible testhost -m shell -a "/tmp/test.sh"
192.168.194.132 | CHANGED | rc=0 >>
127.0.0.1 | CHANGED | rc=0 >>
[root@arslinux-02 ~]# ll /tmp/
總用量 8
-rwxr-xr-x 1 root root 1776 8月 4 22:07 123
drwxr-xr-x 3 root root 21 8月 4 22:02 ansible_test
-rwxr-xr-x 1 root root 49 8月 4 22:24 test.sh
[root@arslinux-02 ~]# date
2019年 08月 04日 星期日 22:26:22 CST
腳本須要 755 權限,若是不是 755 權限,執行不了
四、shell 模塊,還支持遠程執行命令而且帶管道,而 command 不支持
[root@arslinux-01 ~]# ansible testhost -m command -a "cat /etc/passwd |wc -l"
192.168.194.132 | FAILED | rc=1 >>
cat:無效選項 -- l
Try 'cat --help' for more information.non-zero return code
127.0.0.1 | FAILED | rc=1 >>
cat:無效選項 -- l
Try 'cat --help' for more information.non-zero return code
[root@arslinux-01 ~]# ansible testhost -m shell -a "cat /etc/passwd |wc -l"
192.168.194.132 | CHANGED | rc=0 >>
25
127.0.0.1 | CHANGED | rc=0 >>
37
ansible 須要先將腳本寫好並分發到各機器上,而後在批量執行腳本
saltstack 則能夠批量遠程執行腳本,不須要分發
24.20 ansible管理任務計劃
ansible 組名/ip/機器名 -m cron -a "name=' ' job=' ' weekday= " 遠程管理任務計劃
[root@arslinux-01 ~]# ansible 192.168.194.132 -m cron -a "name='test cron' job='/bin/touch /tmp/1234546.txt' weekday=6"
192.168.194.132 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"test cron"
]
}
[root@arslinux-02 ~]# crontab -l
#Ansible: test cron
* * * * 6 /bin/touch /tmp/1234546.txt
說明:cron 是模塊;name 自定義 crontab任務的名稱;job 指的是任務;weekday 指每週幾
其餘的時間表示:分鐘 minute 小時 hour 日期 day 月份 month
ansible 組名/ip/機器名 -m cron -a "name=' ' state=absent" 刪除 cron
[root@arslinux-01 ~]# ansible testhost -m cron -a "name='test cron' state=absent"
192.168.194.132 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": []
}
[root@arslinux-02 ~]# crontab -l
[root@arslinux-02 ~]#
24.21 ansible安裝包和管理服務
ansible 組名/ip/機器名 -m yum -a "name=包名" 遠程 yum 安裝包
ansible 組名/ip/機器名 -m yum -a "name=包名 state=removed" 遠程卸載
[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd"
[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd state=removed"
從新安裝、啓動並設置開機啓動
[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd state=remove state=installed"
ansible 組名/ip/機器名 -m service -a "name= state= enabled= " 啓動服務並設開機啓動
[root@arslinux-01 ~]# ansible 192.168.194.132 -m service -a "name=httpd state=started enabled=no"
[root@arslinux-02 ~]# ps aux|grep httpd
root 11746 0.3 0.5 224052 5000 ? Ss 23:07 0:00 /usr/sbin/httpd -DFOREGROUND
apache 11747 0.0 0.2 224052 2948 ? S 23:07 0:00 /usr/sbin/httpd -DFOREGROUND
apache 11749 0.0 0.2 224052 2948 ? S 23:07 0:00 /usr/sbin/httpd -DFOREGROUND
apache 11750 0.0 0.2 224052 2948 ? S 23:07 0:00 /usr/sbin/httpd -DFOREGROUND
apache 11751 0.0 0.2 224052 2948 ? S 23:07 0:00 /usr/sbin/httpd -DFOREGROUND
apache 11752 0.0 0.2 224052 2948 ? S 23:07 0:00 /usr/sbin/httpd -DFOREGROUND
root 11769 0.0 0.0 112724 988 pts/1 R+ 23:07 0:00 grep --color=auto httpd
[root@arslinux-02 ~]# date
2019年 08月 04日 星期日 23:07:43 CST
說明:name 是服務名稱;state 是操做、狀態;enabled 指是否開機啓動
Ansible文檔的使用
ansible-doc -l 列出全部的模塊
ansible-doc cron 查看指定模塊的文檔
24.22 使用 ansible playbook
Ansible playbook 至關於把模塊寫入到配置文件裏面
一、編輯 test.yml 文件
[root@arslinux-01 ansible]# vim test.yml
---
- hosts: 192.168.194.132
remote_user: root
tasks:
- name: test_playbook
shell: touch /tmp/arsenal4life.txt
說明: 第一行須要有三個槓,hosts 參數指定了對哪些主機進行參做,若是是多臺機器能夠用逗號做爲分隔,也可使用主機組,在 /etc/ansible/hosts 裏定義;
user 參數指定了使用什麼用戶登陸遠程主機操做;
tasks 指定了一個任務,其下面的 name 參數一樣是對任務的描述,在執行過程當中會打印出來,shell是 ansible 模塊名字
二、用 ansible-playbook 執行
[root@arslinux-01 ansible]# ansible-playbook test.yml
PLAY [192.168.194.132] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************
ok: [192.168.194.132]
TASK [test_playbook] ***********************************************************************************************************
[WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because
file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid
of this message.
changed: [192.168.194.132]
PLAY RECAP *********************************************************************************************************************
192.168.194.132 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@arslinux-01 ansible]#
三、查看
[root@arslinux-02 ~]# ll /tmp/arsenal4life.txt
-rw-r--r-- 1 root root 0 8月 5 20:57 /tmp/arsenal4life.txt
24.23 playbook 裏的變量
建立用戶的 playbook:
一、編輯 create_user.yml
[root@arslinux-01 ansible]# vim create_user.yml
---
- name: create_user
hosts: 192.168.194.132
user: root
gather_facts: false
vars:
- user: "test"
tasks:
- name: create user
user: name="{{ user }}"
說明:name 參數對該 playbook 實現的功能作一個概述,後面執行過程當中,會打印 name 變量的值 ,能夠省略;gather_facts 參數指定了在如下任務部分執行前,是否先執行 setup 模塊獲取主機相關信息,這在後面的 task 會使用到 setup 獲取的信息時用到;vars 參數,指定了變量,這裏指定一個 user 變量,其值爲 test ,須要注意的是,變量值必定要用引號引住;user 提定了調用 user 模塊,name 是 user 模塊裏的一個參數,而增長的用戶名字調用了上面 user 變量的值;{{ }} 定義變量
二、執行
[root@arslinux-01 ansible]# ansible-playbook create_user.yml
PLAY [create_user] *************************************************************************************************************
TASK [create user] *************************************************************************************************************
changed: [192.168.194.132]
PLAY RECAP *********************************************************************************************************************
192.168.194.132 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
三、查看
[root@arslinux-02 ~]# id test
uid=1001(test) gid=1001(test) 組=1001(test)
四、若是用戶已經存在,那麼不會變動
[root@arslinux-01 ansible]# ansible-playbook create_user.yml
PLAY [create_user] *************************************************************************************************************
TASK [create user] *************************************************************************************************************
ok: [192.168.194.132]
PLAY RECAP *********************************************************************************************************************
192.168.194.132 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
24.24 playbook 裏的循環
一、編輯 while.yml
[root@arslinux-01 ansible]# vim while.yml
---
- hosts: 192.168.194.132
user: root
tasks:
- name: change mode for files
file: path=/tmp/{{ item }} state=touch mode=600
with_items:
- 1.txt
- 2.txt
- 3.txt
說明: with_items爲循環的對象
二、執行
[root@arslinux-01 ansible]# ansible-playbook while.yml
PLAY [192.168.194.132] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************
ok: [192.168.194.132]
TASK [change mode for files] ***************************************************************************************************
changed: [192.168.194.132] => (item=1.txt)
changed: [192.168.194.132] => (item=2.txt)
changed: [192.168.194.132] => (item=3.txt)
PLAY RECAP *********************************************************************************************************************
192.168.194.132 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
三、查看
[root@arslinux-02 ~]# ll /tmp/*.txt
-rw------- 1 root root 0 8月 5 21:45 /tmp/1.txt
-rw------- 1 root root 0 8月 5 21:45 /tmp/2.txt
-rw------- 1 root root 0 8月 5 21:45 /tmp/3.txt
-rw-r--r-- 1 root root 43 8月 4 22:28 /tmp/ansible_test.txt
-rw-r--r-- 1 root root 0 8月 5 21:08 /tmp/arsenal4lifeaaa.txt
-rw-r--r-- 1 root root 0 8月 5 20:57 /tmp/arsenal4life.txt
[root@arslinux-02 ~]# date
2019年 08月 05日 星期一 21:46:07 CST
24.25 playbook 裏的條件判斷
一、編輯 when.yml
[root@arslinux-01 ansible]# vim when.yml
---
- hosts: testhost
user: root
gather_facts: True
tasks:
- name: use when
shell: touch /tmp/when.txt
when: ansible_ens33.ipv4.address == "192.168.194.132"
二、執行
[root@arslinux-01 ansible]# ansible-playbook when.yml
PLAY [testhost] ****************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************
ok: [192.168.194.132]
ok: [127.0.0.1]
TASK [use when] ****************************************************************************************************************
skipping: [127.0.0.1]
[WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because
file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid
of this message.
changed: [192.168.194.132]
PLAY RECAP *********************************************************************************************************************
127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
192.168.194.132 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
三、查看
[root@arslinux-02 ~]# ll /tmp/when.txt
-rw-r--r-- 1 root root 0 8月 5 22:06 /tmp/when.txt
[root@arslinux-02 ~]# date
2019年 08月 05日 星期一 22:07:11 CST
24.26 playbook 中的 handlers
一、編輯 handler.yml
[root@arslinux-01 ansible]# vim handler.yml
---
- name: handlers test
hosts: 192.168.194.132
user: root
tasks:
- name: copy file
copy: src=/etc/passwd dest=/tmp/aaabbbccc.txt
notify: test handlers
handlers:
- name: test handlers
shell: echo "111111" >> /tmp/aaabbbccc.txt
二、執行
[root@arslinux-01 ansible]# ansible-playbook handler.yml
PLAY [handlers test] ***********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************
ok: [192.168.194.132]
TASK [copy file] ***************************************************************************************************************
changed: [192.168.194.132]
RUNNING HANDLER [test handlers] ************************************************************************************************
changed: [192.168.194.132]
PLAY RECAP *********************************************************************************************************************
192.168.194.132 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
三、查看
[root@arslinux-02 ~]# tail !$
tail /tmp/aaabbbccc.txt
mysql:x:1009:1009::/home/mysql:/bin/bash
php-fpm:x:1010:1011::/home/php-fpm:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
virftp:x:1011:1012::/home/virftp:/sbin/nologin
pure-ftp:x:1020:1020::/home/pure-ftp:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
zabbix:x:997:994:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
111111
說明:只有 copy 模塊真正執行後,纔會去調用下面的 handlers 相關的操做。也就是說若是 1.txt 和 2.txt 內容是同樣的,並不會去執行 handlers 裏面的 shell 相關命令。 這種比較適合配置文件發生更改後,重啓服務的操做
24.27/24.28 playbook 安裝 nginx
思路:先在一臺機器上編譯安裝好nginx、打包,而後再用ansible去下發
一、建立各級目錄,方便管理
[root@arslinux-01 ansible]# mkdir nginx_install
[root@arslinux-01 ansible]# cd nginx_install
[root@arslinux-01 nginx_install]# mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}
[root@arslinux-01 nginx_install]# ls roles/
common/ install/
[root@arslinux-01 nginx_install]# ls roles/common/
files/ handlers/ meta/ tasks/ templates/ vars/
[root@arslinux-01 nginx_install]# ls roles/install/
files/ handlers/ meta/ tasks/ templates/ vars/
說明:roles 目錄下有兩個角色,common 爲一些準備操做,install 爲安裝nginx的操做。每一個角色下面又有幾個目錄,handlers 下面是當發生改變時要執行的操做,一般用在配置文件發生改變,重啓服務。files 爲安裝時用到的一些文件,meta 爲說明信息,說明角色依賴等信息,tasks 裏面是核心的配置文件,templates 一般存一些配置文件,啓動腳本等模板文件,vars 下爲定義的變量
二、打包 nginx 目錄,不打包配置文件,例如 nginx.conf,vhost 下的配置文件
[root@arslinux-01 local]# tar czvf nginx.tar.gz --exclude "nginx.conf" --exclude "vhost" nginx/
nginx/
nginx/sbin/
nginx/sbin/nginx.old
nginx/sbin/nginx
nginx/conf/
nginx/conf/koi-win
nginx/conf/koi-utf
nginx/conf/win-utf
nginx/conf/mime.types
nginx/conf/mime.types.default
nginx/conf/fastcgi_params
nginx/conf/fastcgi_params.default
nginx/conf/fastcgi.conf
nginx/conf/fastcgi.conf.default
nginx/conf/uwsgi_params
nginx/conf/uwsgi_params.default
nginx/conf/scgi_params
nginx/conf/scgi_params.default
nginx/conf/nginx.conf.default
nginx/conf/nginx.conf.bak
nginx/conf/htpasswd
nginx/conf/aminglinux.key
nginx/conf/aminglinux.csr
nginx/conf/aminglinux.crt
nginx/logs/
nginx/logs/error.log
nginx/logs/access.log
nginx/logs/nginx_error.log
nginx/html/
nginx/html/50x.html
nginx/html/index.html
nginx/html/1.php
nginx/client_body_temp/
nginx/proxy_temp/
nginx/fastcgi_temp/
nginx/uwsgi_temp/
nginx/scgi_temp/
三、將 nginx 各部分拷貝到 ansible 下的對應目錄下
[root@arslinux-01 local]# cp nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/
[root@arslinux-01 local]# cp nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
[root@arslinux-01 local]# cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/
四、定義 common 的 tasks,nginx 是須要一些依賴包的
[root@arslinux-01 local]# cd /etc/ansible/nginx_install/roles/common/
[root@arslinux-01 common]# vim tasks/main.yml
- name: Install initializtion require software
yum: name={{ item }} state=installed
with_items:
- zlib-devel
- pcre-devel
這裏也能夠寫成 yum: name="pcre-devel,zlib-devel" state=installed
五、定義變量
[root@arslinux-01 common]# cd ../install/
[root@arslinux-01 install]# vim vars/main.yml
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx
六、首先要把全部用到的文檔拷貝到目標機器
[root@arslinux-01 install]# vim tasks/copy.yml
- name: Copy Nginx Software
copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root
- name: Uncompression Nginx Software
shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/
- name: Copy Nginx Start Script
template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755
- name: Copy Nginx Config
template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644
說明:src 不用寫絕對路徑,由於會自動在 files 下去找;templates,它是一個模塊功能,與 copy 不一樣的是他的文本文件採用了 jinga2 語法,copy 模塊會到 files 下去找,而 template 模塊會到 template 目錄下去找,不須要單獨定義
七、建立用戶,啓動服務,刪除壓縮包
[root@arslinux-01 install]# vim tasks/install.yml
- name: Create Nginx User
user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin
- name: Start Nginx Service
shell: /etc/init.d/nginx start
- name: Add Boot Start Nginx Service
shell: chkconfig --level 345 nginx on
- name: Delete Nginx compression files
shell: rm -rf /tmp/nginx.tar.gz
說明:變量 nginx_user 在 var 下的 main.yml 中定義;用 chkconfig 來啓動 nginx 而不是 systemctl
八、再建立 main.yml 而且把 copy 和 install 調用
[root@arslinux-01 install]# vim tasks/main.yml
- include: copy.yml
- include: install.yml
說明:能夠將 tasks 目錄下的 copy.yml 和 install.yml 和在一個 yml 文件下,不過度開更便於查看
到此兩個 roles:common 和 install 就定義完成了,接下來要定義一個入口配置文件
九、定義入口配置文件
[root@arslinux-01 install]# vim /etc/ansible/nginx_install/install.yml
---
- hosts: 192.168.194.132
remote_user: root
gather_facts: True
roles:
- common
- install
十、執行操做
[root@arslinux-01 install]# ansible-playbook /etc/ansible/nginx_install/install.yml
PLAY [192.168.194.132] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************
ok: [192.168.194.132]
TASK [common : Install initializtion require software] *************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop to
supply multiple items and specifying `name: "{{ item }}"`, please use `name: ['zlib-devel', 'pcre-devel']` and remove the
loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False
in ansible.cfg.
ok: [192.168.194.132] => (item=[u'zlib-devel', u'pcre-devel'])
TASK [install : Copy Nginx Software] *******************************************************************************************
changed: [192.168.194.132]
TASK [install : Uncompression Nginx Software] **********************************************************************************
[WARNING]: Consider using the unarchive module rather than running 'tar'. If you need to use command because unarchive is
insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this
message.
changed: [192.168.194.132]
TASK [install : Copy Nginx Start Script] ***************************************************************************************
changed: [192.168.194.132]
TASK [install : Copy Nginx Config] *********************************************************************************************
changed: [192.168.194.132]
TASK [install : Create Nginx User] *********************************************************************************************
changed: [192.168.194.132]
TASK [install : Start Nginx Service] *******************************************************************************************
changed: [192.168.194.132]
TASK [install : Add Boot Start Nginx Service] **********************************************************************************
changed: [192.168.194.132]
TASK [install : Delete Nginx compression files] ********************************************************************************
[WARNING]: Consider using the file module with state=absent rather than running 'rm'. If you need to use command because file
is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of
this message.
changed: [192.168.194.132]
PLAY RECAP *********************************************************************************************************************
192.168.194.132 : ok=10 changed=8 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
運行流程參考圖:
十一、查看
[root@arslinux-02 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9877/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6730/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7056/master
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 6741/zabbix_agentd
tcp6 0 0 :::3306 :::* LISTEN 6971/mysqld
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::80 :::* LISTEN 9877/nginx: master
tcp6 0 0 :::22 :::* LISTEN 6730/sshd
tcp6 0 0 ::1:25 :::* LISTEN 7056/master
tcp6 0 0 :::10050 :::* LISTEN 6741/zabbix_agentd
[root@arslinux-02 ~]# ll /etc/init.d/
總用量 56
-rw-r--r--. 1 root root 18281 8月 24 2018 functions
-rwxr-xr-x 1 root root 10592 6月 1 16:54 mysqld
-rwxr-xr-x. 1 root root 4569 8月 24 2018 netconsole
-rwxr-xr-x. 1 root root 7923 8月 24 2018 network
-rwxr-xr-x 1 root root 1141 8月 6 22:39 nginx
-rw-r--r--. 1 root root 1160 10月 31 2018 READM
由於沒有 nginx.conf 未配置虛擬主機,80 端口也就不存在,
總結說明:總入口文件 install.yml 定義對哪臺機器進行操做,能夠定義 role 也就是子目錄、子 playbook 腳本;common 裏面會去找 tasks,tasks 會去找其中的 main.yml,操做都會先去找 tasks;install 裏同樣會去找 tasks,tasks 會去找 main.yml,而 main.yml 又包含兩個同目錄下的 copy.yml 和 install.yml,他們會分別執行
24.29/24.30 playbook 管理配置文件
管理 nginx 配置文件的 playbook
一、建立目錄
[root@arslinux-01 ~]# mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}
[root@arslinux-01 ~]# cd /etc/ansible/nginx_config/
[root@arslinux-01 nginx_config]# ls roles/
new/ old/
說明:其中 new 爲更新時用到的,old 爲回滾時用到的,files 下面爲 nginx.conf 和 vhosts 目錄,handlers 爲重啓 nginx 服務的命令
關於回滾,須要在執行 playbook 以前先備份一下舊的配置,因此對於老配置文件的管理必定要嚴格,千萬不能隨便去修改線上機器的配置,而且要保證 new/files 下面的配置和線上的配置一致
二、把 nginx.conf 和 vhosts 目錄放到 files 目錄下面
[root@arslinux-01 nginx_config]# cd /usr/local/nginx/conf/
[root@arslinux-01 conf]# cp -r nginx.conf vhost/ /etc/ansible/nginx_config/roles/new/files/
三、定義變量
[root@arslinux-01 conf]# vim /etc/ansible/nginx_config/roles/new/vars/main.yml nginx_basedir: /usr/local/nginx
四、定義從新加載 nginx 服務
[root@arslinux-01 conf]# vim /etc/ansible/nginx_config/roles/new/handlers/main.yml - name: restart nginx shell: /etc/init.d/nginx reload
五、定義核心任務(拷貝配置、加載 handlers)
[root@arslinux-01 conf]# vim /etc/ansible/nginx_config/roles/new/tasks/main.yml - name: copy conf file copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.dest }} backup=yes owner=root group=root mode=0644 with_items: - { src: nginx.conf, dest: conf/nginx.conf } - { src: vhost, dest: conf/ } notify: restart nginx
六、定義總入口配置文件
[root@arslinux-01 conf]# vim /etc/ansible/nginx_config/update.yml --- - hosts: 192.168.194.132 user: root roles: - new
七、執行
[root@arslinux-01 conf]# ansible-playbook /etc/ansible/nginx_config/update.yml PLAY [192.168.194.132] ********************************************************************************************************* TASK [Gathering Facts] ********************************************************************************************************* ok: [192.168.194.132] TASK [new : copy conf file] **************************************************************************************************** ok: [192.168.194.132] => (item={u'dest': u'conf/nginx.conf', u'src': u'nginx.conf'}) changed: [192.168.194.132] => (item={u'dest': u'conf/', u'src': u'vhost'}) RUNNING HANDLER [new : restart nginx] ****************************************************************************************** changed: [192.168.194.132] PLAY RECAP ********************************************************************************************************************* 192.168.194.132 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@arslinux-02 ~]# ps aux|grep nginx root 6776 0.0 0.2 46112 2944 ? Ss 20:05 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 8409 0.0 0.4 48600 4020 ? S 20:52 0:00 nginx: worker process nobody 8410 0.0 0.4 48600 4020 ? S 20:52 0:00 nginx: worker process root 8437 0.0 0.0 112724 988 pts/0 R+ 20:54 0:00 grep --color=auto nginx [root@arslinux-02 ~]# date 2019年 08月 07日 星期三 20:53:05 CST
八、更改 vhost 參數,更新後查看
[root@arslinux-01 conf]# echo 1111 >> /etc/ansible/nginx_config/roles/new/files/nginx.conf [root@arslinux-01 conf]# ansible-playbook /etc/ansible/nginx_config/update.yml PLAY [192.168.194.132] ********************************************************************************************************* TASK [Gathering Facts] ********************************************************************************************************* ok: [192.168.194.132] TASK [new : copy conf file] **************************************************************************************************** changed: [192.168.194.132] => (item={u'dest': u'conf/nginx.conf', u'src': u'nginx.conf'}) ok: [192.168.194.132] => (item={u'dest': u'conf/', u'src': u'vhost'}) RUNNING HANDLER [new : restart nginx] ****************************************************************************************** changed: [192.168.194.132] PLAY RECAP ********************************************************************************************************************* 192.168.194.132 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@arslinux-02 ~]# tail -3 /usr/local/nginx/conf/nginx.conf include vhost/*.conf; } 1111
九、回滾操做
回滾操做就是把舊的配置覆蓋,而後從新加載 nginx 服務, 每次改動 nginx 配置文件以前先備份到 old 裏,對應目錄爲 /etc/ansible/nginx_config/roles/old/files,回滾的 backup.yml 對應的 roles 爲 old
1)備份
[root@arslinux-01 conf]# rsync -av /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/
2)定義回滾操做的總入口
[root@arslinux-01 nginx_config]# cp update.yml rollback.yml [root@arslinux-01 nginx_config]# vim rollback.yml --- - hosts: 192.168.194.132 user: root roles: - old
3)模擬更改配置內容
[root@arslinux-01 nginx_config]# echo 21232332 >> /etc/ansible/nginx_config/roles/new/files/nginx.conf [root@arslinux-01 nginx_config]# ansible-playbook update.yml [root@arslinux-02 ~]# tail -3 /usr/local/nginx/conf/nginx.conf } 1111 21232332
4)回滾執行操做
[root@arslinux-01 nginx_config]# ansible-playbook rollback.yml [root@arslinux-02 ~]# tail -3 /usr/local/nginx/conf/nginx.conf include vhost/*.conf; } 1111
總結:每次變動,update 以前先將配置文件備份再更改,改完在 playbook update,有問題能夠回滾
如下是幾個示例,幫助你鞏固playbook的用法
示例: https://blog.51cto.com/215687833/1888534
示例:https://www.cnblogs.com/xuyingzhong/p/8466976.html
示例:http://www.yfshare.vip/2017/03/16/Ansible-Playbooks%E4%B9%8B%E5%AE%89%E8%A3%85Mysql/