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 ...
随机推荐
- java初探(1)之秒杀的业务简单实现
前言 秒杀的业务场景广泛存在于电商当中,即有一个倒计时的时间限制,当倒计时为0时,秒杀开始,秒杀之后持续很小的一段时间,而且秒杀的商品很少,因此会有大量的顾客进行购买,会产生很大的并发量,从而创造技术 ...
- Activiti7 表介绍
由于Activiti自生成的表较多,这里先对activiti自生成数据库表进行介绍. 数据库表的创建在后续的demo文章中进行介绍,并且后续会写一篇关于数据库详解的文章,这里先大概知道Activiti ...
- 状压dp:luogu P2704 [NOI2001]炮兵阵地
https://www.luogu.org/problemnew/show/P2704 知识点:1.滚动数组:取模实现 2.位运算优先级最低 顾是if(!(a&b))而不是if(!a& ...
- 关于取表中id最大值+1的select语句,哪种效率更高?
需求:取stock表中id最大值+1,作为下一个id值. 特殊情况:考虑到表中会没有值,max(id)会返回空,因此需要用case when进行判断. 实现一:select (case max(id) ...
- 解锁用户scott并授权
请输入用户名: system 输入口令: 连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Producti ...
- LAMP环境之编译安装httpd服务
“Apache HTTP Server”是开源软件项目的杰出代表,它基于标准的 HTTP 网络协议提供网页浏览服务. 在配置 Apache 网站服务之前,需要正确安装好 httpd 服务器软件.htt ...
- jmeter远程调用
jmeter版本相同 JDK版本1.7以上 脚本文件在所有机器上的路径都一致 修改远程机器jmeter bin目录下的jmeter.properties文件配置:server_port=1001 多个 ...
- git远程仓库创建及代码提交
git仓库创建:1. mkdir project-repertory2. cd project-repertory3. git init --bare 此时,git仓库已成功创建. 本地要提交的代码在 ...
- native到CPU
Native 所谓的native准确的说是借由虚拟机实现的JNI接口调用的操作系统提供的API JNI使得class中的ACC_NATIVE标至的方法能借由JNI类的实例转换为JNI规范(如全限定名) ...
- TCP连接问题之CLOSE_WAIT和TIME_WAIT过多
参考博文 https://dengqsintyt.iteye.com/blog/2086485 Timeout waiting for connection异常排查:https://blog.csdn ...