ansible常用模块 -- debug模块 -- 在执行期间打印语句
debug模块 – 在执行期间打印语句
一、摘要
- 该模块在执行过程中打印语句,对于调试变量或表达式非常有用,而不必停止剧本。
- 与’ when: '指令一起调试很有用。
- Windows目标也支持此模块。
二、参数
参数 | 选项/默认值 | 描述 |
---|---|---|
msg string | Default: “Hello world!” | 打印的定制消息。如果省略,则打印通用消息。 |
var string | 要调试的变量名。 与msg选项互斥。 请注意,该选项已经在Jinja2上下文中运行,并且具有隐式{{}}包装,因此您不应该使用Jinja2分隔符,除非您正在寻找双插值。 | |
verbosity integer | Default: 0 | 一个控制何时运行调试的数字,如果将其设置为3,则只在-vvv或以上时运行调试 |
三、示例
# 打印每个主机的环回地址和网关的示例
- debug:
msg: System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}
- debug:
msg: System {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }}
when: ansible_default_ipv4.gateway is defined
# 示例,打印从上一个任务返回的信息
- shell: /usr/bin/uptime
register: result
- debug:
var: result
verbosity: 2
- name: Display all variables/facts known for a host
debug:
var: hostvars[inventory_hostname]
verbosity: 4
# 示例,打印两行消息,但仅在设置了环境值的情况下
- debug:
msg:
- "Provisioning based on YOUR_KEY which is: {{ lookup('env', 'YOUR_KEY') }}"
- "These servers were built using the password of '{{ password_used }}'. Please retain this for later use."
官方文档:https://docs.ansible.com/ansible/2.9/modules/debug_module.html#debug-module