Ansible : strange jinja2 behaviour with lowercase

can anyone explain the following Jinja behaviour in Ansible?

The following code works: The task is skipped because the job_function is not in the exclusion_functions array

- name: Lowercase test
  hosts: localhost
  gather_facts: false
  vars:
    job_function: 'VRIJwillig'
    exclusion_functions:
      - vrijwilliger
      - maatje

  tasks:
    - name: Test for function in exclusions
      debug:
        msg: In exclusions
      when: (job_function | lower) in exclusion_functions

The following code works as well, the second task is skipped.

- name: Lowercase test
  hosts: localhost
  gather_facts: false
  vars:
    job_function: 'VRIJwillig'
    exclusion_functions:
      - Vrijwilliger
      - Maatje

  tasks:
    - name: Initialiseer variabelen
      set_fact:
        exclusions: "{{ exclusion_functions | lower }}"

    - name: Test for function in exclusions
      debug:
        msg: In exclusions
      when: (job_function | lower) in exclusions

But this doesn’t work, the task isn’t skipped:

---
- name: Lowercase test
  hosts: localhost
  gather_facts: false
  vars:
    job_function: 'VRIJwillig'
    exclusion_functions:
      - Vrijwilliger
      - Maatje

  tasks:
    - name: Test for function in exclusions
      debug:
        msg: In exclusions
      when: (job_function | lower) in (exclusion_functions | lower)

Why is the task not skipped in the last code snippet? job_function | lower is vrijwillig, and exclusion_functions | lower is ['vrijwilliger','maatje'], and the first one isn’t in the second one.

The value of the variable exclusion_functions is a list

    - debug:
        msg: |
          {{ exclusion_functions }}
          {{ exclusion_functions | type_debug }}

gives

  msg: |-
    ['Vrijwilliger', 'Maatje']
    list

The filter lower converts the argument to a string

    - debug:
        msg: |
          {{ exclusion_functions | lower }}
          {{ exclusion_functions | lower | type_debug }}

The output looks like a list but it isn’t. It’s a string

  msg: |-
    ['vrijwilliger', 'maatje']
    str

This explains why the below task isn’t skipped

    - name: Test for function in exclusions
      debug:
        msg: In exclusions
      when: (job_function | lower) in (exclusion_functions | lower)

The value VRIJwillig of the variable job_function is converted to lowercase vrijwillig which is in the string ['vrijwilliger', 'maatje']

The solution is to map the filter lower to each list’s item

    - debug:
        msg: |
          {{ exclusion_functions | map('lower') }}
          {{ exclusion_functions | map('lower') | type_debug }}

shows the result is a list with items converted to lowercase

  msg: |-
    ['vrijwilliger', 'maatje']
    list

Then, as expected, the below task is skipped

    - name: Test for function in exclusions
      debug:
        msg: In exclusions
      when: (job_function | lower) in (exclusion_functions | map('lower'))

Example of a complete playbook for testing

- name: Lowercase test
  hosts: localhost

  vars:

    job_function: VRIJwillig
    exclusion_functions:
      - Vrijwilliger
      - Maatje

  tasks:

    - debug:
        msg: |
          {{ exclusion_functions }}
          {{ exclusion_functions | type_debug }}

    - debug:
        msg: |
          {{ exclusion_functions | lower }}
          {{ exclusion_functions | lower | type_debug }}

    - name: Test for function in exclusions
      debug:
        msg: In exclusions
      when: (job_function | lower) in (exclusion_functions | lower)

    - debug:
        msg: |
          {{ exclusion_functions | map('lower') }}
          {{ exclusion_functions | map('lower') | type_debug }}

    - name: Test for function in exclusions
      debug:
        msg: In exclusions
      when: (job_function | lower) in (exclusion_functions | map('lower'))

1

Whereby I can’t answer

Q: “Can anyone explain the following Jinja behaviour in Ansible?

in order to search for a (sub-)string in a list I like to recommend to use the approach from the following minimal example playbook

---
- name: Lowercase test
  hosts: localhost
  become: false
  gather_facts: false

  vars:

    job_function: 'VRIJwillig'
    exclusion_functions:
      - Vrijwilliger
      - Maatje

  tasks:

    - name: Initialiseer variabelen
      debug:
        var: exclusion_functions | lower

    - name: Test for function in exclusions
      debug:
        msg: In exclusions
      when: exclusions is search(job_function | lower)
      #  Initialiseer variabelen
      vars:
        exclusions: "{{ exclusion_functions | lower }}"

resulting into an output of

TASK [Initialiseer variabelen] *******************************************************
ok: [localhost] =>
  exclusion_functions | lower:
  - vrijwilliger
  - maatje

TASK [Test for function in exclusions] *******************************************************
ok: [localhost] =>
  msg: In exclusions

because the search test – Does string match a regular expression has already a Parameter: ignorecase. See in example the result of

    - name: Test for function in exclusions
      debug:
        msg: In exclusions
      when: exclusions is search(job_function, ignorecase=True)
      #  Initialiseer variabelen
      vars:
        exclusions: "{{ exclusion_functions }}"

If looking for an exact match of list elements use contains test – does the list contain this element

Checks the supplied element against the input list to see if it exists within it.

- name: Test for function in exclusions
      debug:
        msg: In exclusions
      when: exclusions is contains(job_function | lower)
      #  Initialiseer variabelen
      vars:
        exclusions: "{{ exclusion_functions | lower }}"

resulting into a SKIPPED task.

PLAY RECAP *****************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=1

Or use the already given answer.

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