使用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. mysql tinyint(1) 在java中被转化为boolean

    数据库表字段类型为:tinyint 长度为1 在java中对应的类型是boolean 查询时直接在页面展示成true或false 如果是2,3,4 这样的也是默认成true,非常不友好. 解决方案: ...

  2. selenium检测webdriver封爬虫的解决方法

    有不少朋友在开发爬虫的过程中喜欢使用Selenium + Chromedriver,以为这样就能做到不被网站的反爬虫机制发现. 先不说淘宝这种基于用户行为的反爬虫策略,仅仅是一个普通的小网站,使用一行 ...

  3. Django项目上线的准备工作

    settings文件配置 添加上STATIC_ROOT = os.path.join(BASE_DIR, "/static/") 我的配置项目文件的最终 STATIC_URL = ...

  4. 利用commands模块执行shell命令

    利用commands模块执行shell命令 用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态 ...

  5. Hadoop_33_Hadoop HA的搭建

    Hadoop HA的搭建,可参考链接:https://blog.csdn.net/mrbcy/article/details/64939623 说明:    1.在hadoop2.0中通常由两个Nam ...

  6. Ubuntu在命令行安装显卡驱动

    Ubuntu在命令行安装显卡驱动 1.进入电脑的BIOS,把Security Boot设置为Disabled. 2.进入终端,输入一以下命令(这里以安装NVIDIA-390进行演示) sudo add ...

  7. mongo批量写入es

    import pymongo import math from elasticsearch import Elasticsearch from elasticsearch import helpers ...

  8. windows下如何打开.sketch的文件

    1 .sketch的文件只能在苹果mac上支持的一种文件格式,现在越来越多的设计师喜欢用.sketch 2 windows下如果想打开.sketch文件,去Microsoft store 找一个Lun ...

  9. SQL 查询今天、昨天、7天内、30天的数据

    今天的所有数据: 昨天的所有数据: 7天内的所有数据: 30天内的所有数据: 本月的所有数据: 本年的所有数据: 查询今天是今年的第几天: select datepart(dayofyear,getD ...

  10. c#系统泛型委托

    Action<T> 无返回值的系统泛型委托 namespace ConsoleApp1 { public class UserInfo { public int Id { get; set ...