0

I have 2 different database instances in my infrastructure, each used to save different data, each running on different machines, and each of them needs different levels of availability & safety, so while both of them use Postgresql as database, since each of them plays different "role" in my infrastructure, I have written different roles in my ansible code for them to be set up and be ready to serve requests. Let's call them database A and B.
For A I need a replica, managed by repmgr, which is run on a separate machine, and I have another role in my codes that sets up replica of A with configuration specific to A. Since A and replica of A are not interchangeable, they don't belong to the same group in inventory file, and there I have a A group and a A_replica group, explicitly distinct.
Now, I want to add replica of B, using repmgr same as in replica of A, but I don't see a way to reuse codes that I've written for A, since they differ in:

  • hosts that tasks of playbook are run against,
  • Some variables that are used in template directives to generate config files. By default, I define these in group_vars, but I don't know how to define them in playbooks, specifically playbooks that include other (lower-level) playbooks

Is there a solution, or should I literally copy & paste the codes for B?
Thank you very much.

0

First, for hosts target, you can set it as an extra-vars on the command-line ( you can find another example using extra-vars here ).

If you use different variables in your template file for each of the two cases, make sure to check if the variable is specified before proceeding. Then, use host_vars instead of group_vars.

Hope this helps!

0

another role in my codes that sets up replica of A with configuration specific to A

For maximum reuse, extract all configuration that could be different into variables and templates. Primary/secondary boolean, host names and ports, users and secrets. Generalizing may mean being more clever, such as using if statements in templates depending on if a replica is needed or not. Success in abstraction means you (or someone else if you publish on Galaxy!) can re-use the role in other projects.

By default, I define these in group_vars, but I don't know how to define them in playbooks, specifically playbooks that include other (lower-level) playbooks

There are many places to put variables: Variable precedence: Where should I put a variable?

The admin user you always create can be a role default, an "I am a replica" boolean probably goes in a group var, the replication name of a specific database probably goes in a host var. This way you can run a play on hosts: A_replica,B_replica but each uses some unique configuration.

One powerful way to make "sub-classed" roles is to use role dependencies. Create a fully featured but abstract role db. Create roles for each instance of the database as db-a and db-b that each have dependencies on db in their meta. The A and B roles only need to provide variables that are different, and maybe a task or two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.