python-2.7 - Ansible 无法在 sbin 文件夹上运行 shell 模块

标签 python-2.7 ansible ansible-2.x

我在特定主机上运行 Ansible Playbook:

当我从 Ansible 执行例如 iptables -L 命令时,我收到此错误:

已更改:[host] => {“已更改”:true,“cmd”:“iptables -L”,“delta”:“0:00:00.018402”,“end”:“2020-04 -26 09:33:11.274857", "failed_when_result": false, "msg": "非零返回码", "rc": 127, "start": "2020-04-26 09:33:11.256455", “stderr”:“/bin/sh:iptables:未找到命令”,“stderr_lines”:[“/bin/sh:iptables:未找到命令”],“stdout”:“”,“stdout_lines”:[]}

剧本示例:

---
- hosts: all
  gather_facts: no
  tasks:
    - name: ls
      shell: tuned -v
      args:
         executable: /usr/sbin

    - name: iptables flush filter
      iptables:
        chain: "{{ item }}"
        flush: yes
      with_items:  [ 'INPUT', 'FORWARD', 'OUTPUT' ]

    - name: Get iptables rules | No resilience comment
      command: iptables -L
      become: yes
      args:
        executable: /sbin

库存文件:

[hosts]
host
[all:vars]
ansible_user=ansible_user
ansible_become_user=root
ansible_ssh_pass=pass
ansible_become=yes

但是机器上安装了 iptables。 我检查了更多命令,发现 /sbin 文件夹中的所有命令均未找到。 什么原因?! 感谢您的帮助

最佳答案

got that all the commands in /sbin folder not found. What the reason

通常原因$PATH变量,其中不包括/sbin位置。最简单的解决方案是使用要运行的二进制文件的完整路径,因此您不需要尝试调用 iptables,而是需要使用 /sbin/iptables

或者,这可能看起来是更好的解决方案,因为它不需要您硬编码路径或编辑任何内容,您可以为整个剧本设置自己的 $PATH,如文档 in Ansible FAQ 所示。 :

environment:
  PATH: "{{ ansible_env.PATH }}:/thingy/bin"
  OTHER_ENV_VAR: its_new_value

请注意,上面的示例将 /thingy/bin 路径附加到 $PATH 的现有值。您可能想先添加它,或者如果需要的话完全替换现有的 PATH。另请注意,ansible_env 通常由事实收集填充(因此您不得禁用它),并且变量的值取决于执行收集操作的用户。如果您更改 remote_userbecome_user,您最终可能会为这些变量使用错误/不同的值。

关于python-2.7 - Ansible 无法在 sbin 文件夹上运行 shell 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61441742/

相关文章:

docker - Centos7 docker-py 好像没有安装

SSH 正常,但 Ansible 返回 "unreachable"

ansible - 想要在远程主机上运行ansible任务

Ansible 处理程序和 shell 模块

loops - 具有动态变量的 Ansible 循环任务

python - 如何使用另一个词典列表更新词典列表?

python - python 中函数的不同行为

mysql - Ansible 给出 MYSQL 表输出

python - python中的全局变量警告

python - Python 中的截断路径