1

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?

1 Answer 1

2

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?
  1. 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?

  1. 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
3
  • 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 🤔
    – Tim
    Commented Feb 13 at 10:12
  • 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
    – Peter Turner
    Commented Feb 13 at 13:57
  • Thank you very much 👍
    – Tim
    Commented Feb 13 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.