custom facts - "msg": "[Errno 8] Exec format error: b'/etc/ansible/facts.d/custom.fact'", "rc": 8}},

102 views
Skip to first unread message

Rajesh

unread,
Apr 1, 2020, 10:16:34 PM4/1/20
to Ansible Project
Hi All,

I created a custom facts playbook. But I am getting an error when I run. I could not figure out why?

Playbook

- name: Create a custom facts file
  hosts: database
  become: true
  become_user: root
  gather_facts: true
  tasks:
    - name: Create a custom facts directory
      file:
        path: /etc/ansible/facts.d
        state: directory

    - name: Create a custom facts
      template:
        src: templates/custom.fact.j2
        dest: /etc/ansible/facts.d/custom.fact

    - name: Print ansible_local
      debug: var=ansible_local
    - name: Print the custom facts value
      debug:
        msg: "{{ ansible_local.custom.sample_exam }}"

Template file

[automation@workstation plays]$ cat templates/server_list.j2
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_facts']['fqdn'] }}
{% endfor %}

[automation@workstation plays]$

[automation@workstation plays]$ ansible-playbook facts.yml

PLAY [Create a custom facts file] **********************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************
fatal: [ansible5]: FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"setup": {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "cmd": "/etc/ansible/facts.d/custom.fact", "failed": true, "invocation": {"module_args": {"fact_path": "/etc/ansible/facts.d", "filter": "*", "gather_subset": ["all"], "gather_timeout": 10}}, "msg": "[Errno 8] Exec format error: b'/etc/ansible/facts.d/custom.fact'", "rc": 8}}, "msg": "The following modules failed to execute: setup\n"}

PLAY RECAP *********************************************************************************************************************************************************************************************************************************
ansible5                   : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  

[automation@workstation plays]$

Stefan Hornburg (Racke)

unread,
Apr 2, 2020, 1:46:20 AM4/2/20
to ansible...@googlegroups.com
On 4/2/20 4:16 AM, Rajesh wrote:
> Hi All,
>
> I created a custom facts playbook. But I am getting an error when I run. I could not figure out why?

Hello Rajesh,

if /etc/ansible/facts.d/custom.fact is supposed to be a script it needs executable permissions:

- name: Create a custom facts
template:
src: templates/custom.fact.j2
dest: /etc/ansible/facts.d/custom.fact
mode: 0755

Regards
Racke

>
> *_Playbook_*
>
> - name: Create a custom facts file
>   hosts: database
>   become: true
>   become_user: root
>   gather_facts: true
>   tasks:
>     - name: Create a custom facts directory
>       file:
>         path: /etc/ansible/facts.d
>         state: directory
>
>     - name: Create a custom facts
>       template:
>         src: templates/custom.fact.j2
>         dest: /etc/ansible/facts.d/custom.fact
>
>     - name: Print ansible_local
>       debug: var=ansible_local
>     - name: Print the custom facts value
>       debug:
>         msg: "{{ ansible_local.custom.sample_exam }}"
>
> _*Template file*_
>
> [automation@workstation plays]$ cat templates/server_list.j2
> {% for host in groups['all'] %}
> {{ hostvars[host]['ansible_facts']['fqdn'] }}
> {% endfor %}
>
> [automation@workstation plays]$
>
> [automation@workstation plays]$ ansible-playbook facts.yml
>
> PLAY [Create a custom facts file]
> **********************************************************************************************************************************************************************************************************
>
> TASK [Gathering Facts]
> *********************************************************************************************************************************************************************************************************************
> fatal: [ansible5]: FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"setup": {"ansible_facts":
> {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "cmd": "/etc/ansible/facts.d/custom.fact", "failed":
> true, "invocation": {"module_args": {"fact_path": "/etc/ansible/facts.d", "filter": "*", "gather_subset": ["all"],
> "gather_timeout": 10}}, "msg": "[Errno 8] Exec format error: b'/etc/ansible/facts.d/custom.fact'", "rc": 8}}, "msg":
> "The following modules failed to execute: setup\n"}
>
> PLAY RECAP
> *********************************************************************************************************************************************************************************************************************************
> ansible5                   : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  
>
> [automation@workstation plays]$
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/5b9d27a2-cc4e-40b1-9429-284adf50524d%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/5b9d27a2-cc4e-40b1-9429-284adf50524d%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Rajesh

unread,
Apr 2, 2020, 2:38:27 AM4/2/20
to Ansible Project
Thanks for that. I have modified the playbook as you suggested. The playbook creates the custom file, but still giving the below error.

[automation@workstation plays]$ cat facts.yml
- name: Create a custom facts file
  hosts: database
  become: true
  become_user: root
  gather_facts: true
  tasks:
    - name: Create a custom facts directory
      file:
        path: /etc/ansible/facts.d
        state: directory

    - name: Create a custom facts
      template:
        src: templates/custom.fact.j2
        dest: /etc/ansible/facts.d/custom.fact
        mode: 0755

    - name: Output our newly created static fact
      debug:
        msg: "{{ ansible_local.custom.sample_exam }}"
[automation@workstation plays]$

[automation@workstation plays]$ ansible-playbook facts.yml

PLAY [Create a custom facts file] **********************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************
ok: [ansible5]

TASK [Create a custom facts directory] *****************************************************************************************************************************************************************************************************
ok: [ansible5]

TASK [Create a custom facts] ***************************************************************************************************************************************************************************************************************
changed: [ansible5]

TASK [Output our newly created static fact] ************************************************************************************************************************************************************************************************
fatal: [ansible5]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'custom'\n\nThe error appears to be in '/home/automation/plays/facts.yml': line 18, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Output our newly created static fact\n      ^ here\n"}

PLAY RECAP *********************************************************************************************************************************************************************************************************************************
ansible5                   : ok=3    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  

[automation@workstation plays]$

Rajesh

unread,
Apr 4, 2020, 4:18:48 AM4/4/20
to Ansible Project
Could someone help why I am still getting the below error?

Thanks,
Rajesh

Jean-Yves LENHOF

unread,
Apr 4, 2020, 4:35:12 AM4/4/20
to ansible...@googlegroups.com

The first time you run it, files are not yet in place, so you probably need to regather fact, no ? You can do it with setup:

Regards,

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/01148e2a-5846-447b-857a-4dcecdad6e63%40googlegroups.com.

Dennis McCarthy

unread,
Apr 4, 2020, 5:49:51 AM4/4/20
to ansible...@googlegroups.com
I've written a GitHub repo to demo using local_facts here:  https://github.com/dmccuk/ansible_local_facts

The bit you need to do is this (which is also part of the demo):

- name: local facts
  debug: var=ansible_local
  notify:
  - reload facts

- name: reload facts
  setup: filter=ansible_local




> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/5b9d27a2-cc4e-40b1-9429-284adf50524d%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/5b9d27a2-cc4e-40b1-9429-284adf50524d%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/01148e2a-5846-447b-857a-4dcecdad6e63%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
Message has been deleted

Rajesh

unread,
Apr 5, 2020, 2:21:25 AM4/5/20
to Ansible Project
Thanks Dennis. I have modified my playbook as you suggested. But I am still getting the below error.

- name: Create a custom facts file
  hosts: database
  become: true
  become_user: root
  gather_facts: true
  tasks:
    - name: Create a custom facts directory
      file:
        path: /etc/ansible/facts.d
        state: directory

    - name: Create a custom facts
      template:
        src: templates/custom.fact.j2
        dest: /etc/ansible/facts.d/custom.fact
        mode: 0755
   
    - name: local facts
      debug: var=ansible_local
      notify:
        - reload facts

    - name: reload facts
      setup: filter=ansible_local

[automation@workstation plays]$ cat templates/custom.fact.j2
[sample_exam]
server_role=mysql


[automation@workstation plays]$ ansible-playbook facts.yml

PLAY [Create a custom facts file] **********************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************
ok: [ansible5]

TASK [Create a custom facts directory] *****************************************************************************************************************************************************************************************************
ok: [ansible5]

TASK [Create a custom facts] ***************************************************************************************************************************************************************************************************************
changed: [ansible5]

TASK [local facts] *************************************************************************************************************************************************************************************************************************
ok: [ansible5] => {
    "ansible_local": {}
}

TASK [reload facts] ************************************************************************************************************************************************************************************************************************
fatal: [ansible5]: FAILED! => {"changed": false, "cmd": "/etc/ansible/facts.d/custom.fact", "msg": "[Errno 8] Exec format error: b'/etc/ansible/facts.d/custom.fact'", "rc": 8}

PLAY RECAP *********************************************************************************************************************************************************************************************************************************
ansible5                   : ok=4    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/5b9d27a2-cc4e-40b1-9429-284adf50524d%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/5b9d27a2-cc4e-40b1-9429-284adf50524d%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Rajesh

unread,
Apr 5, 2020, 2:22:10 AM4/5/20
to Ansible Project
Tried that but still not working. Thanks for your help.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Dennis McCarthy

unread,
Apr 5, 2020, 4:03:10 AM4/5/20
to ansible...@googlegroups.com
try deleting:  /etc/ansible/facts.d

and let ansible recreate it. If you still get the same issue (delete /etc/ansible/facts.d again) and try running ansible either as root or with sudo just to rule out permissions.



> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/5b9d27a2-cc4e-40b1-9429-284adf50524d%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/5b9d27a2-cc4e-40b1-9429-284adf50524d%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/01148e2a-5846-447b-857a-4dcecdad6e63%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5000fa54-3f67-4d99-8f2c-6d1a33cb6b58%40googlegroups.com.

Jean-Yves LENHOF

unread,
Apr 5, 2020, 4:08:27 AM4/5/20
to ansible...@googlegroups.com

Hi,

Your fact is simple INI file, not a script which give json output.

Try removing the execution bit on your fact (644 for example)

Info provided here with some 2 mn googling :

https://groups.google.com/forum/#!topic/ansible-project/2JRR8a-K9_8

Regards,

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5000fa54-3f67-4d99-8f2c-6d1a33cb6b58%40googlegroups.com.

Rajesh Bandar

unread,
Apr 6, 2020, 7:36:32 AM4/6/20
to ansible...@googlegroups.com
Thanks a lot Jean-Yves. I have changed the mode to 0644 and ran the playbook. It worked fine without any issues. I can't find good documentation on custom facts. Thanks for your help.

- name: Create a custom facts file
  hosts: database
  become: true
  become_user: root
  gather_facts: true
  tasks:
    - name: Create a custom facts directory
      file:
        path: /etc/ansible/facts.d
        state: directory

    - name: Create a custom facts
      template:
        src: templates/custom.fact.j2
        dest: /etc/ansible/facts.d/custom.fact
        mode: 0644

   
    - name: local facts
      debug: var=ansible_local
      notify:
        - reload facts

    - name: reload facts
      setup: filter=ansible_local
[automation@workstation plays]$ ansible-playbook facts.yml

PLAY [Create a custom facts file] **********************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************
ok: [ansible5]

TASK [Create a custom facts directory] *****************************************************************************************************************************************************************************************************
ok: [ansible5]

TASK [Create a custom facts] ***************************************************************************************************************************************************************************************************************
ok: [ansible5]


TASK [local facts] *************************************************************************************************************************************************************************************************************************
ok: [ansible5] => {
    "ansible_local": {
        "custom": {
            "sample_exam": {
                "server_role": "mysql"
            }
        }
    }
}

TASK [reload facts] ************************************************************************************************************************************************************************************************************************
ok: [ansible5]

PLAY RECAP *********************************************************************************************************************************************************************************************************************************
ansible5                   : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  



> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/5b9d27a2-cc4e-40b1-9429-284adf50524d%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/5b9d27a2-cc4e-40b1-9429-284adf50524d%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/01148e2a-5846-447b-857a-4dcecdad6e63%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5000fa54-3f67-4d99-8f2c-6d1a33cb6b58%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Rajesh Bandar

unread,
Apr 6, 2020, 7:37:25 AM4/6/20
to ansible...@googlegroups.com
Thanks Dennis for your help. Once I changed the mode to "644" as suggested by Jean-Yves, the playbook worked fine.

Reply all
Reply to author
Forward
0 new messages