Custom Fact - Drill down into each variable

217 views
Skip to first unread message

Anfield

unread,
Jul 4, 2017, 1:58:06 PM7/4/17
to Ansible Project
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
---
- hosts: localhost
  gather_facts: on
  tasks:
    - name: Display custom facts
      debug: msg="Custom facts are  {{ansible_local.preference.new facts}}"

Kai Stian Olstad

unread,
Jul 4, 2017, 2:31:18 PM7/4/17
to ansible...@googlegroups.com
On 04.07.2017 19:58, Anfield wrote:
> 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
> gather_facts: on
> tasks:
> - name: Display custom facts
> 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'] }}


--
Kai Stian Olstad

Anfield

unread,
Jul 4, 2017, 3:02:20 PM7/4/17
to Ansible Project, ansible-pr...@olstad.com
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"

Reply all
Reply to author
Forward
0 new messages