0

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"`

Sridhar
  • 1
  • 2
  • Can you kindly point exactly where you see a yaml or json representation of a list/dict in the actual content of the `definition` variable ? At best from what I see, you can split that string on new lines to output it as a list of lines which should be more readable: `msg: "{{ irules.split('\n') }}"`. Unless your find a module/filter able bo parse the particular language/representation. – Zeitounator Sep 24 '20 at 14:35
  • Thank you for your response { "msg": [ { "definition": "when HTTP_REQUEST {\n switch -glob [HTTP::uri] {\n \"*HAC*\" { pool char.hr.cal.ed.HAC.pool }\n \n }\n}", ] } – Sridhar Sep 24 '20 at 14:52
  • I tried to split with msg: "{{ irules.split('\n') }}" and ended up with error. The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'split'\n\n – Sridhar Sep 24 '20 at 14:57
  • Ok got it. then it should be `msg: "{{ irules[0].split('\n') }}"`.... or you would have to parse every list element with a filter which understands that language/representation. – Zeitounator Sep 24 '20 at 15:04

1 Answers1

0

Module debug of Ansible cannot print carriage return. It always escape them (converting them into \n). That's why you have many \n in your debug print msg.

The only option I know to output multiline variable is to use the pause module :

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

More detailled answer here can help.

Jean-Pierre Matsumoto
  • 1,917
  • 1
  • 18
  • 26
  • Thanks a lot jean. I'm able to print in expected format. 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 – Sridhar Sep 24 '20 at 15:15
  • Your variable content is already fine. It contains carriage returns as you'd like. You can already write the variable content in a file. – Jean-Pierre Matsumoto Sep 24 '20 at 16:25
  • I have used split msg: "{{ irules[0].split('\n') }}" and copied to a file and content stored in a file is: ["when HTTP_REQUEST {", " switch -glob [HTTP::uri] {", " \"*HAC*\" { pool char.hr.cal.ed.HAC.pool }", " ", " }", "}"] I would like to remove characters [ and " and \ from the file and the format it to similar to expected output.. – Sridhar Sep 24 '20 at 18:51