如何使用 jinja2 输出作为 ansible 指令而不是字符链?

发布于 2025-01-10 17:17:26 字数 2212 浏览 0 评论 0原文

我目前正在使用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行雁书 2025-01-17 17:17:26

您似乎正在尝试生成一个 Jinja 模板作为模板的结果,然后以某种方式诱导 Ansible 重新执行它认为已经完成的模板。这在技术上可能是可以实现的,但它正在对抗 Ansible 的 Jinja 集成,而不是与之合作。

只要您至少有 Jinja 2.10(用于 namespace() 支持),您就可以以相当简单的方式完成此操作,无需双重模板。它在旧版本上也可能是可行的,但会更痛苦。

test.j2

{% set ns = namespace(result=(ansible_play_hosts | map('extract', hostvars))) %}
{% for condition in conditions %}
{% set ns.result = ns.result | selectattr(condition.attribute, condition.verb, condition.text) %}
{% endfor %}
{{ ns.result | map(attribute='inventory_hostname') | to_nice_yaml }}

test.yml

- hosts: all
  tasks:
    - debug:
        msg: "{{ lookup('template', 'test.j2') | from_yaml }}"
      run_once: true
      vars:
        conditions:
          - attribute: ansible_facts.hostname
            verb: match
            text: pe

输出:

PLAY [all] ************************************************************************

TASK [Gathering Facts] ************************************************************ok: [rotten-rusalka.authz-relay.x.mail.umich.edu]
ok: [persistent-servitor.relay-egress.x.mail.umich.edu]
ok: [bounteous-abumiguchi.mx.x.mail.umich.edu]
ok: [worthy-yithian.vdc-relay.x.mail.umich.edu]
ok: [commutual-chthonian.syslog.x.mail.umich.edu]
ok: [yern-elderthing.authz-static.x.mail.umich.edu]
ok: [diligent-griffin.authn-relay.x.mail.umich.edu]
ok: [dauntless-ratthing.dnsbl.x.mail.umich.edu]
ok: [powerful-yithian.covid-relay.x.mail.umich.edu]
ok: [legible-sansei.egress.x.mail.umich.edu]
ok: [dominant-shantak.dnsbl.x.mail.umich.edu]
ok: [trustworthy-dhole.jail.x.mail.umich.edu]
ok: [rose-dobyoshi.mx.x.mail.umich.edu]
ok: [pear-kaichigo.mx.x.mail.umich.edu]
ok: [pandora.x.mail.umich.edu]
ok: [queenly-kuzunoha.egress.x.mail.umich.edu]

TASK [debug] *********************************************************************$
ok: [worthy-yithian.vdc-relay.x.mail.umich.edu] => {
    "msg": [
        "persistent-servitor.relay-egress.x.mail.umich.edu",
        "pear-kaichigo.mx.x.mail.umich.edu"
    ]
}

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:

{% set ns = namespace(result=(ansible_play_hosts | map('extract', hostvars))) %}
{% for condition in conditions %}
{% set ns.result = ns.result | selectattr(condition.attribute, condition.verb, condition.text) %}
{% endfor %}
{{ ns.result | map(attribute='inventory_hostname') | to_nice_yaml }}

test.yml:

- hosts: all
  tasks:
    - debug:
        msg: "{{ lookup('template', 'test.j2') | from_yaml }}"
      run_once: true
      vars:
        conditions:
          - attribute: ansible_facts.hostname
            verb: match
            text: pe

Output:

PLAY [all] ************************************************************************

TASK [Gathering Facts] ************************************************************ok: [rotten-rusalka.authz-relay.x.mail.umich.edu]
ok: [persistent-servitor.relay-egress.x.mail.umich.edu]
ok: [bounteous-abumiguchi.mx.x.mail.umich.edu]
ok: [worthy-yithian.vdc-relay.x.mail.umich.edu]
ok: [commutual-chthonian.syslog.x.mail.umich.edu]
ok: [yern-elderthing.authz-static.x.mail.umich.edu]
ok: [diligent-griffin.authn-relay.x.mail.umich.edu]
ok: [dauntless-ratthing.dnsbl.x.mail.umich.edu]
ok: [powerful-yithian.covid-relay.x.mail.umich.edu]
ok: [legible-sansei.egress.x.mail.umich.edu]
ok: [dominant-shantak.dnsbl.x.mail.umich.edu]
ok: [trustworthy-dhole.jail.x.mail.umich.edu]
ok: [rose-dobyoshi.mx.x.mail.umich.edu]
ok: [pear-kaichigo.mx.x.mail.umich.edu]
ok: [pandora.x.mail.umich.edu]
ok: [queenly-kuzunoha.egress.x.mail.umich.edu]

TASK [debug] *********************************************************************$
ok: [worthy-yithian.vdc-relay.x.mail.umich.edu] => {
    "msg": [
        "persistent-servitor.relay-egress.x.mail.umich.edu",
        "pear-kaichigo.mx.x.mail.umich.edu"
    ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文