ansible,

vars_files vs include_vars in Ansible

Mar 01, 2022 · 1 min read · Post a comment

In the last Ansible post I’ve noted that there’s a difference when choosing between vars_files and include_vars. So, let’s explore the differences on a surface level.

Prerequisites

  • Ansible

vars_files

  • vars_files is a directive which can only be used on a playbook level.
  • vars_files is a type of import, so it’s considered as static reuse, meaning
  • It’s pre-processed during playbook parsing.
  • Cannot be used in a loop.

include_vars

  • include_vars is an Ansible module.
  • include_vars is a type of include, so it’s considered as dynamic reuse, meaning
  • Processed at runtime.
  • Mostly used when you want to include variables dynamically.
  • It has a higher priority than vars_files, thus can override default variable values.

Conclusion

Which one to use then? It’s not kind of a straight forward answer. Mostly, it all comes down to static vs dynamic reuse – Comparing includes and imports: dynamic and static re-use. On a side note, follow our official channel on Telegram.