# Pre-Conditions for a role to execute tasks

**URL:** <https://forum.ansible.com/t/pre-conditions-for-a-role-to-execute-tasks/14985>\
**Category:** Ansible Project\
**Created:** [April 9, 2014, 11:16pm UTC](https://forum.ansible.com/t/pre-conditions-for-a-role-to-execute-tasks/14985 "2014-04-09T23:16:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rui\_Yang](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@Rui\_Yang](https://forum.ansible.com/u/Rui_Yang)\
**Post date:** [April 9, 2014, 11:16pm UTC](https://forum.ansible.com/t/pre-conditions-for-a-role-to-execute-tasks/14985/1 "2014-04-09T23:16:30Z")

</div>

Hi,

Suppose I have a role called Ruby. I will

1. install build tools
2. download ruby source
3. compile
4. install

Because it takes long time to do all these tings and I don’t want to it it the expected Ruby already installed.

How could I do it?

I could do it using the “when” condition on every step. like the following:

- name: check if ruby installed  
shell: ls /opt/rubies  
register: ruby\_installed

- name: install dependencies  
apt: name =…  
when: ruby\_installed.stdout.find(ruby\_full\_version) == -1

Wonder if there is any better solution? like a precondition for all the tasks in a role?

Also I found it would be convenient if we have module like Java, Ruby to install various version of the packages. Like Chef they have Java cookbook.

Any suggestion is really appreciated.

Cheers,  
Rui

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 15, 2014, 8:49pm UTC](https://forum.ansible.com/t/pre-conditions-for-a-role-to-execute-tasks/14985/2 "2014-04-15T20:49:30Z")

</div>

If you ever want to skip a large number of tasks, usage of “group\_by” can easily do this.

- hosts: all  
tasks:

- stat: path=/path/to/ruby  
register: ruby

- group\_by: key=ruby-{{ ruby.stat.exists }}

- hosts: ruby-False  
tasks:

- …

# or roles, etc

This is untested but that’s the general pattern.

Also notice stat is a bit cleaner than “shell” above.
