有没有办法在ansible的json body中插入原始转义的json

Is there a way to insert raw escaped json in json body in ansible

请参阅下面第一个有效的调用。这是我如何在我的系统上创建模板的示例。请注意,我必须使用 {% raw %} ... {% endraw %} 这样 ansible 就不会尝试解释我模板中的变量。

    - name: Create a template : OK
      uri:
        url: https://{{ ip }}/api/v1/templates
        method: POST
        headers:
          Authorization: "{{ token }}"
          Content-Type: application/json
        body: |
          {
            "name": "{{ template_name }}",
            "type": "v1",
            "description": "{{ template_name }}",
            "content": "template: |  {% raw %}\n    {\n   \"class\": \"ABC\",\n      \"param\": {{param1::integer}}\n  }{% endraw %}"
          }
        body_format: json
        timeout: 60
        status_code: 202
        validate_certs: false
      register: json_response

输出正常:

    ok: [notahost] => {
        "invocation": {
            "module_args": {
                "body": {
                    "content": "template: |  \n    {\n   \"class\": \"ABC\",\n      \"param\": {{param1::integer}}\n  }", 
                    "description": "test1", 
                    "name": "test1", 
                    "type": "v1"
                }, 
                "body_format": "json", 

现在,我正尝试将模板的内容移到名为 template1.j2 的文件中。

template1.j2:

    {% raw %} 
    {
      "class": "ABC",
      "param": {{param1::integer}}
    } 
    {% endraw %}

我将模板 template1.j2 插入到我的 JSON 正文中(注意到我在模板中添加了 {% raw %} ... {% endraw %})。

    - name: Create a template NOK
      uri:
        url: https://{{ ip }}/api/v1/templates
        method: POST
        headers:
          Authorization: "{{ token }}"
          Content-Type: application/json
        body: |
          {
            "name": "{{ template_name }}",
            "type": "v1",
            "description": "{{ template_name }}",
            "content": "template: | {{ lookup('file','template1.j2') }}"
          }
        body_format: json
        timeout: 60
        status_code: 202
        validate_certs: false
      register: json_response

输出NOK:

    fatal: [notahost]: FAILED! => {
        "content": "{\"message\":\"request body has an error: failed to decode request body: invalid character '\\n' in string literal\"}\n", 
        "invocation": {
            "module_args": {
                "body": "{\n  \"name\": \"test2\",\n  \"type\": \"v1\",\n  \"description\": \"test2\",\n  \"content\": \"template: |  \n{\n    \"class\": \"ABC\",\n    \"param\": {{param1::integer}}\n} \n\"\n}\n", 
                "body_format": "json", 

出于某种原因,看起来我这样做的方式不起作用,ansible 仍然尝试在创建时解释我的模板中的变量。

关于如何让他在 ansible 任务之外使用我的模板工作有什么想法吗?

PS:我尝试使用 shell ansible 模块加载模板文件,但没有帮助。

感谢和问候, 罗曼

使用 lookup 'file' 而不是 'template',例如模板(您实际上不想在此任务中用作模板)

shell> cat template1.j2
param: {{param1.integer}}

和戏

- hosts: localhost
  vars:
    param1:
      integer: 99
  tasks:
    - debug:
        msg: |
          {{ lookup('template', 'template1.j2') }}
          {{ lookup('file', 'template1.j2') }}

给予

  msg: |-
    param: 99
  
    param: {{param1.integer}}

给定模板

shell> cat template1.j2
{
  "class": "ABC",
  "param": {{param1::integer}}
}

下面的剧本展示了如何使用和不使用模板

创建body
- hosts: localhost

  vars:
    template_name: test1

    body1: |
      {
      "name": "{{ template_name }}",
      "type": "v1",
      "description": "{{ template_name }}",
      "content": "template: |  {% raw %}\n    {\n   \"class\": \"ABC\",\n      \"param\": {{param1::integer}}\n  }{% endraw %}"
      }

    body2:
      {
      "name": "{{ template_name }}",
      "type": "v1",
      "description": "{{ template_name }}",
      "content": "template: |  \n{{ lookup('file','template1.j2') }}"
      }

  tasks:
    - debug:
        var: body1|type_debug
    - debug:
        var: body1
    - debug:
        var: body2|type_debug
    - debug:
        var: body2

给予

ok: [localhost] => 
  body1|type_debug: dict

ok: [localhost] => 
  body1:
    content: |-
      template: |
          {
         "class": "ABC",
            "param": {{param1::integer}}
        }
    description: test1
    name: test1
    type: v1
ok: [localhost] => 
  body2|type_debug: dict

ok: [localhost] => 
  body2:
    content: |-
      template: |
      {
        "class": "ABC",
        "param": {{param1::integer}}
      }
    description: test1
    name: test1
    type: v1