Run a task based on variable defined or not

17 views
Skip to first unread message

Neetu

unread,
Aug 17, 2018, 1:17:23 AM8/17/18
to Ansible Project
Hi Guys


Can some one here help me on how to run a task if a variable is undefined.


I want to check if a variable exists or not and register that .

If Variable exists I want to run a task. 

Has any one successfully done this ?

Sosys

unread,
Aug 17, 2018, 2:16:02 AM8/17/18
to ansible...@googlegroups.com
Hi,

have you tried to use "undefined" in the when statement  ?

===

when: yourvarname is undefined

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d42a13a2-69d6-440c-8998-0f447cb9c59e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Neetu

unread,
Aug 17, 2018, 3:39:35 AM8/17/18
to Ansible Project
Yes, I have used that. 

But my task is being skipped , even when the variable is defined

Here is what I am doing
- name: Check the variable value which is in dictionary
  debug:
    var: certs[a][b][c][d]['certs']
  register: results

- debug:
    var: results

- name: "set the value"
  set_fact:
    s_cert: ["{{ item }}", ]
  with_items:
    - "{{ certs[a][b][c][d]['certs'] }}"
  when: results  is undefined

so , when I run it, in both cases where variable has some value and variable is undefined, the task is being skipped

On Thursday, August 16, 2018 at 11:16:02 PM UTC-7, Benny Kusman wrote:
Hi,

have you tried to use "undefined" in the when statement  ?

===

when: yourvarname is undefined
On Fri, Aug 17, 2018 at 5:17 AM, Neetu <arumalla...@gmail.com> wrote:
Hi Guys


Can some one here help me on how to run a task if a variable is undefined.


I want to check if a variable exists or not and register that .

If Variable exists I want to run a task. 

Has any one successfully done this ?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Kai Stian Olstad

unread,
Aug 17, 2018, 11:36:22 AM8/17/18
to ansible...@googlegroups.com
On 17.08.2018 09:39, Neetu wrote:
> Yes, I have used that.

I have know idea what you are trying to do, but you should probably try
to explain in more detail and maybe show the output.


> But my task is being skipped , even when the variable is defined
>
> Here is what I am doing
> - name: Check the variable value which is in dictionary
> debug:
> var: certs[a][b][c][d]['certs']
> register: results

The variable result will always have a value...


> - debug:
> var: results
>
> - name: "set the value"
> set_fact:
> s_cert: ["{{ item }}", ]
> with_items:
> - "{{ certs[a][b][c][d]['certs'] }}"
> when: results is undefined

...so the variable result will always be defined so this task will never
run.

You probably just want to do this, delete the when and just use

{{ certs[a][b][c][d]['certs'] | default([]) }}

--
Kai Stian Olstad

Neetu

unread,
Aug 17, 2018, 1:32:08 PM8/17/18
to Ansible Project
Sorry for not mentioning it clearly.

I got it fixed.

My issue was , I am taking the values of a.b,c,d dynamically.

so when I am checking for certs[a][b][c][d]['certs'] , it will check until ['certs']., last of the nested dict

My variable is 

certs:
  host1:
    host_type:
       host_name:
         host_flavor:
            certs:
               a: 1
               b:2
  host2:
    host_type:
       host_name:
         host_flavor:
            certs:
               a: 1
               b:2


so, when host3 doesn't have entry, I wanted it to skip the task  that I am running 

so, when using { certs[a][b][c][d]['certs'] | default([]) }} }

it errors out saying type 'dict' has no value u 'host3'

Instead of checking for { certs[a][b][c][d]['certs'] | default([]) }} }, Now I am doing 

{ certs[a] | default({})} --- > which solves my purpose.

when host3 is not present,it will skip the task


Thanks .
Reply all
Reply to author
Forward
0 new messages