How to convert dict or list to string in ansible

play:

  • set_fact:
    irules: “{{ rule | json_query(‘[*].definition’) }}”
  • debug: msg: “{{ irules }}”

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

I’m expecting output to be in below format.

Expected output:
“when HTTP_REQUEST {
switch -glob [HTTP::uri] {
HAC” { pool char.hr.cal.ed.ABC.pool }
}”

I have tried parsing “from_json" to the above play and ended up with the error:
“({{ rule | json_query(‘[*].definition’) | from_json }}): the JSON object must be str, bytes or ``bytearray, not ‘list’”}”

is there a way to convert list to string in ansibe? if not, any other suggestions would be appreciated to achieve “Expected output”`

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

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

I'm expecting output to be in below format.

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

I have tried parsing "*from_json"* to the above play and ended up with the error:
"({{ rule | json_query('[*].definition') | from_json }}): the JSON object must be str, bytes or ``bytearray, not 'list'"}"

is there a way to convert list to string in ansibe? if not, any other suggestions would be appreciated to achieve
"Expected output"`

As a json_query can have multiple results, the result of that filter is a list.

To get the first result you can use irules[0] or irules | first.

Regards
         Racke

Hi Racke,

Thank you for your response. I have used pause module and it got printed as expected. Is there a way to store that printed content in variable? I have to copy that content to a file and add few extra lines to that file and have to apply that configuration content to firewall. Please let me now if you need any info

play

  • pause:
    seconds: 1
    prompt: “{{ irules[0] }}”

output:
Pausing for 1 seconds
(ctrl+C then ‘C’ = continue early, ctrl+C then ‘A’ = abort)
[pause]
when HTTP_REQUEST {
switch -glob [HTTP::uri] {
HAC” { pool char.hr.cal.ed.HAC.pool }

}
}: