【发布时间】:2021-01-10 19:26:25
【问题描述】:
播放:
- 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 中将列表转换为字符串? 如果没有,任何其他建议将不胜感激以实现“预期输出”`
【问题讨论】:
-
您能否准确指出您在
definition变量的实际内容中看到列表/字典的 yaml 或 json 表示的位置?从我所看到的最好的情况来看,您可以将该字符串拆分为新行,以将其输出为应该更具可读性的行列表:msg: "{{ irules.split('\n') }}"。除非您找到能够解析特定语言/表示的模块/过滤器。 -
感谢您的回复 { "msg": [ { "definition": "when HTTP_REQUEST {\n switch -glob [HTTP::uri] {\n \"*HAC*\" {池 char.hr.cal.ed.HAC.pool }\n \n }\n}", ] }
-
我尝试使用 msg: "{{ irules.split('\n') }}" 进行拆分,结果出现错误。该任务包括一个带有未定义变量的选项。错误是:“列表对象”没有属性“拆分”\n\n
-
好的,知道了。那么它应该是
msg: "{{ irules[0].split('\n') }}".... 或者您必须使用能够理解该语言/表示的过滤器来解析每个列表元素。