DevOps_DS
DevOps_DS

Reputation: 75

How can I access ansible facts of another server?

I am trying to access Ansible facts for a different host than my target host. Following is my sample playbook. But facts are not collected for util server when I run the below play. Can someone help me, how can I access facts of a different server?

---
- hosts: build
  gather_facts: yes
  vars:
        dist: "{{  hostvars['util']['ansible_facts']['distribution'] }}"
  tasks:
     - name: Demo Magic variables
       debug:
           var: "{{ dist }}"

Upvotes: 5

Views: 5205

Answers (1)

Zeitounator
Zeitounator

Reputation: 44760

Simply gather the needed facts from your desired server prior to using them. The simplest solution:

---
- name: Gather facts from util server
  hosts: util

- name: Do our job
  hosts: build
  vars:
    dist: "{{ hostvars['util']['ansible_facts']['distribution'] }}"
  tasks:
    - name: Demo Magic variables
      debug:
        var: "{{ dist }}"

Upvotes: 5

Related Questions