无法收集Ansible主机的事实

时间:2019-04-25 19:29:12

标签: ansible

当我尝试使用控制计算机在主机上设置自定义事实时,在Ansible中设置模块会出现错误

---
  - 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 }}"

当我尝试收集Ansible主机的事实时出现以下错误。我已经建立了一个文件getdate.fact和getrole.fact,它们分别具有代码

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

并且当我尝试运行剧本main.yml时,它将出现错误。

[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
}

2 个答案:

答案 0 :(得分:0)

您可能需要在事实脚本中添加“ shebang”行。即,getdate.fact应该如下所示:

#!/bin/sh

echo [date]
echo date=`date`

答案 1 :(得分:0)

如果我没记错的话,可执行文件应该返回JSON:

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