使用具有动态变量[Ansible]的查找模板



我正在使用查找模板插件来创建一个具有动态变量value1的json,该变量将在http请求中作为主体发送。

api返回了一个错误,因为我的json语法不正确。事实上,它已被更改值1变量损坏。

ansible似乎试图将字符串value1添加到json结构Settings.json中,该结构创建了一个损坏的json(检查输出(

Settings.json

{
"FS": {
"key1": "{{ value1 }}",
"key2": "{{ value2 }}",
"key3": "{{ value3 }}" 
}
}

Yaml任务

- set_fact: 
value1: "C:\Users\adminlocal\AppData\Local\Temp\ansible.44q4u5lu.mtobuild\SmokeTest"
- debug: 
var: event_settings
vars: 
event_settings: "{{ lookup('template', 'EventSettings.json.j2') | to_json }}"

输出

{
"event_settings": ""{\n  \"FileSystem\": {\n    \"MetadataPattern\": \"XPDM_MODEL_*.xml\",\n    \"RelativeInputVault\": \"C:\\Users\\adminlocal\\AppData\\Local\\Temp\\ansible.44q4u5lu.mtobuild\\SmokeTest\",\n    \"RelativeOutputVault\": \"OUTPUT\" \n  },\n  \"SessionManifest\": {\n    \"EffectivityMode\": \"PureBSF\",\n    \"NamePrefix\": \"prefix\"\n  }, \n  \"MessageBroker\": \"ActiveMQ\"\n} ""
}

您是否尝试使用反斜杠来转义value1中的字符反斜杠本身?

- name: my task
set_fact: 
value1: "C:\\Users\\adminlocal\\AppData\\Local\\Temp\\ansible.44q4u5lu.mtobuild\\SmokeTest"
value2: "abc"
value3: "123"
- name: debug
vars:
event_settings: "{{ lookup('template', 'EventSettings.json.j2') | to_json }}"
debug: 
var: event_settings

EventSettings.json.j2:

{
"FS": {
"key1": "{{ value1 }}",
"key2": "{{ value2 }}",
"key3": "{{ value3 }}" 
}
}

输出:

ok: [127.0.0.1] => {
"event_settings": {
"FS": {
"key1": "C:\Users\adminlocal\AppData\Local\Temp\ansible.44q4u5lu.mtobuild\SmokeTest",
"key2": "abc",
"key3": "123"
}
}
}

相关内容

  • 没有找到相关文章

最新更新