zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    对javascript的一点点认识总结——《javascript高级程序设计》读书笔记
    .Net Garbage Collection学习心得
    上网本重装系统的历程
    让验证控件进行分组验证
    .net应用程序分层的个人认识
    asp.net 4.0的变化(官网链接地址)
    SQL 延时 插入 修改 删除
    SQL 删除前100条 with as
    jQuery RadioButtonList
    网站推广的100个方法
  • 原文地址:https://www.cnblogs.com/leffss/p/11555022.html
Copyright © 2011-2022 走看看