将vars传递给嵌套剧本的最佳做法? [英] Ansible best practice for passing vars to nested playbooks?

查看:53
本文介绍了将vars传递给嵌套剧本的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图围绕Ansible并建立一个简单的LEMP堆栈.我决定使用嵌套的游戏书,因为我想要在学习的同时尽可能地划分.我遇到了这个问题,我需要传递一些变量,例如mysql的root密码.现在,我想知道是否有最佳实践将变量从主剧本传递到各个剧本,还是应该在各个子剧本中设置这些变量?我正在使用此存储库作为我自己的项目的基础.我还想知道,如果子剧本中设置了默认值,那么主变量中的变量会覆盖该值吗?

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?

推荐答案

我认为最佳实践是,如果可能的话,重用现有代码.如果您还没有听说过,Ansible的 Galaxy 网站位于 https://galaxy.在ansible.com/中,人们可以共享各种现成的角色.这样的角色之一是 mysql (其相关的github存储库位于

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)具有主复制和从复制的完全安装/配置的MySQL Server.

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

这篇关于将vars传递给嵌套剧本的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆