Question

So I am trying to wrap my head around Ansible and building a simple LEMP stack. I decided to work with a nested playbook because I want to compartmentalize as much as possible, while learning. I run in to this issue where I need to pass some variables such as the root password of mysql. Now I wonder if there is any best practice passing varibles from the main playbook down to the individual plays or should the varibles be set in the individual sub-playbooks? I am using this repo as a basis for my own project. I also wonder how overriding works with varibles, if a default value is set in the sub-playbook does the variables set in the main playbook override this value?

Was it helpful?

Solution

I imagine best practices is, if possible, to reuse existing code. If you haven't heard about it already, Ansible has Galaxy site at https://galaxy.ansible.com/ where people share various ready-to-use roles. One of such roles is mysql(its relevant github repo is at https://github.com/bennojoy/mysql.)

Not only can you utilize that role in your playbooks, but that page also has examples that show how to pass parameters/variables to your roles:

4) A fully installed/configured MySQL Server with master and slave replication.

- hosts: master
  roles:
   - {role: mysql, mysql_db: [{name: benz}, {name: benz2}],
                   mysql_users: [{name: ben3, pass: foobar, priv: "*.*:ALL"},
                                 {name: ben2, pass: foo}],
                   mysql_db_id: 8 }

- hosts: slave
  roles:
   - {role: mysql, mysql_db: none, mysql_users: none,
            mysql_repl_role: slave, mysql_repl_master: vm2,
            mysql_db_id: 9, mysql_repl_user: [{name: repl, pass: foobar}] }

OTHER TIPS

A good solution is you have a vars.yml.dist file with examples of the variables that can be set. This exists in you repository and developers would simply make a local copy of vars.yml based on this. Then simply add the following to the your playbook: include: vars.yml This allows you to pass in variables to your roles, nested or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top