如何使J2在VAR值中评估替代

  • 本文关键字:评估 VAR 何使 J2 ansible
  • 更新时间 :
  • 英文 :


我在配置J2模板中具有以下内容:

{% set interface = vars['wireguard_' + item + '_interface'] -%}
{% for key, option in interface_required_keys.items() %}
{{ option }} = {{ interface[key] }}
{% endfor %}

interface[key]值中的某些是 {{ lookup('file', 'some_file') }},这些值不会被Ansible扩展并将其放入结果的配置文件中:{{ lookup('file', 'some_file') }}。如何使J2评估interface[key]中的替代?

因此,这是对代码进行测试,逐文件:

test.yaml:

---
- hosts: localhost
  gather_facts: false
  vars:
  tasks:
  - name: print var
    debug:
      var: some_var.some_subvar.supposed_to_expand

group_vars/all.yml:

---
some_var:
  some_subvar:
    supposed_to_expand: "{{ lookup('file', './file_with_value.txt') }}"

file_with_value.txt:

files_contents_here

此测试没有库存文件。

剧本执行结果:

[http_offline@greenhat-29 test2]$ tree
.
├── file_with_value.txt
├── group_vars
│   └── all.yml
└── test.yml
1 directory, 3 files
[http_offline@greenhat-29 test2]$ ansible-playbook test.yml 
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] *******************************************************************************************************************************************************************************************************
TASK [print var] *******************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "some_var.some_subvar.supposed_to_expand": "files_contents_here"
}
PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
[http_offline@greenhat-29 test2]$ 

我们可以看到some_var.some_subvar.supposed_to_expand经过评估并根据预期获得本地文件的内容。

这也类似于您准备的东西吗?


更新:

关于J2问题:J2文件中的循环将尝试打印interface[key],因此我模拟了该DICE变量的一个键中的一个键具有lookup的值(在group_vars文件中定义了它,address键(。

使用的文件:

[http_offline@greenhat-29 test2]$ tree
.
├── config.j2
├── file_with_value.txt
├── group_vars
│   └── all.yml
└── test.yml
1 directory, 4 files
[http_offline@greenhat-29 test2]$ cat test.yml 
---
- hosts: localhost
  gather_facts: false
  vars:
  tasks:
  - name: run template
    template:
      src: config.j2
      dest: /tmp/config.out
  - name: print var
    debug:
      var: interface
[http_offline@greenhat-29 test2]$ cat group_vars/all.yml 
---
some_var:
  some_subvar:
    supposed_to_expand: "{{ lookup('file', './file_with_value.txt') }}"
interface:
  address: "{{ lookup('file', './file_with_value.txt') }}"
  private_key: value_defined
[http_offline@greenhat-29 test2]$ cat config.j2 
{% set interface_required_keys = { 'address': 'Address', 'private_key': 'PrivateKey' } -%}
{% for key, option in interface_required_keys.items() %}
{{ option }} = {{ interface[key] }}
{% endfor %}[http_offline@greenhat-29 test2]$ 

结果:

[http_offline@greenhat-29 test2]$ ansible-playbook test.yml 
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] *******************************************************************************************************************************************************************************************************
TASK [run template] ****************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [print var] *******************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "interface": {
        "address": "files_contents_here",
        "private_key": "value_defined"
    }
}
PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
[http_offline@greenhat-29 test2]$ cat /tmp/config.out 
Address = files_contents_here
PrivateKey = value_defined
[http_offline@greenhat-29 test2]$ 

感谢 @Ilias-sp,终于得到了解决方案。问题在于``获取doc而没有价值扩展。如果我使用 lookup获得 interface dict,它的值正在正确扩展:

{% set interface = lookup('vars', 'wireguard_' + item + '_interface') -%}

最新更新