Ansible - use fact from local host in remote host

2020-03-23 18:31发布

问题:

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?

回答1:

You should use a hostvars magic variable:

{{ hostvars['localhost']['git_tag'] }}