首页 > 解决方案 > 如何跟踪现有的 ansible 项目

问题描述

我不太确定如何跟踪用 YAML 编写的用于网络设备的现有项目。我已正确设置系统并完美执行所有任务。但我想检查所有数据都被分配了什么。

有没有办法像 python 一样跟踪 ansible?

例如:在 python 中,我可以使用 ipdb 模块或只使用 print() 语句来查看各种东西。

标签: networkingautomationansibleyaml

解决方案


Ansible 提供了一个Playbook Debugger,可用于跟踪任务的执行。

如果你想调试一个剧中的所有东西,你可以通过debugger: always

- name: some play
  hosts: all
  debugger: always
  tasks: ...

然后您可以使用c命令继续下一个任务,p task_vars查看变量或p result._result查看结果。

调试器也可以像这样用于任务或角色级别:

- hosts: all
  roles:
    - role: dj-wasabi.zabbix-agent
      debugger: always

它有助于不让debug任务污染你的角色,同时限制调试的范围。

另一种方法是使用debug module,类似于在 python 中使用 print 语句。您可以像这样在您的任务中使用:

# Example that prints the loopback address and gateway for each host
- 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

# Example that prints return information from the previous task
- shell: /usr/bin/uptime
  register: result

- debug:
    var: result
    verbosity: 2

推荐阅读