ansible的roles roles用于层次性、结构化地组织playbook。roles能够更具层次结构自动装载变量文件,task以及handlers等。要使用roles只需要在playbook中使用include指令即可,简单来讲,roles就是通过分别将变量、文件、任务、模板以及处理器放置于单独的目录中,并可以便捷地include他们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以时用于构建守护进程等守护场景中。
roles就是按照不同的分类和功能进行目录化,模块化,能够实现代码的复合使用。
roles的目录结构 每个角色都有特定的层级目录结构进行组织,每一个roles都必须符合这种结构规范。
roles目录结构及作用 1 2 3 4 5 6 7 8 9 10 playbook.yml roles/ └──project/ ├──tasks/ ├──files/ ├──vars/ ├──templates/ ├──handlers/ ├──default/ └──meta/
roles的创建步骤
创建以roles命名的目录
在roles目录中分别创建以各角色命名的目录
在每个角色命名的目录中分别创建files、handlers、meta、tasks、templates和vars目录;用不到的目录可以创建为空目录,也可以不创建
在playbook文件中,调用各角色
roles的使用 创建httpd的roles 1.创建相应的目录结构
1 2 3 4 5 6 7 8 [root@ansible data] mkdir: created directory ‘roles’ mkdir: created directory ‘roles/httpd’ mkdir: created directory ‘roles/httpd/templates’ mkdir: created directory ‘roles/httpd/tasks’ mkdir: created directory ‘roles/httpd/files’ mkdir: created directory ‘roles/httpd/vars’ mkdir: created directory ‘roles/httpd/handlers’
2.创建所需的task
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [root@ansible data] - name: install httpd yum: name=httpd [root@ansible data] - name: config copy: src=httpd.conf dest=/etc/httpd.conf [root@ansible data] - name: index.html copy: src=index.html dest=/var/www/html [root@ansible data] - name: service start service: name=httpd state=started
3.创建task的执行顺序文件
1 2 3 4 5 6 [root@ansible data] - include: install.yaml - include: config.yaml - include: html.yaml - include: service.yaml
4.将所需要用到的配置文件放至file目录下,对配置文件稍做修改
1 2 3 4 [root@ansible data] [root@ansible data] Listen 8080
5.在files目录下创建,index.html文件
6.创建playbook调用文件
1 2 3 4 5 6 7 8 [root@ansible data] --- - hosts: webserver roles: - role: httpd
7.测试检查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [root@ansible data] PLAY [webserver] *********************************************************************************************** TASK [Gathering Facts] ***************************************************************************************** ok: [192.168.73.135] ok: [192.168.73.134] TASK [httpd : install httpd] *********************************************************************************** changed: [192.168.73.134] changed: [192.168.73.135] TASK [httpd : config] ****************************************************************************************** changed: [192.168.73.134] changed: [192.168.73.135] TASK [httpd : index.html] ************************************************************************************** changed: [192.168.73.134] changed: [192.168.73.135] TASK [httpd : service start] *********************************************************************************** changed: [192.168.73.134] changed: [192.168.73.135] PLAY RECAP ***************************************************************************************************** 192.168.73.134 : ok=5 changed=4 unreachable=0 failed=0 192.168.73.135 : ok=5 changed=4 unreachable=0 failed=0
8.执行playbook
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [root@ansible data] PLAY [webserver] *********************************************************************************************** TASK [Gathering Facts] ***************************************************************************************** ok: [192.168.73.135] ok: [192.168.73.134] TASK [httpd : install httpd] *********************************************************************************** changed: [192.168.73.134] changed: [192.168.73.135] TASK [httpd : config] ****************************************************************************************** changed: [192.168.73.134] changed: [192.168.73.135] TASK [httpd : index.html] ************************************************************************************** changed: [192.168.73.134] changed: [192.168.73.135] TASK [httpd : service start] *********************************************************************************** changed: [192.168.73.135] changed: [192.168.73.134] PLAY RECAP ***************************************************************************************************** 192.168.73.134 : ok=5 changed=4 unreachable=0 failed=0 192.168.73.135 : ok=5 changed=4 unreachable=0 failed=0
9.验证httpd是否被部署
1 2 3 4 5 [root@ansible data] <h1>welcome to mylinuxops.com</h1> [root@ansible data] <h1>welcome to mylinuxops.com</h1>