自引用对象(字段是另一个的结果)Jinja
Self Referencing Object (Field is result of another) Jinja
大家好 :D
我正在尝试创建一个对象,其中一个字段依赖于另一个字段,假设 group_vars/all 文件中的内容类似于:
people_names:
- first_name: tom
last_name: hardy
full_name: " {{ first_name}} {{ last_name}} "
任务非常简单(我正在尝试调试)
---
- hosts: 127.0.0.1
tasks:
- name: Test jinja2template
template: src=test.j2 dest=test.conf
而 test.j2 类似于:
{% for person in people_names %}
person_full_name = person.full_name
{% endfor %}
这在 Ansible/Jinja 中甚至可能吗?
为了构建它,我运行正在执行命令:
ansible-playbook jinja2test.yml --check --diff --connection=local
如果您需要更多信息,请随时询问并提前致谢 :D
编辑:
当我 运行 这个我得到 AnsibleUndefinedVariable 对于 last_name
Q: "Create an object in which a field depends on another."
答:不可能。参见示例 #8603。如果需要,用全名创建字典。例如
vars:
people_names:
- first_name: tom
last_name: hardy
tasks:
- set_fact:
people_full_names: "{{ people_full_names|default([]) +
[item|combine({'full_name': full_name})] }}"
loop: "{{ people_names }}"
vars:
full_name: "{{item.first_name}} {{ item.last_name }}"
- debug:
var: people_full_names
给予
"people_full_names": [
{
"first_name": "tom",
"full_name": "tom hardy",
"last_name": "hardy"
}
]
大家好 :D
我正在尝试创建一个对象,其中一个字段依赖于另一个字段,假设 group_vars/all 文件中的内容类似于:
people_names:
- first_name: tom
last_name: hardy
full_name: " {{ first_name}} {{ last_name}} "
任务非常简单(我正在尝试调试)
---
- hosts: 127.0.0.1
tasks:
- name: Test jinja2template
template: src=test.j2 dest=test.conf
而 test.j2 类似于:
{% for person in people_names %}
person_full_name = person.full_name
{% endfor %}
这在 Ansible/Jinja 中甚至可能吗?
为了构建它,我运行正在执行命令:
ansible-playbook jinja2test.yml --check --diff --connection=local
如果您需要更多信息,请随时询问并提前致谢 :D
编辑: 当我 运行 这个我得到 AnsibleUndefinedVariable 对于 last_name
Q: "Create an object in which a field depends on another."
答:不可能。参见示例 #8603。如果需要,用全名创建字典。例如
vars:
people_names:
- first_name: tom
last_name: hardy
tasks:
- set_fact:
people_full_names: "{{ people_full_names|default([]) +
[item|combine({'full_name': full_name})] }}"
loop: "{{ people_names }}"
vars:
full_name: "{{item.first_name}} {{ item.last_name }}"
- debug:
var: people_full_names
给予
"people_full_names": [
{
"first_name": "tom",
"full_name": "tom hardy",
"last_name": "hardy"
}
]