我知道如何处理jinja2 templates文件并让他们创建文件。我也知道如何使用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风格)注释的机会,这通常是不可能的)
答案 0 :(得分:0)
就我而言,我的工作如下:
我有一个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
变量将更改。
即使您可以将整个文本添加为yaml,导入变量并使用过滤器{{ some_variable | to_json }}
注意:不带引号引起来的格式。那会有所帮助。
如果您不打算远程复制文件,那么用jinja2创建文件是没有意义的。 Ansible本地支持Jinja,但其优势在于可以使用插件来实现更好的可维护性。 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\"}"
}
您可以根据需要管理变量,并由于Ansible拥有jinja和json的存在而动态创建json。