Ansible内容的测试与持续集成指南
1. 调试与断言
对于大多数Ansible剧本而言,在执行过程中测试配置更改和命令执行结果就足以满足测试需求。借助Ansible的一些内置实用模块在剧本运行期间进行测试,能让我们立即确认系统是否处于正确状态。若有可能,应尽量将所有简单测试用例(如比较和状态检查)融入剧本中,Ansible提供了三个模块来简化这一过程。
1.1 debug模块
在积极开发Ansible剧本时,或者出于历史日志记录目的(例如使用Tower或其他CI系统运行剧本),在剧本执行期间打印变量值或特定命令的输出往往很有用。Ansible的 debug
模块可用于在剧本执行期间打印变量或消息。
以下是构建剧本时常用的两种使用 debug
的方式示例:
---
- hosts: 127.0.0.1
gather_facts: no
connection: local
tasks:
- name: Register the output of the 'uptime' command.
command: uptime
register: system_uptime
- name: Print the registered output of the 'uptime' command.
debug:
var: system_uptime.stdout
- name: Print a message if a command r