使用Ansible" set_fact"模块,我需要使用像" from_json"这样的过滤器从json文件中获取存储库URL。我试过几种方式,但仍然没有得到它应该如何运作。
- name: initial validation
tags: bundle
hosts: localhost
connection: local
tasks:
- name: register bundle version_file
include_vars:
file: '/ansible/playbook/workbench-bundle/bundle.json'
register: bundle
- name: debug registered bundle file
debug:
msg: '{{ bundle }}'
我得到了我想要的json:
TASK [debug registered bundle file] ************************************************
ok: [127.0.0.1] => {
"msg": {
"ansible_facts": {
"engine-config": "git@bitbucket.org/engine-config.git",
"engine-monitor": "git@bitbucket.org/engine-monitor.git",
"engine-server": "git@bitbucket.org/engine-server.git",
"engine-worker": "git@bitbucket.org/engine-worker.git"
},
"changed": false
}
}
然后我尝试按键名选择每个值,将此值用作" npm install"的URL。每个包都在不同的实例中。
- name: set_fact some paramater
set_fact:
engine_url: "{{ bundle.('engine-server') | from_json }}"
然后我收到错误:
致命:[127.0.0.1]:失败! => {"失败":true," msg":"模板错误 模板字符串:预期的名称或数字。字符串:{{ 捆绑。(' engine-server')}}"}
我还有许多其他类似loopkup的方法,但仍然会因其他错误而失败。有人可以帮助理解,我如何找到每个参数并将他存储为" set_fact"?感谢
答案 0 :(得分:1)
下面是一个示例工作代码,用于设置类似问题的变量(尽管我看不太有意义):
- name: initial validation
tags: bundle
hosts: localhost
connection: local
tasks:
- name: register bundle version_file
include_vars:
file: '/ansible/playbook/workbench-bundle/bundle.json'
name: bundle
- debug:
var: bundle
- debug:
var: bundle['engine-server']
- name: set_fact some paramater
set_fact:
engine_url: "{{ bundle['engine-server'] }}"
以上假设您的输入数据(您未包括)是:
{
"engine-config": "git@bitbucket.org/engine-config.git",
"engine-monitor": "git@bitbucket.org/engine-monitor.git",
"engine-server": "git@bitbucket.org/engine-server.git",
"engine-worker": "git@bitbucket.org/engine-worker.git"
}