# delegate\_to: localhost and gather\_facts: yes playbook still holds previous hosts facts.

**URL:** <https://forum.ansible.com/t/delegate-to-localhost-and-gather-facts-yes-playbook-still-holds-previous-hosts-facts/26203>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [December 5, 2017, 7:38am UTC](https://forum.ansible.com/t/delegate-to-localhost-and-gather-facts-yes-playbook-still-holds-previous-hosts-facts/26203 "2017-12-05T07:38:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Snehal\_Biche](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@Snehal\_Biche](https://forum.ansible.com/u/Snehal_Biche)\
**Post date:** [December 5, 2017, 7:38am UTC](https://forum.ansible.com/t/delegate-to-localhost-and-gather-facts-yes-playbook-still-holds-previous-hosts-facts/26203/1 "2017-12-05T07:38:15Z")

</div>

Ansible fixed the bug as misunderstood and also`gather_facts`a play directive, it does not work on tasks.

then whats the way to get localhost facts?

1. create role and role YML file with remote hosts defined as

2. create tasks file which actually needs to gather fact of localhost

3. run the role yml file

```auto
role file :- 
-----------------------------
---
- name: My role name
  hosts: windows
  gather_facts: yes
  vars_files:
    - ./roles/myrole/vars/main.yml

  roles:
    - { role: myrole, tags: ["myrole"] }
------------------------------------------------------------

Tasks file :- 
------------------------------------------------------------
---
#---------------------------------------------------------------------------
  - name: Windows OS User home directory
    delegate_to: localhost
    gather_facts: true
    set_fact:
       userhomedir: '{{ ansible_user_dir }}\.m2'
    register: userhomedirwin
    when: ansible_os_family == "Windows"
    tags:
        - winuserhome
  - name: DEBUG Windows OS User home directory
    debug:
      var: userhomedirwin
      verbosity: 3
    when: ansible_os_family == "Windows"
  - name: Linux OS User home directory
    delegate_to: localhost
    gather_facts: true
    set_fact:
       userhomedir: "{{ ansible_user_dir }}/.m2"
    when: ansible_os_family != "Windows"
    register: userhomedirlin
    tags:
          - linuserhome
  - name: DEBUG Linux OS User home directory
    debug:
      var: userhomedirlin
      verbosity: 3
    when: ansible_os_family != "Windows"
  - name: Creating Maven settting config file from template
    local_action: template src={{ srcSettingsXml }} dest={{ destSettingsXml }}
    register: settingxml
    tags:
        - settingxml
  - name: DEBUG Creating Maven Setting xml config file from template
    debug:
      var: settingxml
      verbosity: 3
#---------------------------------------------------------------------------

```

`

```auto
TASK [cobrowse : Windows OS User home directory] ************************************************************************************
task path: /root/svdevops/roles/myrole/tasks/Pre_Setup.yml:57
ok: [192.168.100.222 -> localhost] => {
    "ansible_facts": {
        "userhomedir": "C:\Users\Administrator\.m2"
    },
    "changed": false,
    "failed": false
}
Read vars_file './roles/myrole/vars/main.yml'

TASK [cobrowse : DEBUG Windows OS User home directory] ******************************************************************************
task path: /root/svdevops/roles/myrole/tasks/Pre_Setup.yml:66
ok: [192.168.100.222] => {
    "userhomedirwin": {
        "ansible_facts": {
            "userhomedir": "C:\Users\Administrator\.m2"
        },
        "ansible_facts_cacheable": false,
        "changed": false,
        "failed": false
    }
}
Read vars_file './roles/myrole/vars/main.yml'
Read vars_file './roles/myrole/vars/main.yml'

TASK [cobrowse : Linux OS User home directory] **************************************************************************************
task path: /root/svdevops/roles/myrole/tasks/Pre_Setup.yml:71
skipping: [192.168.100.222] => {
    "changed": false,
    "skip_reason": "Conditional result was False",
    "skipped": true
}
Read vars_file './roles/myrole/vars/main.yml'

TASK [cobrowse : DEBUG Linux OS User home directory] ********************************************************************************
task path: /root/svdevops/roles/myrole/tasks/Pre_Setup.yml:80
skipping: [192.168.100.222] => {
    "skip_reason": "Conditional result was False"
}
Read vars_file './roles/myrole/vars/main.yml'
Read vars_file './roles/myrole/vars/main.yml'

```

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 6, 2017, 3:26pm UTC](https://forum.ansible.com/t/delegate-to-localhost-and-gather-facts-yes-playbook-still-holds-previous-hosts-facts/26203/2 "2017-12-06T15:26:35Z")

</div>

> Ansible fixed the bug as misunderstood and alsogather\_factsa play  
> directive, it does not work on tasks.
> 
> then whats the way to get localhost facts?
> 
> &nbsp;&nbsp;&nbsp;1. create role and role YML file with remote hosts defined as  
> &nbsp;&nbsp;&nbsp;2. create tasks file which actually needs to gather fact of localhost

It looks like you av mixing gather facts versus setting facts.

Gather facts is about getting a lot of information about a host, what actually happening is it runs the setup module on the host.  
Setting facts is setting one or more facts/variables on a host with set\_facts module.

> &nbsp;&nbsp;&nbsp;3. run the role yml file
> 
> role file :-  
> -----------------------------  
> ---  
> - name: My role name  
> &nbsp;&nbsp;hosts: windows  
> &nbsp;&nbsp;gather\_facts: yes

This is correct use of gather\_facts and only place it's allowed to can be used, on the play.

> &nbsp;&nbsp;vars\_files:  
> &nbsp;&nbsp;&nbsp;&nbsp;- ./roles/myrole/vars/main.yml
> 
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;- { role: myrole, tags: ["myrole"] }  
> ------------------------------------------------------------
> 
> Tasks file :-  
> ------------------------------------------------------------  
> ---  
> #---------------------------------------------------------------------------  
> &nbsp;&nbsp;- name: Windows OS User home directory  
> &nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
> &nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: true  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userhomedir: '{{ ansible\_user\_dir }}\.m2'  
> &nbsp;&nbsp;&nbsp;&nbsp;register: userhomedirwin  
> &nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_os\_family == "Windows"  
> &nbsp;&nbsp;&nbsp;&nbsp;tags:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- winuserhome

As already mention gather\_facts can only be used on plays, not on tasks.

> &nbsp;&nbsp;- name: DEBUG Windows OS User home directory  
> &nbsp;&nbsp;&nbsp;&nbsp;debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: userhomedirwin  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;verbosity: 3  
> &nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_os\_family == "Windows"  
> &nbsp;&nbsp;- name: Linux OS User home directory  
> &nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
> &nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: true  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userhomedir: "{{ ansible\_user\_dir }}/.m2"  
> &nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_os\_family != "Windows"  
> &nbsp;&nbsp;&nbsp;&nbsp;register: userhomedirlin  
> &nbsp;&nbsp;&nbsp;&nbsp;tags:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- linuserhome  
> &nbsp;&nbsp;- name: DEBUG Linux OS User home directory  
> &nbsp;&nbsp;&nbsp;&nbsp;debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: userhomedirlin  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;verbosity: 3  
> &nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_os\_family != "Windows"  
> &nbsp;&nbsp;- name: Creating Maven settting config file from template  
> &nbsp;&nbsp;&nbsp;&nbsp;local\_action: template src={{ srcSettingsXml }} dest={{ destSettingsXml }}  
> &nbsp;&nbsp;&nbsp;&nbsp;register: settingxml  
> &nbsp;&nbsp;&nbsp;&nbsp;tags:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- settingxml  
> &nbsp;&nbsp;- name: DEBUG Creating Maven Setting xml config file from template  
> &nbsp;&nbsp;&nbsp;&nbsp;debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: settingxml  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;verbosity: 3

That said, I'm not sure what it is you are trying to achieve.
