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:
{{ hostvars['foo.example.com']['ansible_eth0']['ipv4']['address'] }} | |
or, if you want to do it via an index into a group: | |
{{ hostvars[groups['collectors'][0]]['ansible_eth0']['ipv4']['address'] }} | |
or | |
{{ hostvars['10.104.148.136']['db']['stdout'] }} | |
or | |
- name: full-upgrade all packages | |
apt: update_cache=yes upgrade=full | |
register: upgrade_result | |
- name: Display upgrade result on ansible_local node | |
debug: var=hostvars[target_inventory.stdout].upgrade_result.msg | |
or | |
- hosts: localhost | |
tasks: | |
- command: /bin/echo "this is a test" | |
register: foo | |
- hosts: anotherhost | |
tasks: | |
- debug: var=foo | |
when: foo is defined | |
- debug: var=hostvars['localhost']['foo'] | |
when: hostvars['localhost']['foo'] is defined | |
or | |
If foo is the name of the group, use the host, or one of the hosts where the variable was registered. You want to use something like | |
"{{ hostvars['server IP']['ls_out']['stdout_lines'] }}" |

















I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND