Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - use fact from local host in remote host template

I have a playbook that contains roles for localhost and roles for remote hosts.

In one of the localhost roles I set a fact called git_tag.

I want to use this fact in a template for the remote hosts.

I tried:

- name: Read Version
  set_fact:
    git_tag: "{{ package_json.stdout | from_json | json_query('version')}}"
  delegate_to: "test-server"

But when Ansible reaches the role that reads the template that has {{ git_tag }} it says that git_tag is undefined.

I'm sure I'm doing something wrong. How can I do it?

like image 574
Saar Kuriel Avatar asked Jul 03 '17 12:07

Saar Kuriel


1 Answers

You should use a hostvars magic variable:

{{ hostvars['localhost']['git_tag'] }}
like image 200
techraf Avatar answered Oct 01 '22 20:10

techraf