{"id":6100,"date":"2019-07-03T02:14:02","date_gmt":"2019-07-03T02:14:02","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=6100"},"modified":"2021-11-16T11:33:23","modified_gmt":"2021-11-16T11:33:23","slug":"difference-between-ansible-vars_files-vs-include_vars","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/difference-between-ansible-vars_files-vs-include_vars\/","title":{"rendered":"Difference between ansible vars_files vs include_vars"},"content":{"rendered":"\n<p>Both of them have some commonalities but they solve different purposes. vars_files are read when the play starts. include_vars are read when the play reaches the task.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Format<br> vars_files: directive<br> and<br> include_vars module <\/p><\/blockquote>\n\n\n\n<p><strong>vars_files:<\/strong><br> 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.<\/p>\n\n\n\n<p><strong>include_vars:<\/strong><br> vars_files serves one purpose of including the vars from a set of files but it cannot handle the cases if<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The vars files were created dynamically and you want to include them in play<\/li><li>include vars in a limited scope.<\/li><li>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.<\/li><li>include_vars have higher priority than vars_files so, it can be used to override default configuration(vars).<\/li><li>include_vars are evaluated lazily(evaluated at the time when they are used).<\/li><li>You want to include vars dynamically using a loop.<\/li><li>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.<\/li><li>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).<\/li><\/ul>\n\n\n\n<p>Ansible variable precedence<\/p>\n\n\n\n<p><strong>Reference<\/strong><br> Ansible variable precedence<\/p>\n\n\n\n<p><strong>Reference<\/strong><br> <a href=\"https:\/\/www.devopsschool.com\/tutorial\/ansible\/variables\/\">Ansible Variable Reference<\/a><br> <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/user_guide\/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable\" target=\"_blank\" rel=\"noopener\">Ansible variable precedence<\/a><\/p>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_73231\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_96117\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_73231\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Both of them have some commonalities but they solve different purposes. vars_files are read when the play starts. include_vars are read when the play reaches the task. Format vars_files: directive and include_vars module vars_files: vars_file directive can only be used when defining a play to specify variable files. The variables from those files are included&#8230;<\/p>\n","protected":false},"author":1,"featured_media":8130,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5038],"tags":[639,5449,492,213,5599],"class_list":["post-6100","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","tag-ansible","tag-devopsschool","tag-difference","tag-tutorial","tag-vars"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=6100"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6100\/revisions"}],"predecessor-version":[{"id":25510,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6100\/revisions\/25510"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/8130"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=6100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=6100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=6100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}