AttributeError: ‘list’ object has no attribute ‘splitlines’

Considering the following test playbook:

---
- 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

Which produces the expected result:

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

While using the node_taint fact into a Jinja2 template:

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

The following error is generated:

AttributeError: 'list' object has no attribute 'splitlines'

As temporary workaround, I used:

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

But I prefer the initial Jinja2 format, allowing me to define the proper indentation. I was wondering if you can provide some insight, what would be the proper fix.

Edit: A second usage I will have for the above detailed playbook is:

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

Which produces the same error. I would prefer to avoid the use of for loops inside Jinja template and manipulate the data at fact level, allowing me to use a proper indent inside template.

Given the mre list

  node_taint:
    - node-role.0
    - node-role.1
    - node-role.2

the below debug

   - debug:
       var: node_taint

gives in YAML format

  node_taint:
  - node-role.0
  - node-role.1
  - node-role.2

If you want to indent the lines

    - debug:
        msg: |
          node_taint:
            {{ node_taint | indent(2) }}

you get the below error because the value of node_taint isn’t a string

… The error was: AttributeError: ‘list’ object has no attribute ‘splitlines’

Quoting from the function indent:

jinja-filters.indent(s: str, width: int | str = 4, ...

Return a copy of the string with each line indented by 4 spaces. The first line and blank lines are not indented by default.

The solution is to convert the value of node_taint to a string. Use the filter to_nice_yaml

    - debug:
        msg: |
          node_taint:
            {{ node_taint | to_nice_yaml | indent(2) }}

gives

  msg: |-
    node_taint:
      - node-role.0
      - node-role.1
      - node-role.2

Example of a complete playbook for testing

- hosts: localhost

  vars:

    node_taint:
      - node-role.0
      - node-role.1
      - node-role.2

  tasks:

    - debug:
        var: node_taint

    - debug:
        msg: |
          node_taint:
            {{ node_taint | to_yaml | indent(2) }}

    - debug:
        msg: |
          node_taint:
            {{ node_taint | to_nice_yaml | indent(2) }}

1

Since node_toleration is a list, you can iterate over it to produce a line of indented output for each item:

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

or:

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

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật