ansible

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
}

Solution

  • 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`