roles 用於層次性、結構化地組織playbook。
roles 能夠根據層次型結構自動裝載變數檔案、tasks以及handlers等。
要使用roles只需要在playbook中使用include指令即可。
簡單來講,roles就是通過分別將變數(vars)、檔案(file)、任務(tasks)、模組(modules)及處理器(handlers)放置於單獨的目錄中,並可以便捷地include它們的一種機制。
角色一般用於基於主機構建服務的場景中,但也可以是用於構建守護程式等場景中。
建立 roles 的步驟
(1) 建立以roles命名的目錄;
(2) 在roles目錄中分別建立以各角色名稱命名的目錄,如webservers等。注意:在 roles 必須包括 site.yml檔案,可以為空;
(3) 在每個角色命名的目錄中分別建立files、handlers、meta、tasks、templates和vars目錄;用不到的目錄可以建立為空目錄,也可以不建立;
(4) 在playbook檔案中,呼叫各角色;
roles 內各目錄中可用的檔案
tasks目錄:至少應該包含一個名為main.yml的檔案,其定義了此角色的任務列表;此檔案可以使用include包含其它的位於此目錄中的task檔案;
files目錄:存放由copy或script等模組呼叫的檔案;
templates目錄:template模組會自動在此目錄中尋找Jinja2模板檔案;
handlers目錄:此目錄中應當包含一個main.yml檔案,用於定義此角色用到的各handler;在handler中使用include包含的其它的handler檔案也應該位於此目錄中;
vars目錄:應當包含一個main.yml檔案,用於定義此角色用到的變數;
meta目錄:應當包含一個main.yml檔案,用於定義此角色的特殊設定及其依賴關係;ansible 1.3及其以後的版本才支援;
default目錄:為當前角色設定預設變數時使用此目錄;應當包含一個main.yml檔案。
示例 一 、
分別建立三個 role ,分別為 http mysql php,建立要使用的目錄,一般為全部建立,使用不到的可以為空,
測試主機 1 安裝 httpd
測試主機 2 安裝 mysql
測試主機 3 安裝 http mysql php
1、
建立目錄
[root@node1 ansible_playbooks]# pwd /opt/ansible_playbooks [root@node1 ansible_playbooks]# ls hosts [root@node1 ansible_playbooks]# mkdir -pv roles/{http,mysql,php}/{tasks,handlers,files,vars,templates,meta,default}
2、測試主機為:
[root@node1 ansible_playbooks]# cat hosts [http] 192.168.100.131 [mysql] 192.168.100.132 [lamp] 192.168.100.130 [root@node1 ansible_playbooks]#
3、編輯 role http 任務檔案
[root@node1 roles]# pwd /opt/ansible_playbooks/roles [root@node1 roles]# tree http http ├── default ├── files │ └── httpd.conf ├── handlers │ └── main.yml ├── meta ├── tasks │ └── main.yml ├── templates │ └── httpd.conf └── vars └── main.yml 7 directories, 5 files [root@node1 roles]#
tasks 任務列表
[root@node1 roles]# cat http/tasks/main.yml - name: install httpd service yum: name=httpd state=present - name: start httpd service service: name=httpd state=started enabled=true - name: modify httpd config file from template template: src=httpd.conf dest=/etc/httpd/conf/httpd.conf tags: - modifyhttpconf notify: - restart httpd service [root@node1 roles]#
handlers
[root@node1 roles]# cat http/handlers/main.yml - name: restart httpd service service: name=httpd state=restarted [root@node1 roles]#
files 和 templates 下的檔案差別如下,這裡只是為了演示 vars 的作用
[root@node1 roles]# diff http/files/httpd.conf http/templates/httpd.conf 136c136 < Listen 8090 --- > Listen `listen`.`0`:`listen`.`1` 277c277 < --- > ServerName `host_fqdn`.`0` [root@node1 roles]#
vars
[root@node1 roles]# cat http/vars/main.yml listen: - "`ansible_all_ipv4_addresses`.`0`" - 8080 host_fqdn: - "`ansible_nodename`" [root@node1 roles]#
這裡的 hostname 和 address 其實可以直接使用 setup 變數,這裡這樣做是為了演示 vars 的用法
4、編輯 role mysql 任務檔案
[root@node1 roles]# pwd /opt/ansible_playbooks/roles [root@node1 roles]# tree mysql mysql ├── default ├── files │ └── my.cnf ├── handlers │ └── main.yml ├── meta ├── tasks │ └── main.yml ├── templates │ └── my.cnf └── vars └── main.yml 7 directories, 5 files [root@node1 roles]#
tasks:
[root@node1 roles]# cat mysql/tasks/main.yml - name: install mysql-server package yum: name=mysql-server state=present - name: start mysqld service service: name=mysqld state=started enabled=true - name: copy my.cnf to remote host template: src=my.cnf dest=/etc/my.cnf tags: - modifymycnf notify: restart mysqld service [root@node1 roles]#
handlers:
[root@node1 roles]# cat mysql/handlers/main.yml - name: restart mysqld service service: name=mysqld state=restarted [root@node1 roles]#
files 和 templates 下的 my.cnf 差別如下 (templates 下的多了兩個配置項,其中一個引用了 vars 的變數檔案)
[root@node1 roles]# diff mysql/files/my.cnf mysql/templates/my.cnf 1a2,3 > port=3306 > bind-address=`host_ip`.`0` [root@node1 roles]#
vars:
[root@node1 roles]# cat mysql/vars/main.yml host_ip: - "`ansible_all_ipv4_addresses`.`0`" [root@node1 roles]#
5、編輯 role php 任務檔案
[root@node1 roles]# cat php/tasks/main.yml -name: install php package yum: name=php state=present
6、開始執行
[root@node1 ansible_playbooks]# ansible-playbook -i hosts site.yml PLAY [http] ******************************************************************* GATHERING FACTS *************************************************************** ok: [192.168.100.131] TASK: [http | install httpd service] ****************************************** changed: [192.168.100.131] TASK: [http | start httpd service] ******************************************** changed: [192.168.100.131] TASK: [http | modify httpd config file from template] ************************* changed: [192.168.100.131] NOTIFIED: [http | restart httpd service] ************************************** changed: [192.168.100.131] PLAY [mysql] ****************************************************************** GATHERING FACTS *************************************************************** ok: [192.168.100.132] TASK: [mysql | install mysql-server package] ********************************** changed: [192.168.100.132] TASK: [mysql | start mysqld service] ****************************************** changed: [192.168.100.132] TASK: [mysql | copy my.cnf to remote host] ************************************ ok: [192.168.100.132] PLAY [lamp] ******************************************************************* GATHERING FACTS *************************************************************** ok: [192.168.100.130] TASK: [http | install httpd service] ****************************************** changed: [192.168.100.130] TASK: [http | start httpd service] ******************************************** changed: [192.168.100.130] TASK: [http | modify httpd config file from template] ************************* changed: [192.168.100.130] TASK: [mysql | install mysql-server package] ********************************** changed: [192.168.100.130] TASK: [mysql | start mysqld service] ****************************************** changed: [192.168.100.130] TASK: [mysql | copy my.cnf to remote host] ************************************ changed: [192.168.100.130] TASK: [php | install php package] ********************************************* changed: [192.168.100.130] NOTIFIED: [http | restart httpd service] ************************************** changed: [192.168.100.130] NOTIFIED: [mysql | restart mysqld service] ************************************ changed: [192.168.100.130] PLAY RECAP ******************************************************************** 192.168.100.130 : ok=10 changed=9 unreachable=0 failed=0 192.168.100.131 : ok=5 changed=4 unreachable=0 failed=0 192.168.100.132 : ok=4 changed=2 unreachable=0 failed=0 [root@node1 ansible_playbooks]#
檢視結果
[root@node1 ansible_playbooks]# ansible -i hosts all -m shell -a `netstat -natpl |grep httpd` 192.168.100.131 | success | rc=0 >> tcp 0 0 192.168.100.131:8080 0.0.0.0:* LISTEN 14662/httpd 192.168.100.132 | FAILED | rc=1 >> 192.168.100.130 | success | rc=0 >> tcp 0 0 192.168.100.130:8080 0.0.0.0:* LISTEN 11127/httpd [root@node1 ansible_playbooks]# ansible -i hosts all -m shell -a `netstat -natpl |grep mysqld` 192.168.100.131 | FAILED | rc=1 >> 192.168.100.132 | success | rc=0 >> tcp 0 0 192.168.100.132:3306 0.0.0.0:* LISTEN 61783/mysqld 192.168.100.130 | success | rc=0 >> tcp 0 0 192.168.100.130:3306 0.0.0.0:* LISTEN 11388/mysqld [root@node1 ansible_playbooks]#
還可以進行 when 判斷:
[root@node1 ansible_playbooks]# cat hosts [http] 192.168.100.131 [mysql] 192.168.100.132 192.168.100.131 [lamp] 192.168.100.130 [root@node1 ansible_playbooks]# cat site.yml - hosts: mysql remote_user: root roles: - { role: mysql, when: "ansible_nodename == `v3.lansgg.com`"}
執行結果:可以看到不滿足條件的 skipping
[root@node1 ansible_playbooks]# ansible-playbook -i hosts site.yml PLAY [mysql] ****************************************************************** GATHERING FACTS *************************************************************** ok: [192.168.100.131] ok: [192.168.100.132] TASK: [mysql | install mysql-server package] ********************************** skipping: [192.168.100.131] changed: [192.168.100.132] TASK: [mysql | start mysqld service] ****************************************** skipping: [192.168.100.131] changed: [192.168.100.132] TASK: [mysql | copy my.cnf to remote host] ************************************ skipping: [192.168.100.131] ok: [192.168.100.132] PLAY RECAP ******************************************************************** 192.168.100.131 : ok=1 changed=0 unreachable=0 failed=0 192.168.100.132 : ok=4 changed=2 unreachable=0 failed=0
7、也可以在 傳遞變數
[root@node1 ansible_playbooks]# cat site.yml - hosts: http remote_user: root roles: - { role: http, http_port: 2020,ServerName: "`ansible_nodename`"} [root@node1 ansible_playbooks]#
tasks:
[root@node1 ansible_playbooks]# cat roles/http/tasks/main.yml - name: install httpd service yum: name=httpd state=present - name: start httpd service service: name=httpd state=started enabled=true - name: modify httpd config file from template template: src=httpd.conf dest=/etc/httpd/conf/httpd.conf tags: - modifyhttpconf notify: - restart httpd service [root@node1 ansible_playbooks]#
template 檔案變數部分:
[root@node1 ansible_playbooks]# grep Listen roles/http/templates/httpd.conf |grep -v ^# Listen `listen`.`0`:`http_port` [root@node1 ansible_playbooks]# grep ServerName roles/http/templates/httpd.conf |grep -v ^# ServerName `ServerName` [root@node1 ansible_playbooks]#
執行結果:
[root@node1 ansible_playbooks]# ansible-playbook -i hosts site.yml PLAY [http] ******************************************************************* GATHERING FACTS *************************************************************** ok: [192.168.100.131] TASK: [http | install httpd service] ****************************************** changed: [192.168.100.131] TASK: [http | start httpd service] ******************************************** changed: [192.168.100.131] TASK: [http | modify httpd config file from template] ************************* changed: [192.168.100.131] NOTIFIED: [http | restart httpd service] ************************************** changed: [192.168.100.131] PLAY RECAP ******************************************************************** 192.168.100.131 : ok=5 changed=4 unreachable=0 failed=0