ansible-playbook文件复用
1. ansible-playbook文件复用
1.1) include 和 import区别
include(动态):在运行时导入
- --list-tags,--list-tasks不会显示到输出
- 不能使用notify触发来自include内处理程序名称(handlers)
import(静态):在playbook解析时预先导入
- 不能与循环一起使用
- 将变量用于目标文件或角色名称时,不能使用inventory(主机/主机组等)中的变量
2. import_playbook
2.1) import_palybook结构详解图

3. 一下简单演示意思使用
3.1) 编写lnmp.yaml的文件测试
1 [root@test-1 bin]# vim nginx.yaml
2 [root@test-1 bin]# cat nginx.yaml
3 ---
4 - hosts: web1
5 gather_facts: no
6
7 tasks:
8 - name: Install nginx
9 debug: msg="test nginx "
10
11 [root@test-1 bin]# vim php.yaml
12 [root@test-1 bin]# cat php.yaml
13 ---
14 - hosts: web1
15 gather_facts: no
16
17 tasks:
18 - name: Install php
19 debug: msg="test php "
20
21 [root@test-1 bin]# vim mysql.yaml
22 [root@test-1 bin]# cat mysql.yaml
23 ---
24 - hosts: web1
25 gather_facts: no
26
27 tasks:
28 - name: Install mysql
29 debug: msg="test mysql "
3.2) 编写improt执行文件引用
1 [root@test-1 bin]# vim import_lnmp.yaml
2 [root@test-1 bin]# cat import_lnmp.yaml
3 #import lnmp
4 ---
5 - import_playbook: nginx.yaml
6 - import_playbook: php.yaml
7 - import_playbook: mysql.yaml
3.3) 检查配置文件是否正常
1 [root@test-1 bin]# ansible-playbook --syntax-check nginx.yaml
2
3 playbook: nginx.yaml
4 [root@test-1 bin]# ansible-playbook --syntax-check php.yaml
5
6 playbook: php.yaml
7 [root@test-1 bin]# ansible-playbook --syntax-check mysql.yaml
8
9 playbook: mysql.yaml
10 [root@test-1 bin]# ansible-playbook --syntax-check import_lnmp.yaml
11
12 playbook: import_lnmp.yaml
3.4) 执行improt文件
1 [root@test-1 bin]# ansible-playbook import_lnmp.yaml
2
3 PLAY [web1] ************************************************************************************************************************************
4
5 TASK [Install nginx] ***************************************************************************************************************************
6 ok: [192.168.200.132] => {
7 "msg": "test nginx "
8 }
9 ok: [192.168.200.133] => {
10 "msg": "test nginx "
11 }
12
13 PLAY [web1] ************************************************************************************************************************************
14
15 TASK [Install php] *****************************************************************************************************************************
16 ok: [192.168.200.132] => {
17 "msg": "test php "
18 }
19 ok: [192.168.200.133] => {
20 "msg": "test php "
21 }
22
23 PLAY [web1] ************************************************************************************************************************************
24
25 TASK [Install mysql] ***************************************************************************************************************************
26 ok: [192.168.200.132] => {
27 "msg": "test mysql "
28 }
29 ok: [192.168.200.133] => {
30 "msg": "test mysql "
31 }
32
33 PLAY RECAP *************************************************************************************************************************************
34 192.168.200.132 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
35 192.168.200.133 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4.include_tasks和import_tasks结构详细图

5. 编写include_tasks文件
5.1) 编写include_tasks测试文件
1 [root@test-1 bin]# vim tasks1.yaml
2 [root@test-1 bin]# cat tasks1.yaml
3 #tasks1.yaml
4 ---
5 - name: tasks1
6 debug: msg="hello {{user}}"
7
8 [root@test-1 bin]# vim tasks2.yaml
9 [root@test-1 bin]# cat tasks2.yaml
10 #tasks2.yaml
11 ---
12 - name: tasks2
13 debug: msg="hello {{user}}"
5.2) 编写include_tasks文件
1 [root@test-1 bin]# vim include_tasks.yaml
2 [root@test-1 bin]# cat include_tasks.yaml
3 ---
4 - hosts: web1
5 gather_facts: no
6
7 tasks:
8 - include_tasks: tasks1.yaml
9 - include_tasks: tasks2.yaml
5.3) 检查配置文件
1 [root@test-1 bin]# ansible-playbook --syntax-check include_tasks.yaml
2
3 playbook: include_tasks.yaml
5.4) 执行include_tasks文件
1 [root@test-1 bin]# ansible-playbook include_tasks.yaml
2
3 PLAY [web1] ************************************************************************************************************************************
4
5 TASK [include_tasks] ***************************************************************************************************************************
6 included: /ansible/import/bin/tasks1.yaml for 192.168.200.132, 192.168.200.133
7
8 TASK [tasks1] **********************************************************************************************************************************
9 ok: [192.168.200.132] => {
10 "msg": "hello tasks1"
11 }
12 ok: [192.168.200.133] => {
13 "msg": "hello tasks1"
14 }
15
16 TASK [include_tasks] ***************************************************************************************************************************
17 included: /ansible/import/bin/tasks2.yaml for 192.168.200.132, 192.168.200.133
18
19 TASK [tasks2] **********************************************************************************************************************************
20 ok: [192.168.200.132] => {
21 "msg": "hello tasks2"
22 }
23 ok: [192.168.200.133] => {
24 "msg": "hello tasks2"
25 }
26
27 PLAY RECAP *************************************************************************************************************************************
28 192.168.200.132 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
29 192.168.200.133 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-playbook文件复用的更多相关文章
- ansible playbook实践(四)-如何调试写好的playbook文件
有时,我们写了一个长长,功能很强悍的yaml文件,但是,我们有可能会担心,写的yaml文件是否正确,是否有漏洞危机,毕竟是要修改线上的机器,那么,有可能我们可以从以下几个检查维度来进行,确保在大规模应 ...
- ansible playbook模式及语法
一.什么是playbook及其组成 什么是playbook playbook 翻译过来就是"剧本" playbook的组成 play:定义的是主机的角色 task:定义的是具体执行 ...
- ansible playbook详解
ansible playbook是由yml语法书写,结构清晰,可读性强,所以必须掌握yml基础语法 语法 描述 缩进 YAML使用固定的缩进风格表示层级结构,每个缩进由两个空格组成,不能使用tabs键 ...
- Ansible playbook 编程
Ansible playbook 编程详解与各种小案例 主机规划 添加用户账号 说明: 1. 运维人员使用的登录账号: 2. 所有的业务都放在 /app/ 下「yun用户的家目录」,避免业务数据乱放: ...
- (三)ansible playbook
一,YAML语法 YAML的语法和其他高阶语言类似并且可以简单表达清单.散列表.标量等数据结构.(列表用横杆表示,键值对用冒号分割,键值对里又可以嵌套另外的键值对) YAML文件扩展名通常为.yaml ...
- Ansible playbook API 开发 调用测试
Ansible是Agentless的轻量级批量配置管理工具,由于出现的比较晚(13年)基于Ansible进行开发的相关文档较少,因此,这里通过一些小的实验,结合现有资料以及源码,探索一下Ansible ...
- ansible playbook批量改ssh配置文件,远程用户Permission denied
最近手里的数百台服务器需要改/etc/ssh/sshd_config的参数,禁止root直接登陆,也就是说 [root@t0 ~]# cat /etc/ssh/sshd_config | grep R ...
- ansible playbook 变量
变量优先级 在命令中定义的变量(-e参数指定的) 在inventory中定义的变量(ansible_ssh_user等) 其他变量(role中.play中) 系统通过father_facts定义的变量 ...
- ansible笔记(11):初识ansible playbook(二)
ansible笔记():初识ansible playbook(二) 有前文作为基础,如下示例是非常容易理解的: --- - hosts: test211 remote_user: root tasks ...
随机推荐
- Netty学习笔记-入门版
目录 Netty学习笔记 前言 什么是Netty IO基础 概念说明 IO简单介绍 用户空间与内核空间 进程(Process) 线程(thread) 程序和进程 进程切换 进程阻塞 文件描述符 文件句 ...
- Google Code Jam 2020 Round1B Blindfolded Bullseye
总结 这一题是道交互题,平时写的不多,没啥调试经验,GYM上遇到了少说交个十几发.一开始很快的想出了恰烂分的方法,但是没有着急写,果然很快就又把Test Set3的方法想到了,但是想到归想到,调了快一 ...
- 用于测试XmlAnalyzer 1.00版的八个测试用例
XmlAnalyzer 工程目的:将XML的属性和子节点按字母序排列,然后整形输出: 08. 原文=<project xmlns="http://maven.apache.org/PO ...
- centos7安装jdk11
我下载的网址是http://jdk.java.net/11/ 找安装包的事就说到这里了.我是因为公司用的jdk8,但是,我给个人研究东西的时候,目前定的版本是jdk11 .另外,现在基本全线转到了op ...
- flume读取日志文件并存储到HDFS
配置hadoop环境 配置flume环境 配置flume文件 D:\Soft\apache-flume-1.8.0-bin\conf 将 flume-conf.properties.template ...
- ArrayList源码剖析与代码实测
ArrayList源码剖析与代码实测(基于OpenJdk14) 目录 ArrayList源码剖析与代码实测(基于OpenJdk14) 继承关系 从构造函数开始 从add方法深入 / 数组的扩容 其他的 ...
- Hexo博客迁移
Hexo用户指南 - 博客迁移 GitHub+Hexo搭建博客的过程比较平滑,但是它的配置却非常耗时,一旦电脑出现问题或者需要在另外一台电脑上写博客,那么Hexo博客的迁移非常就让人头疼.下面参考其他 ...
- SpringAOP+源码解析,切就完事了
本文是对近期学习知识的一个总结,附带源码注释及流程图,如有不足之处,还望评论区批评指正. 目录 一.AOP.SpringAOP.AspectJ的区别 二.AOP关键术语 三.通知的五种类型 四.切入点 ...
- vsCode 设置vue文件标签内的style智能提示
VS Code 文件->首选项->设置 搜索:files.associations 点击在setting.json中编辑 最后一行添加配置: "files.association ...
- Mysql 多表连查 xml写法 非注解形式
1.xml写法 <!-- 联查用户users表 --> <resultMap type="nanh.entity.Tasks" id="selectTa ...