🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

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:

{{ 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'] }}"

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.