How to ignore the none value from "Default(None) filter" that allows None value added to list of Item

Hi,

After i create a list and when item is not present then an item with NONE is added to the list.

  • name: Create an empty list variable
    set_fact:
    ece_nodes:

  • name: Append string to ECE node list
    set_fact:
    ece_nodes: “{{ ece_nodes + [ item ] }}”
    with_items:

  • “{{ dp_record | default(None) | regex_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

  • “{{ iampython_record | default(None) | regex_replace(‘-[0-9].[0-9].[0-9].rpm’,‘’) }}”

  • “{{ iamservice_record | default(None) | regex_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

  • “{{ iamesa_record | default(None) | regex_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

But when the “iamesa_record” is not present i get the following list Output:

“msg”: [
[
“dp-steps-common”,
“iam-python-common”,
“iam-service-default”,
“None”
],

IS there any way that if the item is not available then it should not be appended to the list. Only the available items should be added to the list and the unavailable item should be ignored.

With Regards
Rakesh

Yes. Test the "item". For example

  with_items:
    - "{{ dp_record | default(None) }}"
    - "{{ iampython_record | default(None) }}"
    - "{{ iamservice_record | default(None) }}"
    - "{{ iamesa_record | default(None) }}"
  when: item

I've removed the irrelevant regex_replace part of the filters.

HTH,

  -vlado

Hi Vladimir,

I made changes as follows:

  • name: Create an empty list variable
    set_fact:
    ece_nodes:

  • name: Append string to ECE node list
    set_fact:
    ece_nodes: “{{ ece_nodes + [ item ] }}”
    with_items:

  • “{{ dp_record | default(None) | regex_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

  • “{{ iampython_record | default(None) | regex_replace(‘-[0-9].[0-9].[0-9].rpm’,‘’) }}”

  • “{{ iamservice_record | default(None) | regex_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

  • “{{ iamesa_record | default(None) | regex_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

  • name: Display Lists
    debug:
    msg:

  • “{{ ece_nodes }}”

I still get the same :

ok: [BVI06CS2] => {
“msg”: [
[
“dp-steps-common-3.0.12”,
“iam-python-common-1.0.1”,
“iam-service-default-3.1.37”,
“None”
],

If the value is not present it should not be present in the list. Only the list should have present items.
The items which are absent should be not appended.

CHange in playbook made:

  • name: Create an empty list variable
    set_fact:
    ece_nodes:

  • name: Append string to ECE node list
    set_fact:
    ece_nodes: “{{ ece_nodes + [ item ] }}”
    with_items:

  • “{{ dp_record | default(None) | regex_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

  • “{{ iampython_record | default(None) | regex_replace(‘-[0-9].[0-9].[0-9].rpm’,‘’) }}”

  • “{{ iamservice_record | default(None) | regex_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

  • “{{ iamesa_record | default(None) | regex_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

when: item is defined and item != None and item != “”

  • name: Display Lists
    debug:
    msg:
  • “{{ ece_nodes }}”

Still i get the same result:

[
“dp-steps-common-3.0.12”,
“iam-python-common-1.0.1”,
“iam-service-default-3.1.37”,
“None”
],

      when: item is defined and item != None and item != ""
[...]
Still i get the same result:

Yes. This is the result of your "improvement". See below.

See "ComparisonToEmptyStringRule.py"
https://github.com/ansible/ansible-lint/blob/master/lib/ansiblelint/rules/ComparisonToEmptyStringRule.py

    description = (
        'Use ``when: var`` rather than ``when: var != ""`` (or '
        'conversely ``when: not var`` rather than ``when: var == ""``)'
    )

Hi Vladimir,

I tried with mentioned suggestion but no success.

with_items:

  • “{{ dp_record | default(None) }}”
  • “{{ iampython_record | default(None) }}”
  • “{{ iamservice_record | default(None) }}”
  • “{{ iamesa_record | default(None) }}”
    when: item

I am getting following error:
fatal: [BLVYUM01]: FAILED! => {“msg”: "The conditional check ‘item’ failed. The error was: Invalid conditional detected: invalid syntax

Start with this one. It's working

- hosts: localhost
  vars:
    iampython_record: iampython_record
    ece_nodes:
  tasks:
    - set_fact:
        ece_nodes: "{{ ece_nodes + [ item ] }}"
      loop:
        - "{{ dp_record|default(None) }}"
        - "{{ iampython_record|default(None) }}"
        - "{{ iamesa_record|default(None) }}"
      when: item
    - debug:
        var: ece_nodes

See "spurious CONDITIONAL_BARE_VARS warnings #53428"
https://github.com/ansible/ansible/issues/53428