0

[NOTE]: This is not like accessing other hosts' yaml data like described here. This is setting my own variable during execution and want to access it later

I am using ansible-playbook to perform some task on 2 vm pools.

Assume that I have only one host in vms1 pool. And I try to get the hostname from that system dynamically and I want to reuse the same variable across the other hosts block in the same file.

I am getting the error saying,

FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'myHostname' is undefined

My playbook yml file looks somewhat like this,

- hosts: vms1
  tasks:
    - name: Getting file list
      shell: ls /my/path/
      register: shellOutput

    - set_fact:
        fileList: "{{ shellOutput.stdout }}"

- hosts: vms2
  tasks:
    - name: Get file list
      shell: "echo {{ fileList }} >> /tmp/hostFileList.txt"
Ashwin
  • 993
  • 1
  • 16
  • 41
  • `set_fact` sets **a fact** not a variable. "*This is setting my own variable during execution and want to access it later*" ー you can't do it. You can set a fact for some dummy host and access it later with `hostvars`. – techraf May 30 '18 at 12:23
  • @techraf : hostvars['dummy']: "{{ shellOutput.stdout }}" like this? – Ashwin May 30 '18 at 14:48
  • 2
    No, you'd need to use [`add_host` module](https://docs.ansible.com/ansible/latest/modules/add_host_module.html) and set the variable. – techraf May 31 '18 at 06:06

0 Answers0