首页 > 解决方案 > Ansible/Junos_OS - 变量的返回不缩进

问题描述

所以我以这个简单的剧本为例:

- name: Show interfaces
  junos_command:
    commands:
    - show interfaces
    display: text
  register: json_response

我需要将 json_response 保存到文件中:

- name: Saving logs to output
  copy:
    content: "{{ json_response.stdout }}" 
    dest: "./output.txt" 

我知道 json_response.stdout_lines 具有真正有组织的 json,但是当我保存它时,它全部未缩进,如果我使用“json_response.stdout”它会“缩进”但他不承认 '\n' 作为断线字符,所以我执行另一个任务将 \n 替换为断线。我的问题是,无论如何我可以正确保存 json_response 变量吗?当我执行剧本时,调试变量会在我的 shell 上完美缩进,但不适用于我的输出文件。

谢谢。

标签: ansible

解决方案


您可以尝试其中一种格式过滤器,例如:

- name: Saving logs to output
  copy:
    content: "{{ json_response.stdout | to_nice_json(indent=4) }}" 
    dest: "./output.txt" 

更多细节和示例可以在Ansible 文档中找到。


推荐阅读