我在特定主机上运行 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_user
或 become_user
,您最终可能会为这些变量使用错误/不同的值。
关于python-2.7 - Ansible 无法在 sbin 文件夹上运行 shell 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61441742/