Rafaa Ferid
Rafaa Ferid

Reputation: 178

Ansible: Custom facts - error loading facts as JSON or ini

shell> ansible ubuntu-c -m setup -m "filter=ansible_local"
...

I am trying to practice Ansible Custom facts: /etc/ansible/facts.d/try.fact

#!bin/bash
echo {\""thing\"" : \""value\""}

But in keep getting this error:

"try": "error loading facts as JSON or ini - please check content: /etc/ansible/facts.d/try.fact" 

thought when i try to excute it using :

sh ./etc/ansible/facts.d/try.fact

it returns the correct JSON format:

shell> sh /etc/ansible/facts.d/try.fact
{"thing": "value"}

Upvotes: 0

Views: 1769

Answers (3)

user21819272
user21819272

Reputation: 170

I am also working on creating custom-facts and this issue occurs when:

  • The /etc/ansible/facts.d/<factFileName>.factfile is not executable
  • The created fact file is not in the dictionary/JSON format

In your case, this issue is likely occuring due to the lack of executable permission on your fact file.

Upvotes: 1

Vladimir Botka
Vladimir Botka

Reputation: 68034

Let's use a simple playbook

- hosts: localhost
  gather_facts: true
  tasks:
    - debug:
        var: ansible_local

Both, sh/bash and the python scripts

shell> cat /etc/ansible/facts.d/try-exe.fact
#!/bin/sh
printf '%s' '{"thing": "value"}'
shell> cat /etc/ansible/facts.d/try-exe.fact
#!/usr/bin/env python
import sys
print('{"thing": "value"}')
shell> /etc/ansible/facts.d/try-exe.fact
{"thing": "value"}

work as expected

TASK [debug] ******************************************************
ok: [localhost] => 
  ansible_local:
    try-exe:
      thing: value

Also, static files both JSON and INI work as expected

shell> cat /etc/ansible/facts.d/try-json.fact
{"thing": "value"}
shell> cat /etc/ansible/facts.d/try-ini.fact
[default]
thing=value

give

TASK [debug] *******************************************************
ok: [localhost] => 
  ansible_local:
    try-ini:
      default:
        thing: value
    try-json:
      thing: value

Upvotes: 2

flowerysong
flowerysong

Reputation: 2939

As noted in the documentation, the file should be marked as executable if you want it to be executed, otherwise it is treated as a static file containing structured data. To test this you should be executing the file directly (/etc/ansible/facts.d/try.fact), not passing it to sh.

chmod +x /etc/ansible/facts.d/try.fact
/etc/ansible/facts.d/try.fact

Upvotes: 2

Related Questions