kishan agarwal
kishan agarwal

Reputation: 53

Not able to gather facts of ansible host machine

Set up module in ansible gives an error when i tried to set custom facts on host machine using control machine

---
  - hosts: test-servers
    gather_facts: false
    tasks:

      - name: deleting Facts directory
        file:
          path: /etc/ansible/facts.d/
          state: absent

      - name: Creates a directiory
        file:
          path: /etc/ansible/facts.d/
          recurse: yes
          state: directory

      - name: Copy custom date facts to host machine
        copy:
          src: /app/ansible_poc/roles/custom_facts/templates/facts.d/getdate.fact
          dest: /etc/ansible/facts.d/getdate.fact
          mode: 0755

      - name: Copy custom role facts to host machine
        copy:
          src: /app/ansible_poc/roles/custom_facts/templates/facts.d/getrole.fact
          dest: /etc/ansible/facts.d/getrole.fact
          mode: 0755


      - name: Reloading facts
        setup:

      - name: Display message
        debug:
          msg: "{{ ansible_local.getdate.date.date }}"

      - name: Display message
        debug:
          msg: "{{ ansible_local.getrole.role.role }}"

I get following error when i tried to collect facts of ansible host machine. I have set up a file getdate.fact and getrole.fact which has code respectively

#############getdate.fact###############
echo [date]
echo date= `date`
########################################
#############getrole.fact###############
echo [role]
echo role= `whoami`
########################################

and when i tried to run the playbook main.yml then it following error.

[root@ansibletower tasks]# ansible -m setup test-servers
192.168.111.28 | FAILED! => {
    "changed": false,
    "cmd": "/etc/ansible/facts.d/getdate.fact",
    "msg": "[Errno 8] Exec format error",
    "rc": 8
}
192.168.111.27 | FAILED! => {
    "changed": false,
    "cmd": "/etc/ansible/facts.d/getdate.fact",
    "msg": "[Errno 8] Exec format error",
    "rc": 8
}

Upvotes: 0

Views: 1365

Answers (3)

Anderson Madureira
Anderson Madureira

Reputation: 37

I had this problem today.

This problem appears because the file has execute permission. I changed it to 0644 and it works.

Upvotes: 0

Jack
Jack

Reputation: 6168

If I recall correctly, executables are expected to return JSON:

#!/bin/bash
echo '{ "date" : "'$( date )'" }'

Upvotes: 1

larsks
larsks

Reputation: 311988

You probably need to add "shebang" line to your fact scripts. I.e., getdate.fact should look like:

#!/bin/sh

echo [date]
echo date=`date`

Upvotes: 0

Related Questions