You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Ansible debug模块将含多个\n的字符串按多行打印?

Ansible多行字符串打印问题解决

问题出在你debug任务的写法上:给report_string套了双引号,同时使用YAML块标量(|),导致Ansible将字符串内的换行符转义为\n,最终输出成单行。

以下是两种可行的解决方法:

方法一:使用debug模块的var参数(最简洁)

直接指定要打印的变量名,Ansible会保留原始的多行格式:

- name: show the report_string
  debug:
    var: report_string

方法二:调整msg参数的写法

去掉包裹变量的双引号,直接在块标量中引用变量:

- name: show the report_string
  debug:
    msg: |
      {{ report_string }}

或者也可以省略块标量,直接引用变量:

- name: show the report_string
  debug:
    msg: "{{ report_string }}"

以上两种写法都能让Jinja2正确渲染变量中的换行符,输出多行格式的内容。

内容的提问来源于stack exchange,提问作者Tms91

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 04:18:18