Answers: <p>There are only two or three ways that I've been able to go about doing this and only one of them is a good idea.</p>
  <ol>
  <li>use <code>delegate_facts: true</code> and <code>delegate_to: localhost</code> to set the fact on localhost instead of the host you're running on.  That, and only that, will survive an inventory reload if you do one of those inbetween.</li>
  </ol>
  <pre><code> - name: Get the filename
    hosts: host1
    tasks:
      - command: # find command that returns a filename
        register: filename 
  
      - set_fact:
          global_filename: {{ filename }}
        delegate_to: localhost
        delegate_facts: True
  
   - name: Use the filename on another host
     hosts: host2
     tasks: 
      - command: #reuse the {{ hostvars['localhost']['global_filename'] }} from above. How?
  </code></pre>
  <ol start="2">
  <li>use add_host to add host to and set the variable on there, then you've got the host + the vars you wanted to use.</li>
  </ol>
  <pre><code>- name: Get the filename
    hosts: host1
    tasks:
      - command: # find command that returns a filename
        register: filename 
  
      - add_host:
          hostname: host2
          group: host_twos
          vars: 
          - host1_filename: {{ filename }} 
  
   - name: Use the filename on another host
     hosts: host_twos
     tasks: 
      - command: #reuse the {{ host1_filename }} from above. How?
  
  </code></pre>
  <ol start="3">
  <li>write the facts to /tmp and reload with slurp or something. (I would not suggest this since it is liable to be insecure or get overwritten or plain old suck for a number of sundry reasons)</li>
  </ol>
  <p>The better way, IMO to do this would simply be to delegate_to: host2 and run that task inside the first playbook.  That's the pattern I've consistently taken because every time I try to do 1,2 or 3, I wind up having big problems later on.</p>
  <p>This looks like this:</p>
  <pre><code>- name: Get the filename
    hosts: host1
    tasks:
      - command: # find command that returns a filename
        register: filename 
  
      - command: #reuse the {{ filename }} from above. How?
        delegate_to: host2
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-02-13T10:12:56.470Z
Answers: I am confused. You list 3 ways, two of which you don't recommend, and in the end you recommend yet another solution How would this looks like in my play? I am not sure I understand delegate_to. I need the filename on host2 in order to extract the .tar file with that exavt name 
Answers: Tim
Answers: CC BY-SA 4.0
Answers: 2024-02-13T13:57:12.020Z
Answers: @tim I added some examples, I haven't tested them though, but I've implemented each of these patterns in the past and almost always settled on using delegate_to instead of trying to simulate global variables
Answers: Peter Turner
Answers: CC BY-SA 4.0
Answers: 2024-02-13T14:38:45.883Z
Answers: Thank you very much 
Answers: Tim
Answers: CC BY-SA 4.0
Answers: 2024-02-12T16:00:27.557Z
Answers: 2024-02-13T14:02:26.713Z
Answers: 2024-02-13T14:02:26.713Z
Answers: Peter Turner
Body: <p>I can't get this seemingly easy play to work:</p>
  <pre><code>---
  - name: Get the filename
    hosts: host1
    tasks:
      - command: # find command that returns a filename
        register: filename 
  
  - name: Use the filename on another hose
    hosts: host2
    tasks: 
      - command: #reuse the filename from above. How?
  </code></pre>
  <p>I tried vars, set_fact and hostvars - but I could not find a way to make this work </p>
  <p><strong>How do I declare a global variable that can be set in one task, and then used in another?</strong></p>
  
Tags: <ansible>
Title: How to pass the result from one task (on one host) to another task (on another host) inside an ansible playbook?
UserDisplayName: Tim
ThreadText: # How to pass the result from one task (on one host) to another task (on another host) inside an ansible playbook?
  
  (Asked by: username_0 on 2024-02-12)
  
  I can't get this seemingly easy play to work:
  
  ```
  ---
  - name: Get the filename
    hosts: host1
    tasks:
      - command: # find command that returns a filename
        register: filename 
  
  - name: Use the filename on another hose
    hosts: host2
    tasks: 
      - command: #reuse the filename from above. How?
  
  ```
  
  I tried vars, set\_fact and hostvars - but I could not find a way to make this work
  
  **How do I declare a global variable that can be set in one task, and then used in another?**
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-02-12. Score: 2 | Accepted answer
  
  There are only two or three ways that I've been able to go about doing this and only one of them is a good idea.
  
  1. use `delegate_facts: true` and `delegate_to: localhost` to set the fact on localhost instead of the host you're running on. That, and only that, will survive an inventory reload if you do one of those inbetween.
  
  ```
   - name: Get the filename
    hosts: host1
    tasks:
      - command: # find command that returns a filename
        register: filename 
  
      - set_fact:
          global_filename: {{ filename }}
        delegate_to: localhost
        delegate_facts: True
  
   - name: Use the filename on another host
     hosts: host2
     tasks: 
      - command: #reuse the {{ hostvars['localhost']['global_filename'] }} from above. How?
  
  ```
  
  2. use add\_host to add host to and set the variable on there, then you've got the host + the vars you wanted to use.
  
  ```
  - name: Get the filename
    hosts: host1
    tasks:
      - command: # find command that returns a filename
        register: filename 
  
      - add_host:
          hostname: host2
          group: host_twos
          vars: 
          - host1_filename: {{ filename }} 
  
   - name: Use the filename on another host
     hosts: host_twos
     tasks: 
      - command: #reuse the {{ host1_filename }} from above. How?
  
  ```
  
  3. write the facts to /tmp and reload with slurp or something. (I would not suggest this since it is liable to be insecure or get overwritten or plain old suck for a number of sundry reasons)
  
  The better way, IMO to do this would simply be to delegate\_to: host2 and run that task inside the first playbook. That's the pattern I've consistently taken because every time I try to do 1,2 or 3, I wind up having big problems later on.
  
  This looks like this:
  
  ```
  - name: Get the filename
    hosts: host1
    tasks:
      - command: # find command that returns a filename
        register: filename 
  
      - command: #reuse the {{ filename }} from above. How?
        delegate_to: host2
  
  ```
  
  > **Comments:**
  > **username_0**: I am confused. You list 3 ways, two of which you don't recommend, and in the end you recommend yet another solution How would this looks like in my play? I am not sure I understand delegate_to. I need the filename on host2 in order to extract the .tar file with that exavt name   
  > **username_1**: @tim I added some examples, I haven't tested them though, but I've implemented each of these patterns in the past and almost always settled on using delegate_to instead of trying to simulate global variables  
  > **username_0**: Thank you very much   
  
  ---
  
  