{"id":1894,"date":"2020-11-29T12:40:25","date_gmt":"2020-11-29T12:40:25","guid":{"rendered":"https:\/\/linuxdigest.com\/?p=1894"},"modified":"2020-12-21T10:17:15","modified_gmt":"2020-12-21T10:17:15","slug":"how-to-access-hostvars-in-ansible","status":"publish","type":"post","link":"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/","title":{"rendered":"How to access hostvars in Ansible"},"content":{"rendered":"\n<p>Recently, I have been using Ansible to configure hosts that depend on the settings of other hosts in the play. This is a perfect use case for the magic variable called hostvars. Using hostvars you can access variables from any host that is in the play and use that information in the host you are currently working on. After researching this for one of my projects I decided to write up a little tutorial on how to access hostvars. Hopefully, you will find this useful in your own ansible projects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Get hostvars in playbooks<\/h2>\n\n\n\n<p><strong>To access hostvars you call on the hostvars magic variable. Which is a nested list. So to access the variable application_install_path on the host foo.mydomain.com, you would use hostvars[&#8216;foo.mydomain.com&#8217;][&#8216;application_install_path&#8217;]<\/strong>.<\/p>\n\n\n\n<p>You can also use hostvars to get Ansible facts for other hosts. One instance this could be useful is to get the IP address of a certain server so this server knows where to connect. This could be achieved like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"{{ hostvars&#91;'ansible_facts']&#91;'eth0']&#91;'ipv4']&#91;'address'] }}\"<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Hostvars from multiple hosts<\/h2>\n\n\n\n<p>You can also loop through multiple hosts to get information for each of them. In this example we will get the IP addresses of all the hosts in our play:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{% for host in ansible_play_hosts %}\n   {{ hostvars&#91;host]&#91;'ansible_facts']&#91;'eth0']&#91;'ipv4']&#91;'address'] }}\n{% endfor %}<\/code><\/pre>\n\n\n\n<p>You can also do the same for hosts that match a certain group.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{% for host in groups&#91;'database_servers'] %}\n   {{ hostvars&#91;host]&#91;'ansible_facts']&#91;'ansible_hostname'] }}\n{% endfor %}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to list all available hostvars<\/h2>\n\n\n\n<p>If you don&#8217;t know the name of the variable you are looking for, you can print out all the available hostvars for a particular host. Use the debug module from a playbook like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- debug:\n  var: hostvars&#91;'testserver.linuxdigest.com']\n<\/code><\/pre>\n\n\n\n<p>Alternatively you can discover all variables for a given host directly from the command line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ansible testserver.linuxdigest.com -u root -m setup<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">When not to use hostvars<\/h2>\n\n\n\n<p>When I was researching hostvars, I noticed that there is sometimes a little bit of confusion about what the hostvars variable is used for. This might be because variables for a given host are often called host variables.<\/p>\n\n\n\n<p>When you are trying to get a variable for the host that is currently being worked on you don&#8217;t need to use the hostvars variable. You can just call the variable using curly brackets. Like in this example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- file:\n        path: \"{{ application_install_path }}\"\n        state: directory<\/code><\/pre>\n\n\n\n<p>If you need to get a value from an embedded list like ansible_facts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"{{ ansible_facts&#91;'eth0']&#91;'ipv4']&#91;'address'] }}\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>When you need to get the value of variables from other hosts in the inventory, you can use the hostvars magic variable. There is a little bit of information about magic variables in the <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/user_guide\/playbooks_vars_facts.html#information-about-ansible-magic-variables\" target=\"_blank\" rel=\"noreferrer noopener\">Ansible user guide<\/a>.<\/p>\n\n\n\n<p>I hope this short tutorial helps. I plan to write more tutorials about ansible in the coming months. So stay tuned and don&#8217;t forget to bookmark LinuxDigest.com.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I have been using Ansible to configure hosts that depend on the settings of other&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1923,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[37],"tags":[40,41],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to access hostvars in Ansible - Linux Digest<\/title>\n<meta name=\"description\" content=\"Recently, I have been using Ansible to configure hosts that depend on the settings of other hosts in the play. This is a perfect use case for case for hostvars.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to access hostvars in Ansible - Linux Digest\" \/>\n<meta property=\"og:description\" content=\"Recently, I have been using Ansible to configure hosts that depend on the settings of other hosts in the play. This is a perfect use case for case for hostvars.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Digest\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-29T12:40:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-21T10:17:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/11\/Copy-of-Untitled-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"The Linux Digest Guy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"The Linux Digest Guy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/\"},\"author\":{\"name\":\"The Linux Digest Guy\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\"},\"headline\":\"How to access hostvars in Ansible\",\"datePublished\":\"2020-11-29T12:40:25+00:00\",\"dateModified\":\"2020-12-21T10:17:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/\"},\"wordCount\":443,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"keywords\":[\"ansible\",\"devops\"],\"articleSection\":[\"Devops\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/\",\"url\":\"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/\",\"name\":\"How to access hostvars in Ansible - Linux Digest\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/#website\"},\"datePublished\":\"2020-11-29T12:40:25+00:00\",\"dateModified\":\"2020-12-21T10:17:15+00:00\",\"description\":\"Recently, I have been using Ansible to configure hosts that depend on the settings of other hosts in the play. This is a perfect use case for case for hostvars.\",\"breadcrumb\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/linuxdigest.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to access hostvars in Ansible\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/linuxdigest.com\/#website\",\"url\":\"https:\/\/linuxdigest.com\/\",\"name\":\"Linux Digest\",\"description\":\"Linux tutorials for everyone\",\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/linuxdigest.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/linuxdigest.com\/#organization\",\"name\":\"Linux Digest\",\"url\":\"https:\/\/linuxdigest.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"contentUrl\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"width\":1102,\"height\":170,\"caption\":\"Linux Digest\"},\"image\":{\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\",\"name\":\"The Linux Digest Guy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"caption\":\"The Linux Digest Guy\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to access hostvars in Ansible - Linux Digest","description":"Recently, I have been using Ansible to configure hosts that depend on the settings of other hosts in the play. This is a perfect use case for case for hostvars.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/","og_locale":"en_US","og_type":"article","og_title":"How to access hostvars in Ansible - Linux Digest","og_description":"Recently, I have been using Ansible to configure hosts that depend on the settings of other hosts in the play. This is a perfect use case for case for hostvars.","og_url":"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/","og_site_name":"Linux Digest","article_published_time":"2020-11-29T12:40:25+00:00","article_modified_time":"2020-12-21T10:17:15+00:00","og_image":[{"width":350,"height":200,"url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/11\/Copy-of-Untitled-2.jpg","type":"image\/jpeg"}],"author":"The Linux Digest Guy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"The Linux Digest Guy","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/#article","isPartOf":{"@id":"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/"},"author":{"name":"The Linux Digest Guy","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2"},"headline":"How to access hostvars in Ansible","datePublished":"2020-11-29T12:40:25+00:00","dateModified":"2020-12-21T10:17:15+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/"},"wordCount":443,"commentCount":0,"publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"keywords":["ansible","devops"],"articleSection":["Devops"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/","url":"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/","name":"How to access hostvars in Ansible - Linux Digest","isPartOf":{"@id":"https:\/\/linuxdigest.com\/#website"},"datePublished":"2020-11-29T12:40:25+00:00","dateModified":"2020-12-21T10:17:15+00:00","description":"Recently, I have been using Ansible to configure hosts that depend on the settings of other hosts in the play. This is a perfect use case for case for hostvars.","breadcrumb":{"@id":"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/linuxdigest.com\/howto\/how-to-access-hostvars-in-ansible\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxdigest.com\/"},{"@type":"ListItem","position":2,"name":"How to access hostvars in Ansible"}]},{"@type":"WebSite","@id":"https:\/\/linuxdigest.com\/#website","url":"https:\/\/linuxdigest.com\/","name":"Linux Digest","description":"Linux tutorials for everyone","publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxdigest.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxdigest.com\/#organization","name":"Linux Digest","url":"https:\/\/linuxdigest.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","contentUrl":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","width":1102,"height":170,"caption":"Linux Digest"},"image":{"@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2","name":"The Linux Digest Guy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","caption":"The Linux Digest Guy"}}]}},"_links":{"self":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1894"}],"collection":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/comments?post=1894"}],"version-history":[{"count":4,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1894\/revisions"}],"predecessor-version":[{"id":1899,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1894\/revisions\/1899"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media\/1923"}],"wp:attachment":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media?parent=1894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/categories?post=1894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/tags?post=1894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}