Home > Software design >  Is there a way in Ansible to retrieve a variable located on a different host, to get it on the local
Is there a way in Ansible to retrieve a variable located on a different host, to get it on the local

Time:07-26

I have the following playbook:

    - name: Some action on the workers
      hosts: workers
      gather_facts: false
      run_once: true
      tasks:
        - name: Set var for server information
          set_fact:
            server_info:
              "name": "winserver"
              "os": "Microsoft Windows Server 2019 Datacenter"
    
    - name: Some action on the localhost
      hosts: localhost
      gather_facts: false
      run_once: true
      tasks:
        - name: Show script stdout
          debug:
            msg:
              - "{{ server_info }}"

The hosts is actually a group of servers put in a group named workers (for example server1, server2 and server3), where just one is chosen (arbitrary) to run this task. Now I need to retrieve the information from this variable on the localhost, but as I don't know on which server the first task runs, I cannot explicitly reference it by using:

"{{ hostvars['server2']['server_info'] }}"

Does someone know if there is a way to retrieve this variable on the localhost?

CodePudding user response:

Q: "I don't know on which server the first task runs."

A: It's irrelevant which server the first task runs on. The variable server_info will be declared on all of them. For example, given the inventory

shell> cat hosts
[workers]
server1
server2
server

The playbook

- hosts: workers
  gather_facts: false
  run_once: true
  tasks:
    - set_fact:
        server_info: winserver
    - debug:
        var: hostvars[item]['server_info']
      loop: "{{ ansible_play_hosts_all }}"

gives

TASK [debug] *********************************************************
ok: [server1] => (item=server1) => 
  ansible_loop_var: item
  hostvars[item]['server_info']: winserver
  item: server1
ok: [server1] => (item=server2) => 
  ansible_loop_var: item
  hostvars[item]['server_info']: winserver
  item: server2
ok: [server1] => (item=server3) => 
  ansible_loop_var: item
  hostvars[item]['server_info']: winserver
  item: server

You can pick any host you like. For example,

- hosts: localhost
  gather_facts: false
  run_once: true
  tasks:
    - debug:
        var: hostvars.server2.server_info

gives

TASK [debug] ************************************************************
ok: [localhost] => 
  hostvars.server2.server_info: winserver

CodePudding user response:

I just found the answer to this question myself: through the use of

delegate_to: localhost
delegate_facts: true

This way the variable gets stored on the localhost.

- name: Some action on the workers
  hosts: workers
  gather_facts: false
  run_once: true
  tasks:
    - name: Set var for server information
      set_fact:
        server_info:
          "name": "winserver"
          "os": "Microsoft Windows Server 2019 Datacenter"
      delegate_to: localhost
      delegate_facts: true
    
- name: Some action on the localhost
  hosts: localhost
  gather_facts: false
  run_once: true
  tasks:
    - name: Show script stdout
      debug:
        msg:
          - "{{ server_info }}"
  • Related