如何跟踪现有的ansible项目

how to trace an existing ansible project

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

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

例如:在 python 中,我可以使用 ipdb 模块或仅使用 print() 语句来查看所有类型的内容。

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中使用打印语句。您可以像这样在任务中使用:

# 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