# reuse conditionals

**URL:** <https://forum.ansible.com/t/reuse-conditionals/19617>\
**Category:** Ansible Project\
**Created:** [October 26, 2015, 10:45am UTC](https://forum.ansible.com/t/reuse-conditionals/19617 "2015-10-26T10:45:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![r.grosmann](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@r.grosmann](https://forum.ansible.com/u/r.grosmann)\
**Post date:** [October 26, 2015, 10:45am UTC](https://forum.ansible.com/t/reuse-conditionals/19617/1 "2015-10-26T10:45:34Z")

</div>

hello group,

I have a very simple question, but I fail to find an answer. Hopefully you can help me out.

In my playbook, I have a lot of conditional tasks. I use the when: test quite often. In some cases the conditional can get quite long and difficult to read. I would like to be able to store a conditional in a variable so that I don’t have to repeat the components of it.  
For instance:

tasks:

- name: determine stat of /tmp/blip  
stat: path=/tmp/blip  
register: blip

- name: test directory  
when: **blip.stat.isdir is defined and blip.stat.isdir == true**  
debug: msg=“/tmp/blip is a directory”

- name: create previous link  
when: previous.stat.exists and previous.stat.islnk and not ( **blip.stat.isdir is defined and blip.stat.isdir == true** )  
debug: msg=“make link to previous directory”

I would like it to look more like

tasks:

- name: determine stat of /tmp/blip  
stat: path=/tmp/blip  
register: blip

- name: test directory  
when: **{{dir\_is\_there}}**  
debug: msg=“/tmp/blip is a directory”

- name: create previous link  
when: **{{ previous\_link\_exists}}** and not **({dir\_is\_there}}**  
debug: msg=“make link to previous directory”

What is the best way to put parts or complete conditinoals in a variable (or something) to reduce code duplication and to enhance readability? I am sure it is possible in ansible, and I can think of a way to do it, but I am not sure I know all the options to choose the best solution.

thanks in advance for your advice, Ruud

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 26, 2015, 6:00pm UTC](https://forum.ansible.com/t/reuse-conditionals/19617/2 "2015-10-26T18:00:57Z")

</div>

you can define vars that depend on the condition:

vars:  
&nbsp;&nbsp;blip\_dir: "{{ blip.stat.isdir is defined and blip.stat.isdir == true }}"  
&nbsp;&nbsp;not\_blip\_dir: "{{ not blip\_dir }}"  
...  
&nbsp;&nbsp;&nbsp;&nbsp;when: blip\_dir|bool  
...  
&nbsp;&nbsp;&nbsp;&nbsp;when: previous.stat.exists and previous.stat.islnk and not\_blip\_dir|bool

---

<div class="post-metadata">

**Author:** ![r.grosmann](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@r.grosmann](https://forum.ansible.com/u/r.grosmann)\
**Post date:** [October 27, 2015, 8:39am UTC](https://forum.ansible.com/t/reuse-conditionals/19617/3 "2015-10-27T08:39:53Z")

</div>

Hi Brian,

my problem is that I want to do this in the tasks/main.yml of a certain role. It seems it is not possible to define vars on the same level as the tasks:

- vars:

- file: /tmp/zb

- link: blip.stat.exists and blip.stat.islink

- name: bepaal stat van file {{file}}  
stat: path={{file}}  
register: blip

- name: is exists true?  
when: blip.stat.exists  
debug: msg=“blip.stat.isdir exists”

Obviously this syntax is not OK (ERROR: vars is not a legal parameter in an Ansible task or handler). Because I want to do this to enhance readability, the only solution I see for now, moving it to the playbook itself, is not much of a good idea.  
Is it possible to have var definition in the role file itself in some way?

regards, Ruud

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 27, 2015, 12:03pm UTC](https://forum.ansible.com/t/reuse-conditionals/19617/4 "2015-10-27T12:03:22Z")

</div>

you can define them in defaults/main.yml or vars/main.yml as part of the role.

---

<div class="post-metadata">

**Author:** ![r.grosmann](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@r.grosmann](https://forum.ansible.com/u/r.grosmann)\
**Post date:** [October 28, 2015, 8:14am UTC](https://forum.ansible.com/t/reuse-conditionals/19617/5 "2015-10-28T08:14:26Z")

</div>

Hi Brian,  
it was not my intention to put vardefs in another file, I like to have related code in the same file not far from each other.  
But I will certainly give it a try, just to see how it works. So the definition goes in rolename/vars/main.yml.

Another option I was thinking of is the use of registered variables, that have the advantage that they ‘live’ in the same file, but have the drawback that the conditional has to be rewritten using a shell command.

thanks for your help, Ruud
