# Hostvars ansible\_local from another host

**URL:** <https://forum.ansible.com/t/hostvars-ansible-local-from-another-host/5166>\
**Category:** Get Help\
**Created:** [April 26, 2024, 7:53am UTC](https://forum.ansible.com/t/hostvars-ansible-local-from-another-host/5166 "2024-04-26T07:53:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fred/32/5295_2.png) [@Fred](https://forum.ansible.com/u/Fred)\
**Post date:** [April 26, 2024, 7:53am UTC](https://forum.ansible.com/t/hostvars-ansible-local-from-another-host/5166/1 "2024-04-26T07:53:12Z")

</div>

Bonjour, 👋

I’m unable to get “locals facts” from another host. I tried to create collection for Wireguard.

on **wireguard-server.dev** , I have a local fact as:  
`cat /etc/ansible/facts.d/wireguard_client_nextusable_ipv4.fact`

```json
{"wireguard_client_nextusable_ipv4": "10.8.0.5"}

```

On another role, when I try to get this local facts of **wireguard-server.dev** host.

```auto
- name: Deploy Wireguard
  hosts: wireguard-client.dev
  become: yes
  tasks:
  - name: Install Wireguard
    ansible.builtin.import_role:
      name: quanticware.wireguard.install
      tasks_from: tests
    vars:
      wireguard_server_host: "wireguard-server.dev"

```

tasks `tests.yml`

```auto

- ansible.builtin.setup:
    filter: "ansible_local"
  delegate_to: "{{ wireguard_server_host }}"

- ansible.builtin.debug:
    msg: "{{ hostvars[wireguard_server_host][ansible_local][wireguard_client_nextusable_ipv4][wireguard_client_nextusable_ipv4] }}"

```

And execution gives:

```shell
PLAY [Deploy Wireguard] *******************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************************************
Vendredi 26 avril 2024 09:47:23 +0200 (0:00:00.009) 0:00:00.009 ******** 
ok: [wireguard-client.dev]

TASK [quanticware.wireguard.install : ansible.builtin.setup] ******************************************************************************************************************************************************************************************************************************************
Vendredi 26 avril 2024 09:47:24 +0200 (0:00:01.201) 0:00:01.210 ******** 
ok: [wireguard-client.dev -> wireguard-server.dev(192.168.67.6)]

TASK [quanticware.wireguard.install : ansible.builtin.debug] ******************************************************************************************************************************************************************************************************************************************
Vendredi 26 avril 2024 09:47:26 +0200 (0:00:01.235) 0:00:02.446 ******** 
fatal: [wireguard-client.dev]: FAILED! => 
  msg: |-
    The task includes an option with an undefined variable. The error was: ansible.vars.hostvars.HostVarsVars object has no element {'wireguard_client_nextusable_ipv4': {'wireguard_client_nextusable_ipv4': '10.8.0.5'}, 'wireguard_server_ipv4': '{\n "wireguard_client_nextusable_ipv4": "10.8.0.2"\n\n} | to_nice_json\n', 'wireguard_server_ipv6': {'wireguard_server_ipv6': 'fd1d:7e7d:ced8::1/64'}}. ansible.vars.hostvars.HostVarsVars object has no element {'wireguard_client_nextusable_ipv4': {'wireguard_client_nextusable_ipv4': '10.8.0.5'}, 'wireguard_server_ipv4': '{\n "wireguard_client_nextusable_ipv4": "10.8.0.2"\n\n} | to_nice_json\n', 'wireguard_server_ipv6': {'wireguard_server_ipv6': 'fd1d:7e7d:ced8::1/64'}}
  
    The error appears to be in '/Users/fred/labo/devopsotheque/ansible_collections/quanticware/wireguard/roles/install/tasks/tests.yml': line 116, column 3, but may
    be elsewhere in the file depending on the exact syntax problem.
  
    The offending line appears to be:
  
  
    - ansible.builtin.debug:
      ^ here

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************************************
wireguard-client.dev : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0   

Playbook run took 0 days, 0 hours, 0 minutes, 2 seconds
Vendredi 26 avril 2024 09:47:26 +0200 (0:00:00.017) 0:00:02.463 ******** 
=============================================================================== 
Gathering Facts -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1.20s
quanticware.wireguard.install : ansible.builtin.setup ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 1.24s
quanticware.wireguard.install : ansible.builtin.debug ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 0.02s

```

Undefined? But have elements?

When I try:  
`ansible -m setup wireguard-server.dev > setup.json`

```auto
        "ansible_local": {
            "wireguard_client_nextusable_ipv4": {},
            "wireguard_server_ipv4": {},
            "wireguard_server_ipv6": {}
        },

```

Why empty?!

I don’t understand 😟

Thanks 🛟

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [April 26, 2024, 1:49pm UTC](https://forum.ansible.com/t/hostvars-ansible-local-from-another-host/5166/2 "2024-04-26T13:49:35Z")

</div>

When you use `delegate_to`, that will run the task against the delegated target, but the context still _belongs_ to the current inventory host.

So the facts you gathered will be under the current host, not the delegate’s. E.g.

```yaml
---
- hosts: host1
  gather_facts: false
  tasks:
    - ansible.builtin.setup:
      delegate_to: host2

    - ansible.builtin.debug:
        msg: "{{ ansible_facts.fqdn }}"

```

```auto
PLAY [host1] ****************************************************************************************************************************************************************************

TASK [ansible.builtin.setup] *********************************************************************************************************************************************************************************
ok: [host1 -> host2]

TASK [ansible.builtin.debug] *********************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": "host2"
}

PLAY RECAP ***************************************************************************************************************************************************************************************************
host1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

```

So, what you’re looking for is the local facts that now belong to the current host even though they came from the delegate:

```yaml
- ansible.builtin.setup:
    filter: "ansible_local"
  delegate_to: "{{ wireguard_server_host }}"

- ansible.builtin.debug:
    msg: "{{ ansible_local[wireguard_client_nextusable_ipv4][wireguard_client_nextusable_ipv4] }}"

```

That said, if you include the `wireguard_server_host` in your play and gather its facts without delegating; then your `hostvars[wireguard_server_host]` string will work because that host’s vars are now part of the play. You’ll have to use conditionals to keep it from being configured like the rest of your hosts if you need to avoid that.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [April 26, 2024, 7:38pm UTC](https://forum.ansible.com/t/hostvars-ansible-local-from-another-host/5166/3 "2024-04-26T19:38:04Z")

</div>

If your delegated-to host is in your inventory, then you can combine `delegate_to:` with “`delegate_facts: true`” to cause the retrieved facts to be associated with the delegated-to host. See [Delegating facts](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_delegation.html#id6).

(In my limited testing, if the `delegate_to:` host is _not_ in your inventory, then “`delegate_facts: true`” causes the facts just evaporate!)

---

<div class="post-metadata">

**Author:** ![Fred](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fred/32/5295_2.png) [@Fred](https://forum.ansible.com/u/Fred)\
**Post date:** [April 30, 2024, 9:54am UTC](https://forum.ansible.com/t/hostvars-ansible-local-from-another-host/5166/4 "2024-04-30T09:54:10Z")

</div>

Bonjour, 👋

Thanks! Between my initial post and today, I have changed somes variables. But it’s similar situation.

This is works:

```yaml
- name: Deploy Wireguard
  hosts: wireguard-client-01.dev
  become: yes
  vars:
    wireguard_server_host: "wireguard-server.dev"
  tasks:
  - ansible.builtin.setup:
      filter: "ansible_local"
    delegate_to: "{{ wireguard_server_host }}"

  - ansible.builtin.debug:
      msg: "{{ ansible_local.wireguard_server_ipv4.wireguard_server_ipv4 }}"

```

```shell
PLAY [Deploy Wireguard] ***************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************************************
Mardi 30 avril 2024 11:52:14 +0200 (0:00:00.007) 0:00:00.007 *********** 
ok: [wireguard-client-01.dev]

TASK [ansible.builtin.setup] **********************************************************************************************************************************************************************************************************************************************************************
Mardi 30 avril 2024 11:52:16 +0200 (0:00:01.169) 0:00:01.176 *********** 
ok: [wireguard-client-01.dev -> wireguard-server.dev(192.168.67.16)]

TASK [ansible.builtin.debug] **********************************************************************************************************************************************************************************************************************************************************************
Mardi 30 avril 2024 11:52:16 +0200 (0:00:00.875) 0:00:02.052 *********** 
ok: [wireguard-client-01.dev] => 
  msg: 10.8.0.1

PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************************************
wireguard-client-01.dev : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   

Playbook run took 0 days, 0 hours, 0 minutes, 2 seconds
Mardi 30 avril 2024 11:52:16 +0200 (0:00:00.019) 0:00:02.072 *********** 
=============================================================================== 
Gathering Facts ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1.17s
ansible.builtin.setup ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.88s
ansible.builtin.debug ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.02s

```

but with syntax:

```yaml
  - ansible.builtin.debug:
      msg: "{{ ansible_local[wireguard_server_ipv4][wireguard_server_ipv4] }}"

```

I got error!

And if I add `delegate_facts: true` I got error too.

So, what is the difference between,  
`ansible_local[wireguard_server_ipv4][wireguard_server_ipv4`  
and  
`ansible_local.wireguard_server_ipv4.wireguard_server_ipv4` ?

Just to understand if you have any explaination

```auto
ansible --version
ansible [core 2.16.5]
  config file = /Users/fred/labo/devopsotheque/ansible.cfg
  python version = 3.11.8 (main, Feb 6 2024, 21:21:21) [Clang 15.0.0 (clang-1500.1.0.2.5)] (/Users/fred/labo/venv/bin/python3.11)
  jinja version = 3.1.3
  libyaml = True

```

Thanks again @Denney-tech & @utoddl !

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [April 30, 2024, 10:24am UTC](https://forum.ansible.com/t/hostvars-ansible-local-from-another-host/5166/5 "2024-04-30T10:24:06Z")

</div>

> [@Fred](#):
>
> So, what is the difference between,  
> `ansible_local[wireguard_server_ipv4][wireguard_server_ipv4]`  
> and  
> `ansible_local.wireguard_server_ipv4.wireguard_server_ipv4` ?

`ansible_local[wireguard_server_ipv4][wireguard_server_ipv4]` is trying to use the value of the variable `wireguard_server_ipv4` (which doesn’t exist) as the index into or key within `ansible_local`.

To rewrite the equivalent of your second expression in the form of the first, you would spell it thus:

```auto
ansible_local["wireguard_server_ipv4"]["wireguard_server_ipv4"]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 30, 2024, 10:24am UTC](https://forum.ansible.com/t/hostvars-ansible-local-from-another-host/5166/6 "2024-05-30T10:24:13Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
