Ansible Variable: Understanding Ansible Register variable scope and use across playbook and hosts

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’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.

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. ${hostvars.foo.time.stdout} should work in your case.

  • Similar Questions 1 – If you want to access one hosts facts/variables from another host then you need to explicitly reference it via the hostvars variable.
  • Similar Questions 2 – Pass Ansible variables from one role (running on one host) to another role running on another host within the same playbook
  • Similar Questions 3 – How do I set register a variable to persist between plays in ansible?
  • Similar Questions 4 – How to access host variable of a different host with Ansible?
    Solutions

The solution

The solution is to use Ansible’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:

Rajesh Kumar
Follow me