# collecting local facts in ansible from a custom location (not the default)

**URL:** <https://forum.ansible.com/t/collecting-local-facts-in-ansible-from-a-custom-location-not-the-default/33526>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [July 30, 2020, 10:58am UTC](https://forum.ansible.com/t/collecting-local-facts-in-ansible-from-a-custom-location-not-the-default/33526 "2020-07-30T10:58:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dennis\_McCarthy1](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Dennis\_McCarthy1](https://forum.ansible.com/u/Dennis_McCarthy1)\
**Post date:** [July 30, 2020, 10:58am UTC](https://forum.ansible.com/t/collecting-local-facts-in-ansible-from-a-custom-location-not-the-default/33526/1 "2020-07-30T10:58:25Z")

</div>

Hi,

I’m currently collecting remote host facts like this on my servers:

> - name: Copy fact output to file  
> copy:  
> content: “{{ hostvars[inventory\_hostname]|to\_json }}”  
> dest: ‘/tmp/{{ ansible\_hostname }}.yaml’  
> delegate\_to: localhost

This works well but I want to add some custom/local facts that aren’t located in the default /etc/ansible/facts.d directory. I have fact\_path in my ansible.cfg file under [default].

If I create the local fact file and reload the facts in my playbook, with fact\_path = /tmp the facts are listed in the output:

Playbook code:

> - name: local facts  
> debug: var=ansible\_local  
> notify:
> - reload facts

> - name: reload facts  
> setup: filter=ansible\_local

and the output:

“ansible\_local”: {

“local”: {  
“local\_facts”: {  
“ami\_id”: “ami-12345678900”,  
“avail\_zone”: “eu-west-1b”,  
“callout”: “8-6”,  
“cloud”: “AWS”,  
“environment”: “Dev”,  
“instance\_type”: “t2.micro”,  
“region”: “eu-west-1”,  
“support\_team”: “web”  
}

my custom local.fact file lives in /tmp but when I run ansible to create the hostfile, it’s missing the local facts.

If I change the way I collect the facts it works, but I don’t think it’s the best way to do it and the hostfile changes.

This is how I can make it work:

> - name: Add facts to a variable  
> setup:  
> fact\_path: /tmp  
> register: setupvar

> - name: Store facts indexed by hostname in /tmp/facts  
> copy:  
> content: ‘{{ setupvar }}’  
> dest: /tmp/{{ ansible\_hostname }}.yaml  
> delegate\_to: localhost

I have tried caching facts but the custom/local facts are still missing from the hostfiles.

Does anyone have any suggestions on how I can pick up the fact\_path using the hostvars[inventory\_hostname] method?

Thanks
