roles 用於層次性、結構化地組織playbook。
roles 可以根據層次型結構自動裝載變量文件、tasks以及handlers等。
要使用roles只須要在playbook中使用include指令便可。
簡單來說,roles就是經過分別將變量(vars)、文件(file)、任務(tasks)、模塊(modules)及處理器(handlers)放置於單獨的目錄中,並能夠便捷地include它們的一種機制。
角色通常用於基於主機構建服務的場景中,但也能夠是用於構建守護進程等場景中。php
建立 roles 的步驟node
(1) 建立以roles命名的目錄;
(2) 在roles目錄中分別建立以各角色名稱命名的目錄,如webservers等。注意:在 roles 必須包括 site.yml文件,能夠爲空;
(3) 在每一個角色命名的目錄中分別建立files、handlers、meta、tasks、templates和vars目錄;用不到的目錄能夠建立爲空目錄,也能夠不建立;
(4) 在playbook文件中,調用各角色;mysql
roles 內各目錄中可用的文件web
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文件。sql
示例 一 、shell
分別建立三個 role ,分別爲 http mysql php,建立要使用的目錄,通常爲所有建立,使用不到的能夠爲空,
bash
測試主機 1 安裝 httpd tcp
測試主機 2 安裝 mysql
ide
測試主機 3 安裝 http mysql php測試
一、
建立目錄
[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}
二、測試主機爲:
[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]#
三、編輯 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 的用法
四、編輯 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]#
五、編輯 role php 任務文件
[root@node1 roles]# cat php/tasks/main.yml -name: install php package yum: name=php state=present
六、開始執行
[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
七、也能夠在 傳遞變量
[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