ansiblle---roles
使用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的更多相关文章
- MongoDB的内置角色 Built-In Roles
关于芒果的权限控制说白了就是定义 Role(角色) 来控制对数据库进行的操作(调用的方法比如查询方法find). 系统内置的Role分为 以下几大类: Database User Roles 这个是针 ...
- Apple Developer Program Roles Overview
Apple Developer Program Roles Overview There are three roles that can be assigned to Apple Developer ...
- 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 ...
- 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 ...
- [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 ...
- Ansible系列(五):playbook应用和roles自动化批量安装示例
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- IdentityServer4 指定角色授权(Authorize(Roles="admin"))
1. 业务场景 IdentityServer4 授权配置Client中的AllowedScopes,设置的是具体的 API 站点名字,也就是使用方设置的ApiName,示例代码: //授权中心配置 n ...
- ansible roles
roles 特点 目录结构清晰 重复调用相同的任务 目录结构相同 web - tasks - install.yml - copfile.yml - start.yml - main.yml - t ...
- ansible基础-roles
一 简介 注:本文demo使用ansible2.7稳定版 在我看来,role是task文件.变量文件.handlers文件的集合体,这个集合体的显著特点是:可移植性和可重复执行性. 实践中,通常我们以 ...
- Ansible系列之roles使用说明
roles(角色)介绍 ansible自1.2版本开始引入的新特性,用于层次性,结构化地组织playbook.roles能够根据层次型结构自动装载变量文件.tasks以及handlers等.要使用ro ...
随机推荐
- fragment概念理解
fragment概念理解知识,fragment概念理解图片 fragment概念理解内容,fragment概念理介绍,fragment概念理正文 Fragment是Android honeycomb ...
- c#复制文件夹和文件
/// <summary> /// 拷贝文件夹 /// </summary> /// <param name="srcdir"></par ...
- 【python+selenium】截取某个元素
一. selenium截图1.selenium提供了几个截取全屏的方法- get_screenshot_as_file(self, filename) --这个方法是获取当前window的截图,出现I ...
- 【Day3】3.提取商城分类结构
import re with open('index.html','r',encoding='utf-8') as f: html = re.sub('\n','',f.read()) section ...
- Redis5.0.3单机版安装
一.创建redis源码包存放目录 cd /usr/local/ mkdir redis 二.进入创建的目录,下载最新版Redis yum -y install wget wget http://dow ...
- Redis主从、哨兵、集群
主从 命名设置:>6380 slaveof 127.0.0.01 6379 slaveof on one----------配置:-- 注意一点: 一定开启rdb,不能使用aof从节点配置:主节 ...
- xcode 中 vary for traits详解
https://www.jianshu.com/p/d6896437e5a7 这篇文章写的很好!
- 生成器(generator) 详解
1. 生成器是什么? 利用迭代器,我们可以在每次迭代获取数据(通过next()方法)时按照特定的规律进行生成.但是我们在实现一个迭代器时,关于当前迭代到的状态需要我们自己记录,进而才能根据当前状态生成 ...
- git ls-remote url,判断 url 是否存在
git ls-remote url,判断 url 是否存在 git ls-remote <url>
- ubuntu 16.04中文输入法安装
转自: http://blog.csdn.net/u011795345/article/details/53041707 最近刚给笔记本装了Ubuntu+win10双系统,但是ubuntu16.04没 ...