使用ansible中的roles来管理主机。

剧本中的roles
你现在已经学过 tasks 和 handlers,那怎样组织 playbook 才是最好的方式呢?简单的回答就是:使用 roles ! Roles 基于一个已知的文件结构,去自动的加载某些 vars_files,tasks 以及 handlers。基于 roles 对内容进行分组,使得我们可以容易地与其他用户分享 roles 。

你现在已经学过 tasks 和 handlers,那怎样组织 playbook 才是最好的方式呢?简单的回答就是:使用 roles ! Roles 基于一个已知的文件结构,去自动的加载某些 vars_files,tasks 以及 handlers。基于 roles 对内容进行分组,使得我们可以容易地与其他用户分享 roles 。
如果 roles/x/tasks/main.yml 存在, 其中列出的 tasks 将被添加到 play 中
如果 roles/x/handlers/main.yml 存在, 其中列出的 handlers 将被添加到 play 中
如果 roles/x/vars/main.yml 存在, 其中列出的 variables 将被添加到 play 中
如果 roles/x/meta/main.yml 存在, 其中列出的 “角色依赖” 将被添加到 roles 列表中 (1.3 and later)
所有 copy tasks 可以引用 roles/x/files/ 中的文件,不需要指明文件的路径。
所有 script tasks 可以引用 roles/x/files/ 中的脚本,不需要指明文件的路径。
所有 template tasks 可以引用 roles/x/templates/ 中的文件,不需要指明文件的路径。
所有 import  tasks 可以引用 roles/x/tasks/ 中的文件,不需要指明文件的路径。

roles的优势:

  • 目录结构比较清晰

  • 可以相互调用

  • 便于备分

roles的目录结构:

nginx/
├── files
│   └── fstab
├── handlers
│   └── main.yml
├── tasks
│   ├── copyfile.yml
│   ├── install.yml
│   ├── main.yml
│   └── start.yml
├── templates
│   └── nginx.conf
└── vars
└── main.yml

在/etc/ansible/roles目录中创建角色

cd /etc/ansible/roles

已安装nginx为例,创建一下的目录结构

cd /etc/ansible/roles

mkdir nginx

cd nginx

mkdir {tasks,files,templates,handlers,vars}

目录说明:

tasks 存放任务的
files 存放静态文件的,copy模块需要的文件
templates 存放动态文件 template 模块需要渲染变量的文件
handlers handlers 执行的文件
vars 存放变量的文件

cd etc/ansible/roles/nginx/tasks

依次写这些目录中的文件

安装nginx

[root@bogon tasks]# cat yum.yml
- name: yum install nginx
yum: name=nginx

创建nginx用户

[root@bogon tasks]# cat createuser.yml
- name: create{{user}}
user: name={{user}}

启动nginx

[root@bogon tasks]# cat start.yml
- name: start nginx
service: name=nginx state=started

copy nginx的配置文件

[root@bogon tasks]# cat copyfile.yml
- name: copy nginx.conf
template: src=nginx.conf dest=/etc/nginx/nginx.conf
tags: copyfile
notify: restart nginx

任务的执行顺序

[root@bogon tasks]# cat main.yml 
- import_tasks: yum.yml
- import_tasks: start.yml
- import_tasks: createuser.yml
- import_tasks: copyfile.yml

cd /etc/ansible/roles/nginx/handlers

[root@bogon handlers]# cat main.yml
- name: restart nginx
service: name=nginx state=restarted

cd /etc/ansible/roles/nginx/templates
[root@bogon templates]# ll
total 4
-rw-r--r-- 1 root root 2495 Jul 18 16:00 nginx.conf

cat nginx.conf

nginx 的进程数,cpu核数的2倍
worker_processes {{ansible_processor_vcpus*}};
# 线程数
events {
worker_connections ;
}

/etc/ansible/roles/nginx/vars

[root@bogon vars]# cat main.yml
{'user':'nginx'}

/etc/ansible/roles

[root@bogon roles]# cat nginx.yml
- hosts: web
remote_user: root
roles:
- nginx

执行剧本

ansible-playbook    nginx.yml

查找顺序

  • 先查找roles里面的目录

  • 找tasks里面的main.yml

  • 如果发现了import_tasks 根据引入的次序依次执行

  • 如果template,则区templates目录里面找文件

  • 如果发现copy,则区files目录里面找文件

  • 如果发现了notify,则去handlers里面找main.yml

  • 如果发现了变量,则去vars里面找main.yml

相互调用

- import_tasks: roles/nginx/tasks/install.yml

使用ansible roles可以在  https://galaxy.ansible.com/

这个网址上看到别人写好的ansible roels可以直接使用

ansiblle---roles的更多相关文章

  1. MongoDB的内置角色 Built-In Roles

    关于芒果的权限控制说白了就是定义 Role(角色) 来控制对数据库进行的操作(调用的方法比如查询方法find). 系统内置的Role分为 以下几大类: Database User Roles 这个是针 ...

  2. Apple Developer Program Roles Overview

    Apple Developer Program Roles Overview There are three roles that can be assigned to Apple Developer ...

  3. Show Roles Assigned to a Specific User

     Here is a query that I often use to lookup Roles assigned to a specific PeopleSoft user. At run tim ...

  4. Developers, do consider different user roles! - A bad experience with cron

    The Story: Last week, I found one of our embedded arm linux device  ran out of flash space( totally ...

  5. [Hive - LanguageManual] Create/Drop/Grant/Revoke Roles and Privileges / Show Use

    Create/Drop/Grant/Revoke Roles and Privileges Hive Default Authorization - Legacy Mode has informati ...

  6. Ansible系列(五):playbook应用和roles自动化批量安装示例

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  7. IdentityServer4 指定角色授权(Authorize(Roles="admin"))

    1. 业务场景 IdentityServer4 授权配置Client中的AllowedScopes,设置的是具体的 API 站点名字,也就是使用方设置的ApiName,示例代码: //授权中心配置 n ...

  8. ansible roles

    roles 特点 目录结构清晰 重复调用相同的任务 目录结构相同 web - tasks - install.yml - copfile.yml - start.yml -  main.yml - t ...

  9. ansible基础-roles

    一 简介 注:本文demo使用ansible2.7稳定版 在我看来,role是task文件.变量文件.handlers文件的集合体,这个集合体的显著特点是:可移植性和可重复执行性. 实践中,通常我们以 ...

  10. Ansible系列之roles使用说明

    roles(角色)介绍 ansible自1.2版本开始引入的新特性,用于层次性,结构化地组织playbook.roles能够根据层次型结构自动装载变量文件.tasks以及handlers等.要使用ro ...

随机推荐

  1. css改变鼠标指针的形状

    <html> <body> <span style="cursor:auto"> <br /> <span style=&qu ...

  2. JasperReport笔记

    参考: https://blog.csdn.net/dullchap/article/details/51799070 关于 ireport的初步使用 ,笔记记录

  3. apache thinkphp5 强制https://访问

    根目录下,.htaccess文件 <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On ...

  4. 【3】Git命令

    个人推荐的Git知识学习网站:https://git-scm.com . git常用操作图 init -> add -> commit -> remote -> push 初始 ...

  5. Image Processing and Analysis_8_Edge Detection:Multiresolution edge detection techniques ——1995

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  6. 去掉行尾的^M

    1. 处理掉行尾的^M 在windos下进行linux内核驱动编写,调试成功后需要集成到内核代码中去,所以会通过虚拟机共享文件夹拷贝到内核对应目录,这时候看源码文件还是没有异常的. 当对该文件进行回车 ...

  7. PMM 监控 MySQL

    Percona Monitoring and Management (PMM)是一款开源的用于监控 MySQL 和 MongoDB 性能的开源平台,通过 PMM 客户端收集到的 DB 监控数据用第三方 ...

  8. Sublime text3安装

    一.Sublime text3下载 [20190506]下载 官网下载:https://www.sublimetext.com/ https://download.sublimetext.com/Su ...

  9. MYSQL安全模式SQL语法需要注意的地方

    MYSQL安全模式 Mysql版本: 背景: 为了避免在执行delete.update将全表数据清空或者覆盖修改,在新项目营销云中开启了mysql的安全模式. 安全模式要求不能对非主键的条件查询做up ...

  10. spring 的自动定时任务

    spring的自动定时任务有两种 第一种:通过xml配置来设置 需要在xml中引入新的约束,并且需要配置<task:scheduled-tasks> ,主要配置内容如下: <?xml ...