如何使j2来评估var值内的替换

时间:2019-06-29 19:43:33

标签: ansible

我的config 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]中的替换?

2 个答案:

答案 0 :(得分:1)

那么,这是经过测试的代码,一个文件一个文件

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],因此我模拟了该dict变量的键之一具有值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]$ 

答案 1 :(得分:0)

感谢@ ilias-sp,终于找到了解决方案。 问题在于``在没有值扩展的情况下获取文档。 如果我使用interface获得了lookup字典,则其值正在正确扩展:

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