有时,我们写了一个长长,功能很强悍的yaml文件,但是,我们有可能会担心,写的yaml文件是否正确,是否有漏洞危机,毕竟是要修改线上的机器,那么,有可能我们可以从以下几个检查维度来进行,确保在大规模应用之前已经被充分检查。

检查三步骤:

第一步:

加上--syntax-check来检查你的playbook语法是否正确:

[root@localhost playbook]# ansible-playbook -v --syntax-check template.yml

Using /etc/ansible/ansible.cfg as config file

playbook: template.yml

第二步:加上--check,--diff和-i "xx.xx.xx.xx,"在单台机器跑一遍看看预期输出

[root@localhost playbook]# ansible-playbook -v --diff --check template.yml

第三步:加上去掉--check,只跑测试机(或一台不重要的机器)上试一下,看下结果是否符合预期。

另外,有时我们写的yaml文件中包含了一此变量,我们担心变量替换后是否会有一些值不适合,该如何看变量替换成真实值后yaml文件的真实情况呢?我们可以借助template模块来进行:

[root@localhost playbook]# cat template.yml
---
- hosts: all
remote_user: root
gather_facts: no
vars:
cmd: echo 'hello world'
tasks:
- name: final yaml
template: src=/etc/ansible/playbook/template.yml dest=/tmp/template.yml backup=yes
run_once: true
delegate_to: 127.0.0.1
tags:
- g_yaml - name: exec shell
shell: "{{ cmd }}"

run_once表示此模块只跑一次,delegate_to表示转到在本机运行,然后给这个任务打个tag,叫g_yaml,运行时命令如下:

[root@localhost playbook]# ansible-playbook -v -i "127.0.0.1," --tag g_yaml template.yml
Using /etc/ansible/ansible.cfg as config file PLAY [all] ******************************************************************************** TASK [final yaml] *************************************************************************
ok: [127.0.0.1 -> 127.0.0.1] => {"changed": false, "checksum": "db12f54ebb55be35a1731ff9a5a20233afb3b84f", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "path": "/tmp/template.yml", "size": 352, "state": "file", "uid": 0} PLAY RECAP ********************************************************************************
127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0

注意加上只运行某个tag,这样就能确保只有这个任务被执行,而其它任务不会被执行。由于这里已经有run_once: true,所以加不加上-i "127.0.0.1," 关系不大。

再在本机查看煊染后的输出:

[root@localhost playbook]# cat /tmp/template.yml
---
- hosts: all
remote_user: root
gather_facts: no
vars:
cmd: echo 'hello world'
tasks:
- name: final yaml
template: src=/etc/ansible/playbook/template.yml dest=/tmp/template.yml backup=yes
run_once: true
delegate_to: 127.0.0.1
tags:
- g_yaml - name: exec shell
shell: "echo 'hello world'"

这就是我们真正执行时的内容。当然,如果有些变量是引用远程主机的值,如ip等,那就把这个delegate_to去掉,把-i里面的ip替换成远程主机ip,就可以了,如下:

ansible-playbook -v -i "xx.xx.xx.xx," --tag g_yaml template.yml

ansible中还有一个debugger,当出错时用来详细观察输出调试信息,使用方法为,加上strategy: debug:

[root@localhost playbook]# cat debugger.yml
---
- hosts: all
strategy: debug
gather_facts: no
vars:
var1: value1
tasks:
- name: ping
ping: data={{ wrong_var }}

执行如下:

[root@localhost playbook]# ansible-playbook -i "192.168.40.72," debugger.yml 

PLAY [all] ********************************************************************************

TASK [ping] *******************************************************************************
fatal: [192.168.40.72]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'wrong_var' is undefined\n\nThe error appears to have been in '/etc/ansible/playbook/debugger.yml': line 8, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: ping\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'wrong_var' is undefined"}
Debugger invoked
(debug) p result
{'failed': True,
'msg': u"The task includes an option with an undefined variable. The error was: 'wrong_var' is undefined\n\nThe error appears to have been in '/etc/ansible/playbook/debugger.yml': line 8, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: ping\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'wrong_var' is undefined"}
(debug) p task.args
{u'data': u'{{ wrong_var }}'}
(debug) task.args['data'] = '{{ var1 }}'
(debug) p task.args
{u'data': '{{ var1 }}'}
(debug) redo
ok: [192.168.40.72] PLAY RECAP ********************************************************************************
192.168.40.72 : ok=1 changed=0 unreachable=0 failed=0

调试模式下支持如下的命令:

1. p task/vars/host/result 打印值
2. task.args[key] = value 修改task中的参数值
3. vars[key] = value 修改变量值
4. r(edo) 重跑这个失败的任务
5. c(ontinue) 继续任务
6. q(uit) 退出调试,整个执行过程也会终止

关于第二和第三点,一个是修改参数值,一个是修改变量值,这里补充再做个说明:

- hosts: test
strategy: debug
gather_facts: yes
vars:
pkg_name: not_exist
tasks:
- name: install package
apt: name={{ pkg_name }} 执行后的输出
(debug) p task.args
{u'name': u'{{ pkg_name }}'}
(debug) task.args['name'] = 'bash'
(debug) p task.args
{u'name': 'bash'}
(debug) redo 这里面name为任务中的参数值 或者:
(debug) p vars['pkg_name']
u'not_exist'
(debug) vars['pkg_name'] = 'bash'
(debug) p vars['pkg_name']
'bash'
(debug) redo
这里面pkg_name为playbook中的变量值

如上信息应该可以帮你写出一个更好的playbook。

ansible playbook实践(四)-如何调试写好的playbook文件的更多相关文章

  1. Android最佳性能实践(四)——布局优化技巧

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43376527 在前面几篇文章其中.我们学习了怎样通过合理管理内存,以及高性能编码技 ...

  2. js介绍,js三种引入方式,js选择器,js四种调试方式,js操作页面文档DOM(修改文本,修改css样式,修改属性)

    js介绍 js运行编写在浏览器上的脚本语言(外挂,具有逻辑性) 脚本语言:运行在浏览器上的独立的代码块(具有逻辑性) 操作BOM 浏览器对象盒子 操作DOM 文本对象 js三种引入方式 (1)行间式: ...

  3. 【实践报告】Linux实践四

    Linux内核分析 实践四——ELF文件格式分析 一.概述 1.ELF全称Executable and Linkable Format,可执行连接格式,ELF格式的文件用于存储Linux程序.ELF文 ...

  4. nodejs 实践:express 最佳实践(四) express-session 解析

    nodejs 实践:express 最佳实践(四) express-session 解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs ...

  5. I2C(四)linux3.4(写代码)

    title: I2C(四)linux3.4(写代码) date: 2019/1/29 17:18:42 toc: true --- I2C(四)linux3.4(写代码) 老师的参考代码 https: ...

  6. WebSocket原理与实践(四)--生成数据帧

    WebSocket原理与实践(四)--生成数据帧 从服务器发往客户端的数据也是同样的数据帧,但是从服务器发送到客户端的数据帧不需要掩码的.我们自己需要去生成数据帧,解析数据帧的时候我们需要分片. 消息 ...

  7. Ansible Tower系列 四(使用tower执行一个命令)【转】

    在主机清单页面中,选择一个主机清单,进入后,选择hosts里的主机 Paste_Image.png 点击 RUN COMMANDS MODULE 选择 commandARGUMENTS 填写 ifco ...

  8. Linux及安全实践四——ELF文件格式分析

    Linux及安全实践四——ELF文件格式分析 一.ELF文件格式概述 1. ELF:是一种对象文件的格式,用于定义不同类型的对象文件中都放了什么东西.以及都以什么样的格式去放这些东西. 二.分析一个E ...

  9. 孤荷凌寒自学python第七十四天开始写Python的第一个爬虫4

    孤荷凌寒自学python第七十四天开始写Python的第一个爬虫4 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...

随机推荐

  1. hbmy周赛1--A

    Age Sort You are given the ages (in years) of all people of a country with at least 1 year of age. Y ...

  2. 除了四大传统OA软件商,国内还有这些优秀的OA协同产品

    国内OA 软件市场经过多年的发展,在产品.应用.服务方面都已相对成熟,也出现了众多优秀的OA软件品牌. 据中国软件协会2017年公布的数据显示:泛微OA.致远OA.华天动力OA.蓝凌OA的销售仍稳居O ...

  3. Spark算子--foreach和foreachPartition

    转载请标明出处http://www.cnblogs.com/haozhengfei/p/6776fe93f754daf60d00d2cb509422a1.html foreach和foreachPar ...

  4. TP5使用phpmailer实现邮件发送

    1.从github下载PHPMailer,在vendor目录中新建文件夹phpmailer,将压缩包中的class.phpmailer.php和class.smtp.php复制到phpmailer中, ...

  5. ip 淘宝ip库 精简版

    <?php header('Content-type: text/html; charset=utf-8'); //根据ip获取城市.网络运营商等信息 function findCityByIp ...

  6. hasResultError

    hasResultError 的作用是 让返回的对象可以报错误信息.

  7. Linux IO时事检测工具iostat

    Linux IO时事检测工具iostat iostat命令用于检测linux系统io设备的负载情况,运行iostat将显示自上次运行该命令以后的统计信息.用户可以通过指定统计的次数和时间来获得所需的统 ...

  8. 使用 IDEA和Maven 整合SSH框架

    1.创建web工程 一路next 下去就行.完成后,IDEA会自动构建maven工程. 2.创建如下项目结构 需要将 java文件夹设置为SourcesRoot目录,否则无法创建package 设置操 ...

  9. python简单词频统计

    任务 简单统计一个小说中哪些个汉字出现的频率最高 知识点 文件操作 字典 排序 lambda 代码 import codecs import matplotlib.pyplot as plt from ...

  10. 武侠--生活--java

    一.名词解释 1.向上转型 大白话:村支书通知你爸去大队领过年发的面粉,结果你爸不在家,你装成你爸去了,村支书一看,行,你具有你爸的所有功能,就给了你. 官方解释:子类引用的对象转换为父类类型称为向上 ...