简体   繁体   English

结合使用url模块和jinja2模板

[英]Using url module with jinja2 templates

I know how to process jinja2 templates files and let them create files. 我知道如何处理jinja2 模板文件,并让他们创建文件。 I also know how to POST to webservices using the url module. 我也知道如何使用url模块发布到Web服务。 For now I use some code like this, which successfully posts hardcoded JSON to my remote service: 现在,我使用类似这样的代码,它将成功将硬编码的JSON发布到我的远程服务:

  tasks:
    - name: GSA app definition
      uri:
        url: "http://localhost:8764/api/apps?relatedObjects=false"
        method: POST
        force_basic_auth: yes
        user: "{{ admin_name }}"
        password: "{{ admin_pass }}"
        body_format: json
        body: "{\"name\":\"My new app\", \"description\":\"A really great new app\" }"
        follow_redirects: all
        status_code: 200
        timeout: 15
      register: app_gsa_cfg

But the JSON is static, how can I process a jinja2 template and POST its content ? 但是JSON是静态的,如何处理jinja2模板并发布其内容? I would prefer not having to create temporary files on disk and POST them, what I am looking for is a direct connection or perhaps an approach that puts the template processing result into a string. 我希望不必在磁盘上创建临时文件并将其发布,我正在寻找的是直接连接,或者也许是一种将模板处理结果放入字符串中的方法。

For starters a jinja2 template could look like this, later I will add variables too: 对于初学者来说,jinja2模板可能看起来像这样,稍后我也会添加变量:

{#
This file creates the basic GSA app in Fusion. See https://doc.lucidworks.com/fusion-server/4.2/reference-guides/api/apps-api.html#create-a-new-app for details
#}

{
  "name": "GSA",
  "description": "Contains all configuration specific to the migrated GSA legacy searches"
}

(I know that this has little advantage over a static json included into the playbook. But is is easier to edit and offers me the opportunity to have (jinja style) comments in Json, which is normally not possible) (我知道,与剧本中包含的静态json相比,这没有什么优势。但是它更易于编辑,并为我提供了在Json中使用(jinja风格)注释的机会,这通常是不可能的)

In my case, what I do is the following: 就我而言,我要做的是:

I have an API, so I do the following: 我有一个API,因此请执行以下操作:

- name: Change API Status
  uri:
    url: "{{ enpoint }}/v1/requests/{{ whatever }}"
    method: PATCH
    user: "{{ tokenid }}"
    password: x
    headers:
      X-4me-Account: "myaccount"
    body: '{ "status":"{{ reqstatus }}" }'
    body_format: json
    status_code:
      - 201
      - 200
    force_basic_auth: true
    validate_certs: false
    return_content: true

Then your reqstatus var will change. 然后,您的reqstatus var将更改。

Even you can add your whole text as yaml, import into a variable and convert with filters {{ some_variable | to_json }} 甚至您也可以将整个文本添加为​​yaml,导入变量并使用过滤器进行转换{{ some_variable | to_json }} {{ some_variable | to_json }}

Note: Have a look to the formatting without escaping quotes. 注意:不带引号引起来的格式。 That will help. 那会有所帮助。

It makes no sense creating a file with jinja2 if you are not going to copy it remotely. 如果您不打算远程复制文件,则用jinja2创建文件是没有意义的。 Ansible supports jinja natively but its strength is the possibility to have plugins for better maintainability. Ansible本地支持Jinja,但其优势在于可以使用插件来实现更好的可维护性。 There is no difference between template (or win_template ) modules unless (as said) you copy the file somewhere. template (或win_template )模块之间没有区别,除非( win_template )将文件复制到某处。 Look this example: 看这个例子:

---
- name: Adhoc Jinja
  hosts: localhost
  connection: local
  gather_facts: false

  vars:
    mytemplate:
      - name: "GSA"
        description: "Contains all configuration specific to the migrated GSA legacy searches"
      - name: "Another Name"
        description: "Contains Another Var"

  tasks:
    - name: Read Vars Loop
      debug:
        msg: "{{ item | to_json }}"
      with_items: "{{ mytemplate }}"

    - name: Include Vars
      include_vars: adhocjinja2.yml

    - name: Read Vars Loop
      debug:
        msg: "{{ item | to_json }}"
      with_items: "{{ mytemplate }}"

And adhocjinja2.yml: 和adhocjinja2.yml:

mytemplate:
  - name: "GSA2"
    description: "Contains all configuration specific to the migrated GSA legacy searches"
  - name: "Another Name 2"
    description: "Contains Another Var"

The output is: 输出为:

TASK [Read Vars Loop] **************************************************************************************
ok: [localhost] => (item={'name': 'GSA', 'description': 'Contains all configuration specific to the migrated GSA legacy searches'}) => {
    "msg": "{\"name\": \"GSA\", \"description\": \"Contains all configuration specific to the migrated GSA legacy searches\"}"
}
ok: [localhost] => (item={'name': 'Another Name', 'description': 'Contains Another Var'}) => {
    "msg": "{\"name\": \"Another Name\", \"description\": \"Contains Another Var\"}"
}

TASK [Include Vars] ****************************************************************************************
ok: [localhost]

TASK [Read Vars Loop] **************************************************************************************
ok: [localhost] => (item={'name': 'GSA2', 'description': 'Contains all configuration specific to the migrated GSA legacy searches'}) => {
    "msg": "{\"name\": \"GSA2\", \"description\": \"Contains all configuration specific to the migrated GSA legacy searches\"}"
}
ok: [localhost] => (item={'name': 'Another Name 2', 'description': 'Contains Another Var'}) => {
    "msg": "{\"name\": \"Another Name 2\", \"description\": \"Contains Another Var\"}"
}

You can manage your variables as you want and create your json on the fly as Ansible has jinja and json it its heart. 您可以根据需要管理变量,并快速创建json,因为Ansible将jinja和json作为其核心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM