当前位置:网站首页>【ansible问题处理】远程执行用户环境变量加载问题
【ansible问题处理】远程执行用户环境变量加载问题
2022-06-24 13:03:00 【一边学习一边哭】
问题现象
使用ansible命令行执行远程命令
使用command模块,出现报错:[Errno 2] No such file or directory
一样的命令换shell模块,出现报错:/bin/sh: ifconfig: command not foundnon-zero return code
问题原因
通过shell模块的报错可以很清晰的发现,是执行过程中找不到命令。
所以问题原因变得清晰,远程执行过程中环境变量加载不全的问题,导致有些命令找不到。
为什么会导致环境变量加载不全?
ansible命令行远程执行命令时,实际上执行的是non-login shell,non-login shell只会加载~/.bashrc、/etc/bashrc两个文件
关于login shell和non-login shell 加载配置文件
login shell加载环境变量顺序:/etc/profile --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc
non-login shell加载环境变量顺序:~/.bashrc --> /etc/bashrc
问题解决
*方法一:修改ansible配置文件
修改ansible.cfg配置文件,切换用户实现login shell,加载环境变量。这种方法比较合适,修改ansible参数,对于其他使用ansible的用户是无感的。
[privilege_escalation]
become=True
become_method=sudo
become_user=root
#become_ask_pass=False
become_flags='-i'become=True(必须),开启切换用户。ansible命令行选项对应 --become 或 -b
become_method 支持sudo 或者 su,如果使用su切换用户,become_flags需要改成 '-' 或 '-l'。ansible命令行选项对应 --become-method
become_user 填写需要切换的用户。ansible命令行选项对应 --become-user
become_flags sudo或者su命令的选项。ansible命令行没有对应选项,必须要写入ansible.cfg配置文件。
方法二:手动加载环境变量
方法2.1:
可以选择在执行命令或者脚本之前,先执行 source /etc/profile。但是这种方法每次执行的时候都需要先手动加载环境变量
方法2.2:
在远程机器执行 echo "source /etc/profile" >> /etc/bashrc ,这个方法一劳永逸,但是需要远程机器执行一次。
方法三:命令使用绝对路径
这种方法是最笨的。既然只是获取不到环境变量而已,那么只需要每次用到命令的时候,都输入命令的绝对路径就好了。
如果你只是临时解决一下问题,也不想折腾上面两种方法,可以先使用绝对路径解决。
边栏推荐
- 根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]
- Jericho After sleep, the system will wake up regularly and continue to run without resetting [chapter]
- P2pdb white paper
- box-sizing
- Operation of simulated test question bank and simulated test platform for safety production management personnel of fireworks and firecrackers production units in 2022
- Harmony os. (2)
- 如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
- AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)
- ssh-keygen 配置无需每次输入密码
- Solution of channel management system for food and beverage industry: realize channel digital marketing layout
猜你喜欢

PgSQL queries the largest or smallest data of a field in a group

简谈企业Power BI CI /CD 实施框架

数字臧品系统开发 NFT数字臧品系统异常处理源码分享

常见的单例模式&简单工厂

Idea connection MySQL custom generated entity class code

Daily knowledge popularization

HarmonyOS. two

【比特熊故事汇】6月MVP英雄故事|技术实践碰撞境界思维

厨卫电器行业B2B交易协同管理平台开发,优化企业库存结构

Overview of SAP marketing cloud functions (III)
随机推荐
Unity 热力图建立方法
厨卫电器行业B2B交易协同管理平台开发,优化企业库存结构
数商云:加强供应商管理,助推航空运输企业与供应商高效协同
One click to generate University, major and even admission probability. Is it so magical for AI to fill in volunteer cards?
Operation of simulated examination platform of examination question bank for B certificate of Jiangxi provincial safety officer in 2022
食品饮料行业渠道商管理系统解决方案:实现渠道数字化营销布局
百度地图API绘制点及提示信息
Halcon 绘制区域 到图片中
记录一次Mongotemplate的And和Or的各种套
greendao使用问题
Method of inputting dots under letters in markdown/latex
怎样评价国产报表工具和BI软件
初识云原生安全:云时代的最佳保障
Jupiter notebook operation
AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)
Rongyun communication has "hacked" into the heart of the bank
CONDA and pip commands
智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理
laravel下视图间共享数据
#21Set经典案例