ansible vars_files与include_vars [英] ansible vars_files vs include_vars
问题描述
两者之间有什么区别
vars_files:指令
和
-include_vars 模块
何时不赞成使用上述任何一种方法?
vars_files 和 include_vars 都被标记为稳定接口,因此它们都不被弃用.两者都有一些共同点,但解决了不同的目的.
vars_files:
vars_file 指令只能在定义用于指定变量文件的播放时使用.这些文件中的变量包含在剧本中.由于它是在播放开始时使用的,因此很可能暗示其他一些播放(在此播放之前)创建了这些vars文件,或者它们是在运行配置之前静态创建的;表示它们是该剧的配置变量.
include_vars:
vars_files 的一个目的是将一组文件中的vars包括在内,但是如果
- vars文件是动态创建的,您想将它们包括在播放中
- 在有限范围内包括vars.
- 您有多个vars文件,并且要根据某些条件(例如如果本地数据库存在,则包括本地数据库的配置,否则包括远程托管数据库的配置.
- include_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 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 serves one purpose of including the vars from a set of files but it cannot handle the cases if
- 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).
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屋!