ansible - 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 上完美缩进,但不适用于我的输出文件。
谢谢。
解决方案
您可以尝试其中一种格式过滤器,例如:
- name: Saving logs to output
copy:
content: "{{ json_response.stdout | to_nice_json(indent=4) }}"
dest: "./output.txt"
更多细节和示例可以在Ansible 文档中找到。
推荐阅读
- java - 如何在 JDK 9 或更高版本中获取引导类路径
- java - 为什么这段代码使用 Math.pow 打印“HELLO WORLD”?
- r - 按到邻居的距离散点图颜色
- javascript - 在AngularJS函数中转换javascript函数
- spring-boot - Sec:authorize 在 Spring Security 4 中不起作用
- sparql - 查找出生在给定城市的所有艺术家
- python - 分配前引用的python UnboundLocalError
- javascript - onClick / onItemClick JSX 语法 React + Office UI Fabric
- c++ - C ++:在子目录中加载dll - 清单
- sql-server - 内存中的 SQL Server 等效项