如何使用 jinja2 输出作为 ansible 指令而不是字符链?
我目前正在使用“extract”显示一些变量:
- name: Display hostvars over conditional
hosts: all
tasks:
- debug:
msg: "{{
ansible_play_hosts
| map('extract', hostvars, inventory_hostname)
|selectattr('ansible_distribution', 'regex', 'Rocky|CentOS' )
|selectattr('ansible_distribution_major_version', '==', '8' )
|flatten
}}"
结果是与这些条件匹配的名称列表:
TASK [debug]
ok: [ancolie-lba] => {
"msg": [
"vm703-dev"
]
}
现在我想使用 jinja2 ansible_facts.j2 对此显示进行模板化,
{{'{{'}}
ansible_play_hosts
|map('extract', hostvars)
{% for condition in conditions %}
|selectattr('{{condition.attribute}}', '{{condition.verb}}', '{{condition.text}}' )
{% endfor %}
|flatten
{{'}}'}}
以便使用 jinja2 输出文件,如下所示:
- block:
- name: generate a conf
template:
src: ansible_facts.j2
dest: /tmp/ansible_facts
- debug:
msg: "{{lookup('file', '/tmp/ansible_facts')}}"
tags: template
delegate_to: localhost
vars:
conditions:
- attribute: 'ansible_distribution'
verb: 'regex'
text: 'Rocky|CentOS'
- attribute: 'ansible_distribution_major_version'
verb: '=='
text: '8'
但似乎ansible将查找输出解释为链字符,而不是像最初那样执行过滤器:
返回什么:
"msg": "{{ansible_play_hosts|map('extract', hostvars, os)|selectattr('ansible_distribution', 'regex', 'Rocky|CentOS' )|selectattr('ansible_distribution_major_version', '==', '8' )|flatten}}"
预期结果:
TASK [debug] *********************************************************************************************************************************************************************************
ok: [ancolie-lba] => {
"msg": [
"vm703-dev"
]
}
如何使用jinja2输出作为ansible指令而不是一串字符?
I am currently displaying some vars with 'extract':
- name: Display hostvars over conditional
hosts: all
tasks:
- debug:
msg: "{{
ansible_play_hosts
| map('extract', hostvars, inventory_hostname)
|selectattr('ansible_distribution', 'regex', 'Rocky|CentOS' )
|selectattr('ansible_distribution_major_version', '==', '8' )
|flatten
}}"
The result is a list of name that match to those conditions:
TASK [debug]
ok: [ancolie-lba] => {
"msg": [
"vm703-dev"
]
}
and now I'd like to template this display with jinja2 ansible_facts.j2
{{'{{'}}
ansible_play_hosts
|map('extract', hostvars)
{% for condition in conditions %}
|selectattr('{{condition.attribute}}', '{{condition.verb}}', '{{condition.text}}' )
{% endfor %}
|flatten
{{'}}'}}
So as to use the jinja2 output file like follow:
- block:
- name: generate a conf
template:
src: ansible_facts.j2
dest: /tmp/ansible_facts
- debug:
msg: "{{lookup('file', '/tmp/ansible_facts')}}"
tags: template
delegate_to: localhost
vars:
conditions:
- attribute: 'ansible_distribution'
verb: 'regex'
text: 'Rocky|CentOS'
- attribute: 'ansible_distribution_major_version'
verb: '=='
text: '8'
But it seems that ansible interprets the lookup output as a chain character instead of excuting the filters as initially:
What is returned:
"msg": "{{ansible_play_hosts|map('extract', hostvars, os)|selectattr('ansible_distribution', 'regex', 'Rocky|CentOS' )|selectattr('ansible_distribution_major_version', '==', '8' )|flatten}}"
Expected result:
TASK [debug] *********************************************************************************************************************************************************************************
ok: [ancolie-lba] => {
"msg": [
"vm703-dev"
]
}
How to use the jinja2 output as an ansible directive instead of a chain of characters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎正在尝试生成一个 Jinja 模板作为模板的结果,然后以某种方式诱导 Ansible 重新执行它认为已经完成的模板。这在技术上可能是可以实现的,但它正在对抗 Ansible 的 Jinja 集成,而不是与之合作。
只要您至少有 Jinja 2.10(用于
namespace()
支持),您就可以以相当简单的方式完成此操作,无需双重模板。它在旧版本上也可能是可行的,但会更痛苦。test.j2
:test.yml
:输出:
You appear to be trying to produce a Jinja template as the result of your template, then somehow induce Ansible to re-do the templating that it thinks is already complete. This might be technically possible to achieve, but is fighting against Ansible's Jinja integration instead of working with it.
As long as you have at least Jinja 2.10 (for
namespace()
support) you can do this in a fairly straightforward way with no double-templating. It may also be doable on older versions, but would be more of a pain.test.j2
:test.yml
:Output: