如何使用包含 jinja 分隔符的 ansible 字符串变量? [英] how do I use an ansible string variable that contains jinja delimiters?

查看:28
本文介绍了如何使用包含 jinja 分隔符的 ansible 字符串变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个

- name: ugly
  hosts: localhost
  vars:
    badstr: "asdf{%jkl"
    mydir: "."
    mydict:
      filea:
        Value:  "blue!42!"
      fileb:
        Value:  "a{%isbad"
  tasks:
    - copy:
        dest: "{{ item.key }}"
        content: "{{ item.value.Value }}"
      loop: "{{ mydict|default({})|dict2items }}"

给我这个错误:

fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while templating 'asdf{%jkl'. Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: Encountered unknown tag 'jkl'.. String: asdf{%jkl"}

'mydict' 结构是从插件返回的,我无法定义成员.值"之一包含{%".对它的任何引用都会导致错误,无论是作为变量、文件内容还是模板.

The 'mydict' structure is returned from a plugin and I do not get to define the members. One of the 'Value's contains a "{%". Any reference to it will cause an error, whether as a variable, file content or in a template.

我尝试过各种不安全、{{、%raw 等的引用和组合.它要么给我错误信息,要么将变量的名称放在文件中.

I have tried all kinds of quoting and combinations of unsafe, {{, %raw, etc. It either gives me the error or puts the name of the variable in the file.

如何将值写入文件?或者只是将其用作变量?

How can I write the value to a file? Or just use it as a variable?

MacOS 11.3 上的 Ansible 2.8.4,RHEL 7 上的 ansible 2.9.

Ansible 2.8.4 on MacOS 11.3, also ansible 2.9 on RHEL 7.

推荐答案

这里的问题不在于 copy 任务中的值评估;问题是它们是如何设置的.例如,如果我创建一个名为 example.sh 的简单 ansible 模块,如下所示这个:

The problem here is not in the copy task where the values are evaluated; the problem is how they are being set. For example, if I create a simple ansible module named example.sh that looks like this:

#!/bin/sh

cat <<EOF
{
    "files": {
    "filea": {
      "Value": "blue!42!"
    },
    "fileb": {
      "Value": "a{%isbad"
    }
    }
}
EOF

我可以写这样的剧本:

- name: ugly
  hosts: localhost
  tasks:
    - example:
      register: mydict

    - copy:
        dest: "{{ item.key }}"
        content: "{{ item.value.Value }}"
      loop: "{{ mydict.files|dict2items }}"

这会按预期运行,创建一个包含以下内容的文件 fileb,没有任何错误:

And this runs as expected, creating without any errors a file fileb with the content:

a{%isbad


同样,如果我从 JSON 文件中读取数据并通过 from_json 传递它,它也可以正常工作:


Similarly, if I read the data from a JSON file and pass it through from_json, it also works fine:

- name: ugly
  hosts: localhost
  tasks:
    - set_fact:
        mydict: "{{ lookup('file', 'data.json')|from_json }}"

    - copy:
        dest: "{{ item.key }}"
        content: "{{ item.value.Value }}"
      loop: "{{ mydict.files|dict2items }}"


只有在上下文中定义变量时才会出现问题Ansible 正在寻找 Jinja 模板——因此,作为剧本中的变量,vars 文件,set_fact 的参数,等

您可以通过改变自己的方式来潜在地解决问题消耗这些值.

You can potentially work around the problem by changing how you are consuming these values.

这篇关于如何使用包含 jinja 分隔符的 ansible 字符串变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆