ansible vars_files 与 include_vars [英] ansible vars_files vs include_vars

查看:52
本文介绍了ansible vars_files 与 include_vars的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别

  • vars_files 指令

include_vars 模块

当上述任何一项被弃用或不鼓励时,应该使用哪个?

Which should be used when, is any of above deprecated, discouraged?

推荐答案

vars_filesinclude_vars 都被标记为稳定接口,因此它们都不会被弃用.它们都有一些共同点,但它们解决的目的不同.

Both vars_files and include_vars are labeled as stable interfaces so neither of them are deprecated. Both of them have some commonalities but they solve different purposes.

vars_files:

vars_file 指令只能在定义播放以指定变量文件时使用.这些文件中的变量包含在剧本中.由于它在播放开始时使用,因此很可能意味着其他播放(在此播放之前)创建了这些 vars 文件,或者它们是在运行配置之前静态创建的;意味着它们是该剧的一种配置变量.

vars_file directive can only be used when defining a play to specify variable files. The variables from those files are included in the playbook. Since it is used in the start of the play, it most likely implies that some other play(before this play) created those vars files or they were created statically before running the configuration; means they were kind of configuration variables for the play.

include_vars:

vars_files 的一个目的是包含一组文件中的变量,但它无法处理以下情况

vars_files serves one purpose of including the vars from a set of files but it cannot handle the cases if

  • vars 文件是动态创建的,您希望将它们包含在游戏中
  • 在有限范围内包含变量.
  • 您有多个 vars 文件,并且希望根据某些条件将它们包含在内,例如如果本地数据库存在,则包括本地数据库的配置,否则包括远程托管数据库的配置.
  • include_vars 具有更高的 priorityvars_files 因此,它可用于覆盖默认配置(vars).
  • include_vars 是惰性求值的(在使用时求值).
  • 您想使用循环动态包含变量.
  • 您想读取一个文件并将所有这些变量放在一个命名字典中,而不是读取全局变量命名空间中的所有变量.
  • 您想在不知道 vars 文件的确切名称的情况下包含目录中的所有文件或目录中的某些文件子集(基于前缀或排除列表).
  • the vars files were created dynamically and you want to include them in play
  • include vars in a limited scope.
  • You have multiple vars files and you want to include them based on certain criteria e.g. if the local database exists then include configuration of local database otherwise include configuration of a remotely hosted database.
  • include_vars have higher priority than vars_files so, it can be used to override default configuration(vars).
  • include_vars are evaluated lazily(evaluated at the time when they are used).
  • You want to include vars dynamically using a loop.
  • You want to read a file and put all those variables in a named dictionary instead of reading all variables in the global variable namespace.
  • You want to include all files in a directory or some subset of files in a directory(based on prefix or exclusion list) without knowing the exact names of the vars file(s).

这些是我能想到的一些情况,如果您想使用上述任何一种情况,那么您需要 include_vars.

These are some of the cases which I can think of and if you want to use any of the above cases then you need include_vars.

这篇关于ansible vars_files 与 include_vars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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