如何在字典中将字典或列表转换为字符串

时间:2020-09-24 13:28:17

标签: json ansible

播放:

  - set_fact:
      irules: "{{ rule | json_query('[*].definition') }}"
  - debug:
      msg: "{{ irules }}"

输出:

"msg": [
    "when HTTP_REQUEST {\n    switch -glob [HTTP::uri] {\n    \"*HAC*\" { pool char.hr.cal.ed.ABC.pool }\n        \n     }\n}"
]

}

我希望输出格式如下。

预期输出:

"when HTTP_REQUEST {
     switch -glob [HTTP::uri] {
     "*HAC*" { pool char.hr.cal.ed.ABC.pool }   
 }

我尝试将“ from_json”解析为上述播放,并出现错误:

"({{ rule | json_query('[*].definition') | from_json }}): the JSON object must be str, bytes or ``bytearray, not 'list'"}" 

有没有一种方法可以将清单转换为ansibe中的字符串? 否则,将不胜感激任何其他建议以实现“预期的输出”`

1 个答案:

答案 0 :(得分:0)

Ansible的模块debug无法打印回车。它总是将它们转义(将它们转换为\n)。这就是为什么您在调试打印消息中有许多\n的原因。

我知道输出多行变量的唯一选择是使用pause模块:

- pause:
    seconds: 1
    prompt: "{{ irules[0] }}"

更详细的答案here可以提供帮助。

相关问题