繁体   English   中英

结合使用url模块和jinja2模板

[英]Using url module with jinja2 templates

我知道如何处理jinja2 模板文件,并让他们创建文件。 我也知道如何使用url模块发布到Web服务。 现在,我使用类似这样的代码,它将成功将硬编码的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

但是JSON是静态的,如何处理jinja2模板并发布其内容? 我希望不必在磁盘上创建临时文件并将其发布,我正在寻找的是直接连接,或者也许是一种将模板处理结果放入字符串中的方法。

对于初学者来说,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"
}

(我知道,与剧本中包含的静态json相比,这没有什么优势。但是它更易于编辑,并为我提供了在Json中使用(jinja风格)注释的机会,这通常是不可能的)

就我而言,我要做的是:

我有一个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

然后,您的reqstatus var将更改。

甚至您也可以将整个文本添加为​​yaml,导入变量并使用过滤器进行转换{{ some_variable | to_json }} {{ some_variable | to_json }}

注意:不带引号引起来的格式。 那会有所帮助。

如果您不打算远程复制文件,则用jinja2创建文件是没有意义的。 Ansible本地支持Jinja,但其优势在于可以使用插件来实现更好的可维护性。 template (或win_template )模块之间没有区别,除非( win_template )将文件复制到某处。 看这个例子:

---
- 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 }}"

和adhocjinja2.yml:

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

输出为:

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\"}"
}

您可以根据需要管理变量,并快速创建json,因为Ansible将jinja和json作为其核心。

暂无
暂无

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

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