Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

ansible vars_files vs include_vars

What is the difference between

  • vars_files directive

    and

  • include_vars module

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...