0

I have a bit of JSON I need to send to an API via Ansible, URI module.

It's failing due to an undefined var in the JSON. Specifically It's failing due to this line.

"message": "{{message}}"

The thing here is that I need this line as is. There is no var meant to substitute {{message}}, the line has to be...

"message": "{{message}}"

How do I tell Ansible or the URI module to just send the contents as it is?

Original JSON

    "message":"{{ policy.message | default('{{message}}') }}",

I've not tried slapping {% raw %}, {% endraw %} lines to the start and end of the JSON, because I do need to do some logic to it first.

Also it's failing for that var because it's the first one like that in the JSON. I have 5 more like it in there.

The Task

As I think it will help clear some things up, here are the tasks...

- name: apply_policy.yml | render policy
  set_fact:
    _policy_rendered: "{{ lookup('template', 'policy.json.j2') | string }}"
  check_mode: no

# Calls API with POST method to CREATE policy
- name: apply_policy.yml | CREATE policy
  uri:
    url:  https://api.xxxxx/policies/
    headers:
      content-type: application/json
      Host: api.xxxxx.com
      Connection: keep-alive
      Authorization: GenieKey {{key}}
    method: POST
    body: "{{ _policy_rendered }}"
    return_content: yes
    body_format: json
    status_code: [201]

U880D
  • 8,601
  • 6
  • 24
  • 40
  • 2
    Does this answer your question? [Escape jinja2 syntax in a jinja2 template](https://stackoverflow.com/questions/25359898/escape-jinja2-syntax-in-a-jinja2-template) – β.εηοιτ.βε Sep 30 '22 at 07:09
  • 1
    You should use 'raw' indeed. Also, for readability you might use https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines – Kevin C Sep 30 '22 at 07:47
  • @β.εηοιτ.βε unfortunately not. If I were to do that in my uri module, it would interpret the `{{ _policy_rendered }}` as the raw contents to use, not the contents of the var. @KevinC that would help, but I need to render the json to produce that line int he first place. Then I need it not to try and render it again. – user2519653 Sep 30 '22 at 09:13

0 Answers0