Ansible best practice for passing vars to nested playbooks?

点点圈 提交于 2020-01-11 10:14:49

问题


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?


回答1:


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}] }



回答2:


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.



来源:https://stackoverflow.com/questions/22077272/ansible-best-practice-for-passing-vars-to-nested-playbooks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!