Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to override a template defined into an ansible galaxy role?

I'm trying to set up a server with ansible and I'd like to use this galaxy role.

It defines a template that I'd like to customize, but I don't know how.

Right now I defined the role into requirements.yml and I installed it using:

ansible-galaxy install -r requirements.yml

which installed the role somewhere on my system.

I tried by recreating the folder tree into my repository where I store my playbooks:

roles
  |- ansible-role-passenger
     |- templates
        |- passenger.j2

but it does not work. When I run my playbook, ansible uses the passenger.j2 file from inside the galaxy role.

I think I can fork the galaxy role on github and just edit the file passenger.j2 like I want, but I don't know if this is there is a "better" way to do it :)

like image 463
delphaber Avatar asked Apr 18 '19 09:04

delphaber


People also ask

What is the difference between an Ansible role and a playbook?

Ansible playbook is a script file which contains all the tasks that need to be performed along with all the ingredients required to perform these tasks. Roles are ways of automatically certain var files, tasks, and handlers based on the known file structure.

Can Ansible roles be reused by playbooks in a different directory?

Ansible will look here when we want to use our new role in a playbook later in this tutorial. Within this directory we will define roles that can be reused across multiple playbooks and different servers. Each role that we will create requires its own directory.

Under which conditions can Ansible Galaxy be used to install roles?

When set, the ANSIBLE_ROLES_PATH variable is used during playbook execution to locate installed roles, and by ansible-galaxy to determine where to install roles. It can be set to a single directory path, or to a list of paths (e.g., /etc/ansible/roles:~/. ansible/roles).

What are templates in Ansible roles?

A template is a file that contains all your configuration parameters, but the dynamic values are given as variables in the Ansible. During the playbook execution, it depends on the conditions such as which cluster you are using, and the variables will be replaced with the relevant values.


2 Answers

Your findings are unfortunately true. Overriding a hardcoded template in a role from a calling playbook is merely impossible unless the role's author implemented that as a feature. Note that this is also true for simple files in the files directory.

The best way I have found so far: given that the role contains the default template in templates/passenger.j2, add a var in default/main.yml such as passenger_config_template: passenger.j2 and use that var in the role. The user can then override that var in its playbook/inventory and use a different name for the template which will be fetched in an other role or directly in a templates directory at playbook level.

You can have a look at a similar issue and an accepted PR I once made to @geerlingguy on his ansible-role-gitlab. He might consider doing the same thing on his passenger role (or might accept your PR if you propose one).

like image 88
Zeitounator Avatar answered Oct 28 '22 02:10

Zeitounator


The reply from @Zeitounator above is totally legit.

But in the case where the author of an Ansible role is not responsive or playing dumb, there is another simpler approach.

Steps:

1) Execute the role as you would do, don't change anything

2) Write a small piece of Ansible code to simply override any file or template yourself.

Simple approach and is a no-brainer for all.

cheers

like image 20
Khayrattee Wasseem Avatar answered Oct 28 '22 03:10

Khayrattee Wasseem