Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename roles/rolename/tasks/main.yml to rolename.yml in Ansible

Tags:

ansible

By default, Ansible looks for the tasks for a role in a main.yml. I have too many main.yml files and I'd like to rename this to rolename.yml or something that is more unique. How can I change Ansible's default behavior to use rolename.yml instead of tasks/main.yml?

like image 447
Josh Unger Avatar asked May 11 '15 17:05

Josh Unger


2 Answers

As Bruce already pointed out this is hardcoded. But I have an issue with this behavior as well, as my IDE displays the filename in the tab and I used to have a bazillion tabs named "main.yml".

My standard setup is to have two files:

  • main.yml
  • role-name.yml

In the main.yml then simply is an include task to the role-name.yml. Along with this include I handle tags, because I want all my roles to be tagged with their name.

---

- include: role-name.yml
  tags: role-name

...
like image 117
udondan Avatar answered Sep 17 '22 13:09

udondan


Unfortunately there's no way to do this. The name main.yml is hardcoded into the ansible source code. (If you really care, look for the function _resolve_main in this file.)

Role tasks will always be in the file roles/<rolename>/tasks/main.yml, variables in roles/<rolename>/vars/main.yml, etc. Because the path that each file lives in provides the full detail of the name of the role & purpose of the file, there's really no need to change the name from main.yml. You would just end up with something like roles/<rolename>/tasks/<rolename>.yml which is redundant.

This is all documented in Ansible's Best Practices document.

like image 33
Bruce P Avatar answered Sep 19 '22 13:09

Bruce P