0

Background

I am writing a playbook that installs two of the same web application on different machines. Both applications require their own MySQL/MariaDB database. I plan to pass the MySQL information (database name, username, and password) via external variables that meet specific placeholders in a template file (e.g. mysql.php).

Problem

I have the same file for both installations of the application (for example's sake, mysql.php). Both have placeholders for {{ db_name }}, {{ db_user }}, and {{ db_password }}. How would it be possible for me to have multiple, external variables I pass to my playbook be used in each "installation" separately?

If this doesn't make sense, I will be happy to re-clarify.

0

You can include variables files at run-time.

For instance you could have:

/<ansible-root>/environments/
                            /env-A.yml
                            /env-B.yml

These files would contain the site-specific variables, like:

---
variable_a: foo 
variable_b: bar

...which you would reference from your plays with the standard ({{ variable_a }}) notation.

When you run your playbook, just include the correct file:

$ ansible-playbook playbook.yml -e @environments/env-A.yml
  • But wouldn't this method prevent me from running the playbook on multiple hosts at once? So I'd have to do environment A, then B, then C, for each, individual host. – Justin W. Flory Jul 15 '16 at 19:28
  • Do you have your different hosts in separate groups? If so, you can use group_vars for that. – EEAA Jul 15 '16 at 19:28
  • Currently, I have the hosts together in the same group, e.g. [my-app]. – Justin W. Flory Jul 15 '16 at 19:47
  • Well then I think you'll need to re-work things a bit. – EEAA Jul 15 '16 at 19:47
  • Okay. I'm trying to think of how that work, though. Even if I grouped them together in a different way, I don't see how to solve the question about them all having their own, separate database (even if they share the same actual host or not). – Justin W. Flory Jul 15 '16 at 19:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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