ansible----roles

roles简介

ansible1.2版本引入的新特性,用于层次性、结构化地组织playbook。roles能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量、文件、任务、模板及处理器放置于单独的目录中,并可以便捷地include它们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中

roles目录结构

playbook.yml
roles/
角色目录下,分项目目录:
project/
项目目录下细分为如下几个目录:
tasks/
files/
vars/
templates/
handlers/
default/    不常用
meta/       不常用

role应用

用role实现安装http

[root@centos7 ansible]# mkdir /data/ansible
[root@centos7 ansible]# cd /data/ansible
[root@centos7 ansible]# mkdir roles/{httpd,mysql,redis}/{tasks,handlers,files} -pv 
[root@centos7 ansible]# cd roles/httpd/tasks/
[root@centos7 tasks]# vim install.yml       #需要把playbook中的任务一个动作拆分成一个yaml文件
- name: install httpd package
  yum: name=httpd
[root@centos7 tasks]# vim config.yml
- name: config file
  copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes             #拷贝的源文件会自动到当前角色目录下的files目录中获取
  notify: restart            #触发器会自动到当前角色的目录下的handlers目录下获取
[root@centos7 tasks]# vim service.yml
- name: start service
  service: name=httpd state=started enabled=yes
[root@centos7 tasks]# vim main.yml            #指定当前任务都包含哪些动作文件和执行动作的顺序
- include: install.yml
- include: config.yml
- include: service.yml
[root@centos7 tasks]# vim ../handlers/main.yml
- name: restart
  service: name=httpd state=restarted

准备好一个配置文件,复制到/data/ansible/roles/httpd/files/目录下

[root@centos7 tasks]# cd /data/ansible
[root@centos7 ansible]# vim role_httpd.yml        #编辑一个playbook,调用http角色
---
#httpd role
- hosts: appsrvs
  remote_user: root

  roles:                     
    - role: httpd             #指定角色

目录结构如下:
|-- role_httpd.yml
`-- roles
    |-- httpd
    |   |-- files
    |   |   `-- httpd.conf
    |   |-- handlers
    |   |   `-- main.yml
    |   `-- tasks
    |       |-- config.yml
    |       |-- install.yml
    |       |-- main.yml
    |       `-- service.yml
    |-- mysql
    |   |-- files
    |   |-- handlers
    |   `-- tasks
    `-- redis
        |-- files
        |-- handlers
        `-- tasks

通过跨角色实现文件复用:

试验场景
部署nginx和网站页面时,发现http角色目录下有网站页面,可以通过调用另一个角色的文件来实现,不需要自己再专门编辑一个文件
目录结构:
[root@centos7 roles]# tree
.
|-- httpd
|   |-- files
|   |   `-- index.html
|   |-- handlers
|   `-- tasks
|-- nginx
|   |-- files
|   |-- handlers
|   `-- tasks
|       |-- config.yml
|       |-- index.yml
|       |-- install.yml
|       |-- main.yml
|       `-- service.yml
`-- redis
    |-- files
    |-- handlers
    `-- tasks
[root@centos7 roles]# vim nginx/tasks/index.yml
- name: index.html
  copy: src=roles/httpd/files/index.html dest=/usr/share/nginx/html/            #指定调用角色文件的相对路径

条件式地使用roles

[root@centos7 ansible]# vim role_http_nginx.yml         
---
#test roles
- hosts: appsrvs
  remote_user: root

  roles:
    - {role: httpd, tags: [httpd,web], when: ansible_distribution_major_version == "7" }           #指定角色,给http角色设置两个标签,并且通过判断远程主机的主版本号进行选择性的执行;把三个步骤组合成一个字典;远程主机的主版本号是7,安装http;版本号是8,安装nginx
    - {role: nginx, tags: [nginx,web], when: ansible_distribution_major_version == "8" }

当执行playbook时,指定执行标签为web时,则两个角色(http、nginx)都进行执行

ansible推荐资料

http://galaxy.ansible.com
http://github.com/
http://ansible.com.cn/
https://github.com/ansible/ansible
https://github.com/ansible/ansible-examples

原文地址:https://www.cnblogs.com/dongzhanyi123/p/11914499.html