前面介紹過 Ansible 常用模組,熟悉這些模組是打底的基本功。接下來我們透過一個實作案例來認識 playbook——它把這些基礎模組靈活組合起來,就能完成相當複雜的業務場景。
先釐清 playbook 裡幾個常見概念:
vars:Ansible 的變數,用來重用某些定義值,主要會在模板中派上用場。
tasks:任務,就是依設定檔的定義去執行裡面的操作。
handlers:當被操作主機的設定發生改變時才觸發的動作。它需要被「通知」,然後 handlers 定義的動作才會執行;本質上就是一份 task 清單,與前述 tasks 並無根本差異。
roles:role 能把任務等模組化,讓复用性更強。
下面我們以「一鍵安裝 LNMP 環境並部署 WordPress 部落格」為例來學習(取自 GitHub 上的範例,我們稍作修改即可)。
[root@ansible wordpress-nginx]# tree . ├── group_vars │ ├── all │ └── all.0 ├── hosts ├── LICENSE.md ├── README.md ├── roles │ ├── common │ │ ├── files │ │ │ ├── epel.repo │ │ │ ├── iptables-save │ │ │ └── RPM-GPG-KEY-EPEL-6 │ │ ├── handlers │ │ │ └── main.yml │ │ └── tasks │ │ └── main.yml │ ├── mysql │ │ ├── handlers │ │ │ └── main.yml │ │ ├── tasks │ │ │ └── main.yml │ │ └── templates │ │ └── my.cnf.j2 │ ├── nginx │ │ ├── handlers │ │ │ └── main.yml │ │ ├── tasks │ │ │ └── main.yml │ │ └── templates │ │ └── default.conf │ ├── php-fpm │ │ ├── handlers │ │ │ └── main.yml │ │ ├── tasks │ │ │ └── main.yml │ │ └── templates │ │ └── wordpress.conf │ └── wordpress │ ├── tasks │ │ └── main.yml │ └── templates │ └── wp-config.php ├── site.retry └── site.yml 21 directories, 23 files
以上是整體目錄結構,其中 site.yml 是總入口。我們挑 mysql 部分來看,其它大同小異:
[root@ansible wordpress-nginx]# cat site.yml
- name: Install WordPress, MySQL, Nginx, and PHP-FPM
hosts: wordpress
remote_user: root
roles:
- common
- mysql
- nginx
- php-fpm
- wordpress
[root@ansible wordpress-nginx]# cat hosts
[wordpress]
172.16.80.127
[root@ansible wordpress-nginx]# cat roles/mysql/handlers/main.yml
---
- name: restart mysql
service: name=mysqld state=restarted
[root@ansible wordpress-nginx]# cat roles/mysql/tasks/main.yml
---
- name: Install Mysql package
yum: name={{ item }} state=present
with_items:
- mysql-server
- MySQL-python
- libsemanage-python
# with_items: 代表下面列出的變量都會傳到 item 裡面
- name: Create Mysql configuration file
template: src=my.cnf.j2 dest=/etc/my.cnf
notify:
- restart mysql
- name: Start Mysql Service
service: name=mysqld state=started enabled=yes
[root@ansible wordpress-nginx]# cat roles/mysql/templates/my.cnf.j2
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
port={{ mysql_port }}
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
來實際跑一遍這個配置:
[root@ansible wordpress-nginx]# ansible-playbook -i hosts site.yml
PLAY [Install WordPress, MySQL, Nginx, and PHP-FPM] ****************************
TASK [setup] *******************************************************************
ok: [172.16.80.127]
TASK [common : Copy the EPEL repository definition] ****************************
ok: [172.16.80.127]
TASK [common : Create the GPG key for EPEL] ************************************
ok: [172.16.80.127]
TASK [common : Set up iptables rules] ******************************************
ok: [172.16.80.127]
TASK [mysql : Install Mysql package] *******************************************
ok: [172.16.80.127] => (item=[u'mysql-server', u'MySQL-python', u'libsemanage-python'])
TASK [mysql : Create Mysql configuration file] *********************************
ok: [172.16.80.127]
TASK [mysql : Start Mysql Service] *********************************************
ok: [172.16.80.127]
TASK [nginx : Install nginx] ***************************************************
ok: [172.16.80.127]
TASK [nginx : Copy nginx configuration for wordpress] **************************
changed: [172.16.80.127]
TASK [php-fpm : Install php-fpm and deps] **************************************
ok: [172.16.80.127] => (item=[u'php', u'php-fpm', u'php-enchant', u'php-IDNA_Convert', u'php-mbstring', u'php-mysql', u'php-PHPMailer', u'php-process', u'php-simplepie', u'php-xml'])
TASK [php-fpm : Disable default pool] ******************************************
ok: [172.16.80.127]
TASK [php-fpm : Copy php-fpm configuration] ************************************
ok: [172.16.80.127]
TASK [wordpress : Download WordPress] ******************************************
changed: [172.16.80.127]
TASK [wordpress : Extract archive] *********************************************
changed: [172.16.80.127]
[WARNING]: Consider using unarchive module rather than running tar
TASK [wordpress : Add group "wordpress"] ***************************************
changed: [172.16.80.127]
TASK [wordpress : Add user "wordpress"] ****************************************
changed: [172.16.80.127]
TASK [wordpress : Fetch random salts for WordPress config] *********************
changed: [172.16.80.127 -> localhost]
[WARNING]: Consider using get_url or uri module rather than running curl
TASK [wordpress : Create WordPress database] ***********************************
changed: [172.16.80.127]
TASK [wordpress : Create WordPress database user] ******************************
changed: [172.16.80.127]
TASK [wordpress : Copy WordPress config file] **********************************
changed: [172.16.80.127]
TASK [wordpress : Change ownership of WordPress installation] ******************
changed: [172.16.80.127]
TASK [wordpress : Start php-fpm Service] ***************************************
changed: [172.16.80.127]
RUNNING HANDLER [nginx : restart nginx] ****************************************
changed: [172.16.80.127]
PLAY RECAP *********************************************************************
172.16.80.127 : ok=23 changed=12 unreachable=0 failed=0
看看執行結果的截圖:
最後打開瀏覽器訪問 https://172.16.80.127,會自動跳轉到安裝畫面 https://172.16.80.127/wp-admin/install.php。
本文出自「厚德載物」部落格,謝絕轉載!