ansible使用4-Playbook Roles and Include Statements
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的更多相关文章
- Ansible Playbook Roles and Include Statements
介绍 虽然可以在一个非常大的文件中编写一个playbook(您可能会以这种方式开始学习playbook),但最终您将需要重新使用文件并开始组织事情. 在基本级别,饱含任务的文件允许您将配置策略分解成较 ...
- Playbook 角色(Roles) 和 Include 语句
简介 当我们刚开始学习运用 playbook 时,可能会把 playbook 写成一个很大的文件,到后来可能你会希望这些文件是可以方便去重用的,所以需要重新去组织这些文件. Include 语句 基本 ...
- ansible编译httpd playbook示例
以下是playbook的内容.它的处理流程是: 1.先在本地下载apr,apr-util,httpd共3个.tar.gz文件. 2.解压这3个文件. 3.安装pcre和pcre-devel依赖包. 4 ...
- Ansible playbook roles
1 概述 角色(roles):如果我们使用playbook写成一个文件,这个文件会很大,但是不方便组织,我们可以分组,把playbook根据功能,如handler,tasks等分门别类的放在在各自的 ...
- devops工具-Ansible进阶playbook&roles
一.playbook介绍 playbook 是 Ansible 管理配置.部署应用的核心所在,一个playbook由有多“play组成”,而一个play实际就是一个task,每个task是由多 ...
- ansible的高级应用-roles
在之前我们知道了playbook,类似于shell的脚本,playbook适用于一些不太麻烦的部署任务,比如说使用playbook安装mysql,那么我们直接写一个playbook文件即可.可是如果我 ...
- ansible中的playbook详解
首先简单说明一下playbook,playbook是什么呢?根本上说playbook和shell脚本没有任何的区别,playbook就像shell一样,也是把一堆的命令组合起来,然后加入对应条件判断等 ...
- ansible核心模块playbook介绍
ansible的playbook采用yaml语法,它简单地实现了json格式的事件描述.yaml之于json就像markdown之于html一样,极度简化了json的书写.在学习ansible pla ...
- playbook+roles
playbook setup ansible_all_ipv4_addresses # ipv4的所有地址 ansible_all_ipv6_addresses # ipv6的所有地址 ansible ...
随机推荐
- 写一个Spring Boot的Hello World
尽管这个demo也就hello world水平,但我还是要记录一下(总算能动了QAQ),毕竟老是看文章不动手不行啊 上次写Servlet的CRUD项目还是2月份,虽然代码忘的差不多了,但我就记得JDB ...
- element el-tree循环遍历树形结构,并动态赋值disabled属性
凌晨3点,功夫不负有心人,已经累趴,效果终于出来: 贴上代码: <style scoped> .form { width: 50%; } </style> <templa ...
- UVA 10806
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> us ...
- MYSQL生成两个日期之间的所有日期数据
set @i = -1; set @sql = repeat(" select 1 union all",-datediff('2021-01-01','2030-12-31')+ ...
- HIVE 计算指定日期本周的第一天和最后一天
-- 计算指定日期本周的第一天和最后一天 select day ,dayofweek(day) as dw1 ,date_add( - dayofweek(day)) as Su_s -- 周日_st ...
- Doxygen生成美丽注释文档(1):初体验
Chapter 1 - 准备工作 (Windows环境) 1.1 程序包下载 1. Doxygen * 源码: git clone https://github.com/doxygen/doxygen ...
- PHP、thinkPHP5.0开发网站文件管理功能(三)编辑文件
public function edit(){ $file = iconv('UTF-8','GB2312',urldecode(input('file'))); if(empty($file)|| ...
- Git命令行中文显示错误
中文文件名乱码(git status.git log.git pull .git push) #不对0x80以上的字符进行quote,解决git status/commit时中文文件名乱码git co ...
- java——二分搜索树 BST(递归、非递归)
~ package Date_pacage; import java.util.Stack; import java.util.ArrayList; import java.util.LinkedLi ...
- Ajax传统操作
JavaScript: ...