Philip Shangguan
Philip Shangguan

Reputation: 527

How do I pass a variable from one host to another host in Ansible playbook

I need to create a new disk that is 50% bigger than a current data disk on a GCP instance. The playbook I have so far:

- hosts: testserver
  become: yes
  become_user: root

  tasks:
  - name: get disk size
   ansible.builtin.shell: /bin/df -l | /bin/grep "/mnt/data" | /usr/bin/awk '{print $3}'
   register: disk_space_reg

  - debug:
    var: disk_space_reg.stdout_lines[0]


- hosts: localhost
  connection: local

  tasks:
  - debug: var=hostvars['testserver']['disk_space_reg.stdout_lines[0]']
    when: hostvars['testserver']['disk_space_reg.stdout_lines[0]'] is defined

I want to use df -l on the test server to get the disk size, then on the localhost, to use google.cloud.gcp_compute_disk module to create the new disk next. Before creating the disk, I want to see the original disk size, but ansible skipped the second debug, and here is the result:

PLAY [testserver] *********************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************************************
Tuesday 27 July 2021  16:42:55 +0000 (0:00:00.055)       0:00:01.422 **********
ok: [wag-ppc]

TASK [get disk size] *****************************************************************************************************************************************************************************************************************
Tuesday 27 July 2021  16:42:57 +0000 (0:00:01.737)       0:00:03.160 **********
changed: [wag-ppc]

TASK [debug] *************************************************************************************************************************************************************************************************************************
Tuesday 27 July 2021  16:42:58 +0000 (0:00:00.353)       0:00:03.513 **********
ok: [wag-ppc] => {
    "disk_space_reg.stdout_lines[0]": "106975872"
}

PLAY [localhost] *********************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************************************
Tuesday 27 July 2021  16:42:58 +0000 (0:00:00.068)       0:00:03.582 **********
ok: [localhost]

TASK [debug] *************************************************************************************************************************************************************************************************************************
Tuesday 27 July 2021  16:42:58 +0000 (0:00:00.700)       0:00:04.283 **********
skipping: [localhost]

What missed here?

Upvotes: 3

Views: 2466

Answers (1)

dahiya_boy
dahiya_boy

Reputation: 9503

Hostvars: hostvars variables can only access the ansible facts gathered data.

Issue: In your debug you are tring to access the data which is not in your fact.

Solution: Assign your variable to set_fact

Ref: Caching facts

Like registered variables, facts are stored in memory by default. However, unlike registered variables, facts can be gathered independently and cached for repeated use. With cached facts, you can refer to facts from one system when configuring a second system, even if Ansible executes the current play on the second system first.

Upvotes: 1

Related Questions