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

32 views
Skip to first unread message

Rakesh Parida

unread,
Feb 5, 2020, 6:52:22 AM2/5/20
to Ansible Project
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


Vladimir Botka

unread,
Feb 5, 2020, 7:22:16 AM2/5/20
to Rakesh Parida, ansible...@googlegroups.com
On Wed, 5 Feb 2020 03:52:22 -0800 (PST)
Rakesh Parida <rakeshp...@gmail.com> wrote:

> [...]
> with_items:
> - "{{ dp_record | default(None) }}"
> - "{{ iampython_record | default(None) }}"
> - "{{ iamservice_record | default(None) }}"
> - "{{ iamesa_record | default(None) }}"
>
> 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.

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

Rakesh Parida

unread,
Feb 5, 2020, 11:34:38 AM2/5/20
to Ansible Project
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. 

Rakesh Parida

unread,
Feb 5, 2020, 11:37:02 AM2/5/20
to Ansible Project
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"
        ], 


Vladimir Botka

unread,
Feb 5, 2020, 12:18:12 PM2/5/20
to Rakesh Parida, ansible...@googlegroups.com
On Wed, 5 Feb 2020 08:37:02 -0800 (PST)
Rakesh Parida <rakeshp...@gmail.com> wrote:

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

> >> Yes. Test the "item". For example
> >> when: item

Vladimir Botka

unread,
Feb 5, 2020, 12:27:02 PM2/5/20
to Rakesh Parida, ansible...@googlegroups.com
On Wed, 5 Feb 2020 08:37:02 -0800 (PST)
Rakesh Parida <rakeshp...@gmail.com> wrote:

> when: item is defined and item != None and item != ""

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 == ""``)'
)

Rakesh Parida

unread,
Feb 5, 2020, 2:44:23 PM2/5/20
to Ansible Project
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 

Vladimir Botka

unread,
Feb 5, 2020, 3:07:40 PM2/5/20
to Rakesh Parida, ansible...@googlegroups.com
On Wed, 5 Feb 2020 11:44:23 -0800 (PST)
Rakesh Parida <rakeshp...@gmail.com> wrote:

> 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

Vladimir Botka

unread,
Feb 5, 2020, 3:26:36 PM2/5/20
to Rakesh Parida, ansible...@googlegroups.com
On Wed, 5 Feb 2020 21:07:14 +0100
Vladimir Botka <vbo...@gmail.com> wrote:

> On Wed, 5 Feb 2020 11:44:23 -0800 (PST)
> Rakesh Parida <rakeshp...@gmail.com> wrote:
>
> > when: item
> >
> > I am getting following error:
> > fatal: [BLVYUM01]: FAILED! => {"msg": "The conditional check 'item' failed.
> > The error was: Invalid conditional detected: invalid syntax

See "spurious CONDITIONAL_BARE_VARS warnings #53428"
https://github.com/ansible/ansible/issues/53428
Reply all
Reply to author
Forward
0 new messages