English 中文(简体)
Ansible Jinja2: AttributeError: list Object has no Depende dilines
原标题:Ansible Jinja2: AttributeError: list object has no attribute splitlines

考虑到以下试验游戏手册:

---
- name: Node Tolerations
  hosts: localhost
  gather_facts: false
  vars:
    tolerations:
      - key: node.cilium.io/agent-not-ready
        operator: Exists
        effect: NoExecute
      - key: node-role.kubernetes.io/control-plane
        operator: Exists
        effect: NoSchedule
  tasks:
    - name: Set node tolerations fact
      ansible.builtin.set_fact:
        node_tolerations: "{{ (node_tolerations | default([]) | union([ : .join((item.key, item.effect))])) }}"
      with_items:  {{ tolerations }} 

    - name: Set node taint fact
      ansible.builtin.set_fact:
        node_taint: "{{ node_tolerations | select( search ,  control-plane ) }}"

    - name: Variable output
      ansible.builtin.debug:
        var: node_taint

产生预期结果:

ok: [localhost] =>
  node_taint:
  - node-role.kubernetes.io/control-plane:NoSchedule

使用<代码>node_taint a. 列入Jinja2模板的事实:

node-taint:
  {{ node_taint | indent(2) }}

产生以下错误:

AttributeError:  list  object has no attribute  splitlines 

As temporary workaround, I used:

node-taint:
  - {{ node_taint | join }}

但是,我倾向于最初的金贾2格式,允许我界定适当的诱惑。 我很想知道,你是否能够提供一些见解,什么是适当的解决办法。

Edit: 上文详细游戏手册的第二次使用是:

tolerations:
  {{ tolerations | indent(2) }}

产生同样的错误。 我更希望避免在金贾模板中使用<条码>(<>>>>格式,并在事实上操纵数据,使我能够在模板内使用适当的<条码>indent。

问题回答

node_toleration 清单是一份清单,您可以自行编制每个项目的目录:

node-taint:
{% for toleration in node_toleration | select( search ,  control-plane ) %}
{{     * 2 }}- {{ toleration }}
{% endfor %}

node-taint:
{% for toleration in node_toleration | select( search ,  control-plane ) %}
{{  -   + toleration | indent(2) }}
{% endfor %}




相关问题
Strip whitespace in generated HTML using pure Python code

I am using Jinja2 to generate HTML files which are typically very huge in size. I noticed that the generated HTML had a lot of whitespace. Is there a pure-Python tool that I can use to minimize this ...

String concatenation in Jinja

I just want to loop through an existing list and make a comma delimited string out of it. Something like this: my_string = stuff, stuff, stuff, stuff I already know about loop.last, I just need to ...

How do you sort a list in Jinja2?

I am trying to do this: {% for movie in movie_list | sort(movie.rating) %} But that s not right...the documentation is vague...how do you do this in Jinja2?

How to get django context automatically in Jinja2 filters?

For example, I have an paginator object with a lot of attributes, and don t want do write something like {{ paginate(paginator) }} in templates. How can a get context automatically in the filter ...

热门标签