How to handle role dependencies execution in Ansible Playbooks

Viewed 268

I have started an Ansible Collection with the different roles needed to install an application server for our application. Currently the roles are split as wildfly, mysql, elasticsearch, mongodb, etc...

Since multiple roles (to install WildFly and ElasticSearch in my case) also need Java to be installed and configured, I split the tasks related to Java into its own java role and added the java role into the roles dependencies in roles/[wildfly,elasticsearch]/meta/main.yml.

dependencies:
  - role: java

In the playbook I now have:

---
- hosts: all
  roles:
    - role: my_collection.bootstrap

- hosts: elasticsearch
  roles:
    - role: my_collection.elasticsearch

- hosts: wildfly
  roles:
    - role: my_collection.wildfly

And during the execution, the hosts will automatically also install Java where needed.

The problem I now have is how to handle the duplicate execution of java tasks if an host is assigned both wildfly and elasticsearch. The tasks related to the install are not causing any problems but there are tasks that are taking longer because they need to check the current state and apply changes only if needed. I would thus prefer to only apply the tasks from this role once.

Is there a way to ignore duplicate roles on a single host ? Or perhaps I missed a better way to organize the roles to avoid this problem ?

0 Answers
Related