How to convert dict or list to string in ansible

2,407 views
Skip to first unread message

Devops testing

unread,
Sep 24, 2020, 9:46:16 AM9/24/20
to Ansible Project
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"`  

 

Stefan Hornburg (Racke)

unread,
Sep 24, 2020, 10:25:15 AM9/24/20
to ansible...@googlegroups.com
On 9/24/20 3:46 PM, Devops testing wrote:
> 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

>
>  
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/79aca988-710a-4671-b967-a70d55052871n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/79aca988-710a-4671-b967-a70d55052871n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Devops testing

unread,
Sep 24, 2020, 12:14:38 PM9/24/20
to ansible...@googlegroups.com
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 }

     }
}:


You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/-hbUslJhn5s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4be02972-e14f-59f0-7f40-6ec1090f7b11%40linuxia.de.
Reply all
Reply to author
Forward
0 new messages