task include

---
# possibly saved as tasks/foo.yml
- name: placeholder foo
command: /bin/foo - name: placeholder bar
command: /bin/bar tasks:
- include: tasks/foo.yml

变量传递

tasks:
- include: wordpress.yml wp_user=timmy
- include: wordpress.yml wp_user=alice
- include: wordpress.yml wp_user=bob # 1.4版本以上,可以传递列表与字典
tasks:
- { include: wordpress.yml, wp_user: timmy, ssh_keys: [ 'keys/one.txt', 'keys/two.txt' ] } tasks:
- { include: wordpress.yml, wp_user: timmy, ssh_keys: [ 'keys/one.txt', 'keys/two.txt' ] } # include文件使用{{ }}引用变量
{{ wp_user }} # 其他方式
tasks:
- include: wordpress.yml
vars:
wp_user: timmy
some_list_variable:
- alpha
- beta
- gamma

handlers include

---
# this might be in a file like handlers/handlers.yml
- name: restart apache
service: name=apache state=restarted handlers:
- include: handlers/handlers.yml

import文件

- name: this is a play at the top level of a file
hosts: all
remote_user: root tasks: - name: say hi
tags: foo
shell: echo "hi..." - include: load_balancers.yml
- include: webservers.yml
- include: dbservers.yml

角色

playbooks目录结构

site.yml
webservers.yml
fooservers.yml
roles/
common/
files/
templates/
tasks/
handlers/
vars/
defaults/
meta/
webservers/
files/
templates/
tasks/
handlers/
vars/
defaults/
meta/

playbook中定义

---
- hosts: webservers
roles:
- common
- webservers

使用变量

---
- hosts: webservers
roles:
- common
- { role: foo_app_instance, dir: '/opt/a', port: 5000 }
- { role: foo_app_instance, dir: '/opt/b', port: 5001 }

使用表达式

---
- hosts: webservers
roles:
- { role: some_role, when: "ansible_os_family == 'RedHat'" }

assign tags

---
- hosts: webservers
roles:
- { role: foo, tags: ["bar", "baz"] }
---
- hosts: webservers pre_tasks:
- shell: echo 'hello' roles:
- { role: some_role } tasks:
- shell: echo 'still busy' post_tasks:
- shell: echo 'goodbye'

role默认变量

在defaults/main.yml中添加

role依赖

role依赖存储在meta/main.yml文件

---
dependencies:
- { role: common, some_parameter: 3 }
- { role: apache, port: 80 }
- { role: postgres, dbname: blarg, other_parameter: 12 } # 全路径
---
dependencies:
- { role: '/path/to/common/roles/foo', x: 1 } # 使用版本控制
---
dependencies:
- { role: 'git+http://git.example.com/repos/role-foo,v1.1,foo' }
- { role: '/path/to/tar/file.tgz,,friendly-name' }

allow_duplicates: yes

---
dependencies:
- { role: wheel, n: 1 }
- { role: wheel, n: 2 }
- { role: wheel, n: 3 }
- { role: wheel, n: 4 } # meta/main.yml内容
---
allow_duplicates: yes
dependencies:
- { role: tire }
- { role: brake } # result
tire(n=1)
brake(n=1)
wheel(n=1)
tire(n=2)
brake(n=2)
wheel(n=2)
...
car

ansible使用4-Playbook Roles and Include Statements的更多相关文章

  1. Ansible Playbook Roles and Include Statements

    介绍 虽然可以在一个非常大的文件中编写一个playbook(您可能会以这种方式开始学习playbook),但最终您将需要重新使用文件并开始组织事情. 在基本级别,饱含任务的文件允许您将配置策略分解成较 ...

  2. Playbook 角色(Roles) 和 Include 语句

    简介 当我们刚开始学习运用 playbook 时,可能会把 playbook 写成一个很大的文件,到后来可能你会希望这些文件是可以方便去重用的,所以需要重新去组织这些文件. Include 语句 基本 ...

  3. ansible编译httpd playbook示例

    以下是playbook的内容.它的处理流程是: 1.先在本地下载apr,apr-util,httpd共3个.tar.gz文件. 2.解压这3个文件. 3.安装pcre和pcre-devel依赖包. 4 ...

  4. Ansible playbook roles

    1  概述 角色(roles):如果我们使用playbook写成一个文件,这个文件会很大,但是不方便组织,我们可以分组,把playbook根据功能,如handler,tasks等分门别类的放在在各自的 ...

  5. devops工具-Ansible进阶playbook&roles

    一.playbook介绍     playbook 是 Ansible 管理配置.部署应用的核心所在,一个playbook由有多“play组成”,而一个play实际就是一个task,每个task是由多 ...

  6. ansible的高级应用-roles

    在之前我们知道了playbook,类似于shell的脚本,playbook适用于一些不太麻烦的部署任务,比如说使用playbook安装mysql,那么我们直接写一个playbook文件即可.可是如果我 ...

  7. ansible中的playbook详解

    首先简单说明一下playbook,playbook是什么呢?根本上说playbook和shell脚本没有任何的区别,playbook就像shell一样,也是把一堆的命令组合起来,然后加入对应条件判断等 ...

  8. ansible核心模块playbook介绍

    ansible的playbook采用yaml语法,它简单地实现了json格式的事件描述.yaml之于json就像markdown之于html一样,极度简化了json的书写.在学习ansible pla ...

  9. playbook+roles

    playbook setup ansible_all_ipv4_addresses # ipv4的所有地址 ansible_all_ipv6_addresses # ipv6的所有地址 ansible ...

随机推荐

  1. LUNA16数据集(一)简介

    LUNA16,全称Lung Nodule Analysis 16,是16年推出的一个肺部结节检测数据集,旨在作为评估各种CAD(computer aid detection计算机辅助检测系统)的ban ...

  2. 在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\106f9ae8\cc0e1169\App_global.asax.haz99mum.dll”--“拒绝访问。 ”错误

    解决方法: 1,通常的解决方法:原因是由于系统目录下的Temp目录无相应的权限所致,具体操作如下:C:\Windows\temp-->属性-->安全-->编辑-->添加NETW ...

  3. MySQL安全优化

    一.数据库相关 1. MySQL版本的选择 在正式生产环境中,建议使用5.6或以上系列的版本(5.7不建议,曾经用过这个版本,问题有点多). 2. 运行用户与端口的配置 2.1.确保MySQL运行用户 ...

  4. xshell传输文件到linux

    我们使用xshell来连接linux之后会发现方便了很多~然而使用xshell要怎么把本地文件传到linux上面呢? 1.yum安装一款工具.#yum install  lrzsz -y 2检查是否安 ...

  5. C:foreEach

    c:forEach用法   <c:foreach>用法   <c:foreach>类似于for和foreach循环   以下是我目前见过的用法:1.循环遍历,输出所有的元素.& ...

  6. jquery插件分页

    收藏地址: http://www.jq22.com/yanshi5697

  7. js学习笔记 -- 函数

    js函数有类似javaMethod用法 Math.max.apply( Math.max.call( Array map,reduce,filter,sort , , , , , , , , ]; v ...

  8. 未来HTML6出现的10个特性

    网络技术正趋向于发展为一个巨大的移动APP市场,在Web开发的革命浪潮中起着指示性作用,自HTML引入以来,创建可转换,有新意的网络移动应用程序变得So easy,web开发中运用先进技术也很容易处理 ...

  9. CSS选择器比较:queryselector queryselectorall

    官网解释: querySelector() and querySelectorAll() are two JavaScript functions very useful when working w ...

  10. java多线程之原子变量

    看链接博客:http://blog.csdn.net/u011116672/article/details/51068828