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;
  • 相关阅读:
    微信小程序:动画(Animation)
    小程序滚动事件之头部渐隐渐现demo
    小程序tab栏可滑动,可点击居中demo
    ES7中前端异步特性:async、await。
    vue中生成二维码
    vue之vue-cookies
    echarts中boundaryGap属性
    ES6数组方法总结
    手写自己的ORM框架For SQlServer(简单的CURD)
    Sqlsever新增作业执行计划傻瓜式操作
  • 原文地址:https://www.cnblogs.com/leffss/p/11555022.html
Copyright © 2011-2022 走看看