{"id":13379,"date":"2020-05-13T14:47:21","date_gmt":"2020-05-13T14:47:21","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=13379"},"modified":"2023-07-20T13:08:15","modified_gmt":"2023-07-20T13:08:15","slug":"understanding-ansible-register-variable-scope-and-use-across-playbook-and-hosts","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/understanding-ansible-register-variable-scope-and-use-across-playbook-and-hosts\/","title":{"rendered":"Ansible Variable: Understanding Ansible Register variable scope and use across playbook and hosts"},"content":{"rendered":"\n<p>Registered variables are similar to facts, with a few key differences. Like facts, registered variables are host-level variables. However, registered variables are only stored in memory. Register variables don&#8217;t survive across plays and different hosts than localhost. Registered variables are only valid on the host for the rest of the current playbook run.<\/p>\n\n\n\n<p>Register variables, like facts, are per host. The values can differ depending on the machine. So this looks exactly expected. To access the variable from a different host, you need to go through hostvars, e.g. <strong>${hostvars.foo.time.stdout}<\/strong> should work in your case.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Similar Questions 1 &#8211; If you want to access one hosts facts\/variables from another host then you need to explicitly reference it via the hostvars variable.<\/li>\n\n\n\n<li>Similar Questions 2 &#8211; Pass Ansible variables from one role (running on one host) to another role running on another host within the same playbook<\/li>\n\n\n\n<li>Similar Questions 3 &#8211; How do I set register a variable to persist between plays in ansible?<\/li>\n\n\n\n<li>Similar Questions 4 &#8211; How to access host variable of a different host with Ansible?<br>Solutions<\/li>\n<\/ul>\n\n\n\n<p><strong>The solution<\/strong><\/p>\n\n\n\n<p>The solution is to use Ansible&#8217;s built-in hostvars variable to have the second host explicitly reference the first hosts variable. So modify the first example like this: You can access pretty much any inventory facts\/variables by doing something like this:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/049efee7fa4065e9c1e3dd2d16de70db.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_75630\"><iframe loading=\"lazy\"  id=\"_ytid_26733\"  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_75630\"  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 class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Registered variables are similar to facts, with a few key differences. Like facts, registered variables are host-level variables. However, registered variables are only stored in memory. Register variables don&#8217;t survive&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5038],"tags":[],"class_list":["post-13379","post","type-post","status-publish","format-standard","hentry","category-ansible"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/13379","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=13379"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/13379\/revisions"}],"predecessor-version":[{"id":37158,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/13379\/revisions\/37158"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=13379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=13379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=13379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}