How to prevent ansible to suppress escaped quotes?

56 views
Skip to first unread message

Gaétan QUENTIN@Work

unread,
Aug 20, 2021, 10:47:10 AM8/20/21
to Ansible Project
Hi,


 in a json file we have this escaped quoted part:

{

 inputs: {

   "value": "my \" value \" is here"

[ .. ]


this json file is read from extra var at command line: -e "@inputs.json"


then a template jinja use it and create another json file . The j2
template use {{ inputs.value  }} which become:  "my  value  is here"


-> protected quotes have disappeared, which make our final system to hang.


How to prevent this?


Regards

--
-----------------
Gaétan

Only doctor Who can reboot the universe.}}

Felix Fontein

unread,
Aug 20, 2021, 11:14:00 AM8/20/21
to ansible...@googlegroups.com
Hi,

I tried to replicate this, but without success. The quotes were there.
So you probably have to provide more information.

> {
>
>  inputs: {
>
>    "value": "my \" value \" is here"
>
> [ .. ]

Please note that it must be "inputs" for a proper JSON file. Also
please note that the "value" line you inserted above contains some
special invisible characters that trip up both JSON and YAML parsers.

Cheers,
Felix

Gaétan QUENTIN

unread,
Aug 23, 2021, 6:32:39 AM8/23/21
to Ansible Project
Sorry i have simplified too much my example.
Here is the whole thing:

inputs_with_escaped_quotes.json :

{

    "inputs": {

        "value": "this is a \" escaped quoted \"  \" string \"  yeah..."

    }

}

 

 

Tasks :

Escaped_quotes.yml :

---

- name: escaped quotes set_fact

  set_fact:

    jsoncode: "{{ lookup('template', 'template_with_escaped_quotes.j2') }}"

 

- name: Save file

  local_action:

    module: copy

    content: "{{ jsoncode }}"

    dest: "{{ role_path }}/files/output.json"

 

 

Templates  template_with_escaped_quotes.j2:

{

    "result_value": "{{ inputs.value }}"

}

 

 

 

Json result : output.json :

{

    "result_value": "this is a " escaped quoted "  " string "  yeah..."

}



Regards

Stefan Hornburg (Racke)

unread,
Aug 23, 2021, 6:38:03 AM8/23/21
to ansible...@googlegroups.com
On 23/08/2021 12:32, Gaétan QUENTIN wrote:
> Sorry i have simplified too much my example.
> Here is the whole thing:
>

Using templates for structured data like XML and JSON is a bad idea in most cases (IMHO).

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/e542a036-a0e6-44f8-a1f7-2111a25bb703n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/e542a036-a0e6-44f8-a1f7-2111a25bb703n%40googlegroups.com?utm_medium=email&utm_source=footer>.


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

Dick Visser

unread,
Aug 23, 2021, 7:16:55 AM8/23/21
to ansible...@googlegroups.com
Yup.
I would suggest from_json and to_json etc.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f6e14f98-55b1-aa2c-1138-235ea05a627e%40linuxia.de.
>


--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Gaétan QUENTIN

unread,
Aug 23, 2021, 8:09:49 AM8/23/21
to Ansible Project
hi,

the inputs are read from extra variable :  -e "@inputs.json". So they  are already turned into dict. Not enough?

Regards
Gaétan

Brian Coca

unread,
Aug 23, 2021, 10:10:59 AM8/23/21
to Ansible Project
your `jsoncode` is a misnomer at that point since it will
automatically transform into a python structure internally, use
|string or |to_json to avoid this.

--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages