How to user hostvars

25 views
Skip to first unread message

Olivier Studer

unread,
Aug 3, 2023, 9:43:30 AM8/3/23
to Ansible Project
Hi,

I want to use hostvars to use a result of a task runing for on an host to apply this values/result to another host. The "register" can not be use because it is dedicated to the host, not sharing.

The goals is to active the Slave Mariadb server with the value from the Master server. 

I think that I need to use the host-inventory (ini file) with this value. How can write this value ?

Thanks for your help
Regards
Olivier

Pierre TOURON

unread,
Aug 7, 2023, 1:59:55 PM8/7/23
to Ansible Project
Hi,

I'm pretty sure you could query facts cache to that end :

$ less test_facts_sharing.yml
---

- name: Local host play
  connection: local
  hosts: localhost
  gather_facts: false
  tasks:
  - name: Store value
    ansible.builtin.set_fact:
      foo: "bar"

- name: Remote host play
  hosts: anotherhost
  gather_facts: false
  tasks:
    - name: Print stored value from another host
      ansible.builtin.debug:
        var: hostvars['localhost']['foo']

$ ansible-playbook -i ~/conf/ansible/inventories/ test_facts_sharing.yml

PLAY [Local host play] *******************************************************************************************************************************************************************************************************************************************************************

TASK [Store value] ***********************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

PLAY [Remote host play] ******************************************************************************************************************************************************************************************************************************************************************

TASK [Print stored value from another host] **********************************************************************************************************************************************************************************************************************************************
ok: [anotherhost] => {
    "hostvars['localhost']['foo']": "bar"
}

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************************
anotherhost                    : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Reply all
Reply to author
Forward
0 new messages