Ansible best practice for passing vars to nested playbooks?

后端 未结 2 1013
灰色年华
灰色年华 2021-01-20 06:11

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 possib

相关标签:
2条回答
  • 2021-01-20 06:33

    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.

    0 讨论(0)
  • 2021-01-20 06:39

    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}] }
    
    0 讨论(0)
提交回复
热议问题