ansible 远程执行时提示 command not found 问题

问题

最近在学习 ansible ,在使用普通用户远程执行 ip a 命令是发现提示错误:/bin/sh: ip: command not found。

原因

command not found 命令未找到,一般想到的是环境变量的问题。网上查找资料,也证实了这个猜测,根本原因是 ansible 登录方式为 non-login shell(与之对应的是 login shell )。login shell 登陆后会加载 /etc/profile、~/.bash_profile,而 non-login shell 登陆后不会加载 /etc/profile、~/.bash_profile,而是加载 /etc/bashrc、~/.bashrc,一般的环境变量都设置在 /etc/profile、~/.bash_profile,所以这时就出现了找不到命令的情况。

  • login shell:取得 shell 需要完整的登录流程,就是说通过输入账号和密码登录系统。
  • non-login shell:取得 shell 不需要重复登录的举动。比如 X Window 登录 linux 后,再以 X 的图形界面启动终端机,此时那个终端机并没有需要输入账号和密码或者在原本的 shell 环境下再次执行 sh 命令,同样也没有输入账号密码就进入新的 shell环境(前一个 shell 的子进程)。

解决方案

  1. 把 /etc/profile、~/.bash_profile中的环境变量同步一份到 /etc/bashrc、~/.bashrc,这种方案不推荐。
  2. 执行命令或脚本时命令都写全路径,我一般采用这种方案。
  3. 执行命令和脚本时前面首先加载环境变量,比如: . /etc/profile &> /dev/null; . ~/.bash_profile &> /dev/null; ip a;
原文地址:https://www.cnblogs.com/leffss/p/11555022.html