2

Not Duplicate: 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 hostname
      shell: hostname
      register: shellOutput

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

- hosts: vms2
  tasks:
    - name: Pinging
      shell: "ping {{ myHostname }} -n 2 >> /tmp/otherHosts.txt"
Ashwin
  • 993
  • 1
  • 16
  • 41
  • I'm having the exact same questions and can't believe this is not easily possible – berlinguyinca Dec 17 '19 at 18:52
  • I'll try to answer this question for future reference, as the suggested answer in post 'accessing inventory host variable in ansible playbook' does not address the question done here. In order to do this, you will need to save the value to a file in your machine: shell: echo {{ shellOutput }} > /tmp/temporal delegate_to: localhost Then, in your next play (or block), you need to "set_facts" from that file, again delegating to localhost. Sorry I can not write this any better, since the issue is closed and I can only comment which does not have a format. – carrotcakeslayer Dec 18 '19 at 21:01

0 Answers0