Ansible 错误报告 'command not found' 错误

Ansible wrongly reports 'command not found' error

我是 Ansible 的新手,我无法解决错误:我使用 ansible.builtin.shell 调用 pcs 实用程序 (Pacemaker)。 pcs 安装在远程机器上,当我 ssh 那台机器时我可以使用它,但是 Ansible 报告 'command not found' 错误代码 127.

这是我的 inventory.yml:

---
all:
  children:
    centos7:
      hosts:
        UVMEL7:
          ansible_host: UVMEL7

这是我的剧本,TestPcs.yaml:

---
- name: Test the execution of pcs command
  hosts: UVMEL7

  tasks:
   - name: Call echo
     ansible.builtin.shell: echo
   - name: pcs
     ansible.builtin.shell: pcs

注意:我还使用了echo命令来验证我是否正确地使用了ansible.builtin.shell

我启动我的剧本:ansible-playbook -i inventory.yml TestPcs.yaml --user=traite

我得到了这个结果:

PLAY [Test the execution of pcs command] *****************************************************************************************************************************************************************************************************

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

TASK [Call echo] *****************************************************************************************************************************************************************************************************************************
changed: [UVMEL7]

TASK [pcs] ***********************************************************************************************************************************************************************************************************************************
fatal: [UVMEL7]: FAILED! => {"changed": true, "cmd": "pcs", "delta": "0:00:00.003490", "end": "2022-03-10 15:02:17.418475", "msg": "non-zero return code", "rc": 127, "start": "2022-03-10 15:02:17.414985", "stderr": "/bin/sh: pcs : commande introuvable", "stderr_lines": ["/bin/sh: pcs : commande introuvable"], "stdout": "", "stdout_lines": []}

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

pcs 命令失败,stderr 中出现 'command not found' 错误。

另一方面,当我 ssh 机器和 运行 pcs 命令时,命令被执行并且 returns 1 与 127 不同。pcs [=42 是正常的=] 是一个错误:我将测试用例简化到严格的最低限度以使我的问题简短。

我希望 Ansible 具有相同的行为: return 代码 1.

上的电脑出错

这是我模拟 Ansible 所做的事情(基于@Zeitounator 的评论):ssh <user>@<machine> '/bin/bash -c "echo $PATH"'

我得到了 bash 手册页中解释的默认 PATH。在我的系统中 sh 链接到 bash.

我看到 /etc/profile 执行我需要的路径操作。但是,似乎由于选项 -c,bash 未作为登录名 shell 启动,因此 etc/profile 未获得来源。

我最终手动完成了这项工作:

---
- name: Test the execution of pcs command
  hosts: UVMEL7

  tasks:
   - name: Call echo
     ansible.builtin.shell: echo
   - name: pcs
     ansible.builtin.shell: source /etc/profile && pcs

按预期执行 pcs。

综上所述,我的可执行文件没有被执行,因为保存它的文件夹没有在我的 PATH 环境变量中列出。这是因为 /bin/sh aka /bin/bash 是使用标志 -c 调用的,这会阻止采购 /etc/profile 和其他配置文件。问题是 'solved' 通过手动获取正确设置 PATH 环境变量的配置文件。