在 Ansible 中,我如何从字典中获取值并将它们存储在事实中?
In Ansible how do I get values from a dictionary and store them in a fact?
Ansible v2.2.1.0
如何修改 中的答案来执行此操作?
我有字典:
filenames_for_version:
1.0:
file1: "Hello.txt"
file2: "Goodbye.txt"
2.0:
file1: "HelloWorld.txt"
file2: "GoodbyeWorld.txt"
现在我想写一个任务...
- name: Get the corresponding **file1** for the version
set_fact: f1_4_ver={{ what do I put here??? }}
- name: Get the corresponding **file2** for the version
set_fact: f2_4_ver={{ what do I put here??? }}
我希望能够在调用我的剧本时将版本作为 CLI 参数传递,例如,
$ ansible-playbook -i "localhost," -c local get_filename_for_version.yml -e "version=1.0"
...如果我通过 version=1.0,我会得到...
f1_4_ver=Hello.txt
f2_4_ver=Goodbye.txt
...如果我通过 version=2.0,我会...
f1_4_ver=HelloWorld.txt
f2_4_ver=GoodbyeWorld.txt
什么代码应该在..
{{ what do I put here??? }}
...我的部分任务?
您在 variable/key 名称中使用点是自找麻烦,但是:
{{ filenames_for_version[version].file1 }}
{{ filenames_for_version[version].file2 }}
Ansible v2.2.1.0
如何修改
我有字典:
filenames_for_version:
1.0:
file1: "Hello.txt"
file2: "Goodbye.txt"
2.0:
file1: "HelloWorld.txt"
file2: "GoodbyeWorld.txt"
现在我想写一个任务...
- name: Get the corresponding **file1** for the version
set_fact: f1_4_ver={{ what do I put here??? }}
- name: Get the corresponding **file2** for the version
set_fact: f2_4_ver={{ what do I put here??? }}
我希望能够在调用我的剧本时将版本作为 CLI 参数传递,例如,
$ ansible-playbook -i "localhost," -c local get_filename_for_version.yml -e "version=1.0"
...如果我通过 version=1.0,我会得到...
f1_4_ver=Hello.txt
f2_4_ver=Goodbye.txt
...如果我通过 version=2.0,我会...
f1_4_ver=HelloWorld.txt
f2_4_ver=GoodbyeWorld.txt
什么代码应该在..
{{ what do I put here??? }}
...我的部分任务?
您在 variable/key 名称中使用点是自找麻烦,但是:
{{ filenames_for_version[version].file1 }}
{{ filenames_for_version[version].file2 }}