# Custom Fact - Drill down into each variable

**URL:** <https://forum.ansible.com/t/custom-fact-drill-down-into-each-variable/24893>\
**Category:** Ansible Project\
**Created:** [July 4, 2017, 5:58pm UTC](https://forum.ansible.com/t/custom-fact-drill-down-into-each-variable/24893 "2017-07-04T17:58:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 4, 2017, 5:58pm UTC](https://forum.ansible.com/t/custom-fact-drill-down-into-each-variable/24893/1 "2017-07-04T17:58:06Z")

</div>

Created a custom fact which only seems to work when not executable - etc/ansible/facts.d/preference.fact

[new facts]  
material = concrete  
structure = square

When the preference.fact is set to executable I get the following error -

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“changed”: false, “cmd”: “/etc/ansible/facts.d/preference.fact”, “failed”: true, “msg”: “[Errno 8] Exec format error”, “rc”: 8}

Fixed that by setting the fact to not executable and the playbook runs fine

My second question is how to drill into the fact to reference each fact separately if i need to ?  
I cant seem to drill in any further than - debug: msg="Custom facts are {{ansible\_local.preference}}

Playbook

---

<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:** [July 4, 2017, 6:30pm UTC](https://forum.ansible.com/t/custom-fact-drill-down-into-each-variable/24893/2 "2017-07-04T18:30:53Z")

</div>

> Created a custom fact which only seems to work when not executable -  
> etc/ansible/facts.d/preference.fact
> 
> [new facts]  
> material = concrete  
> structure = square

I highly recommend against using spaces in fact keys.  
You must likely will have a lot of problems if you do.

> When the preference.fact is set to executable I get the following error -
> 
> TASK [Gathering Facts]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [localhost]: FAILED! =\> {"changed": false, "cmd":  
> "/etc/ansible/facts.d/preference.fact", "failed": true, "msg": "[Errno 8]  
> Exec format error", "rc": 8}
> 
> Fixed that by setting the fact to not executable and the playbook runs fine

This is as expected.  
If a file is executable Ansible will try to run the file, and it need to a valid program/script.  
When the file is not an executable Ansible will read it content.

> My second question is how to drill into the fact to reference each fact  
> separately if i need to ?  
> I cant seem to drill in any further than - debug: msg="Custom facts are  
> {{ansible\_local.preference}}
> 
> Playbook  
> ---  
> - hosts: localhost  
> &nbsp;&nbsp;gather\_facts: on  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Display custom facts  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug: msg="Custom facts are {{ansible\_local.preference.new facts}}"

The space is causing the the problem.  
To make it work with space you would need to use square brackets and not the dot notation.

{{ ansible\_local.preference.['new facts'] }}

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 4, 2017, 7:02pm UTC](https://forum.ansible.com/t/custom-fact-drill-down-into-each-variable/24893/3 "2017-07-04T19:02:20Z")

</div>

Thanks. It works now

Changed the playbook line to this and it works fine → debug: msg=“Custom facts are {{ansible\_local.preference.general.material}}”

Output -

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

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [Display custom facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“changed”: false,  
“msg”: “Custom facts are concrete”
