簡體   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