当前位置:网站首页>[ansible problem processing] remote execution user environment variable loading problem
[ansible problem processing] remote execution user environment variable loading problem
2022-06-24 14:21:00 【Crying while learning】
Problem phenomenon
Use ansible The command line executes remote commands
Use command modular , There is an error :[Errno 2] No such file or directory
Same command change shell modular , There is an error :/bin/sh: ifconfig: command not foundnon-zero return code
Question why
adopt shell Module errors can be clearly found , The command was not found during execution .
So the cause of the problem becomes clear , During remote execution Incomplete loading of environment variables , As a result, some commands cannot be found .
Why does it cause incomplete loading of environment variables ?
ansible When the command line executes a command remotely , It's actually executing non-login shell,non-login shell Only... Will be loaded ~/.bashrc、/etc/bashrc Two documents
About login shell and non-login shell Load profile
login shell Load environment variable order :/etc/profile --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc
non-login shell Load environment variable order :~/.bashrc --> /etc/bashrc
Problem solving
* Method 1 : modify ansible The configuration file
modify ansible.cfg The configuration file , Switch user implementation login shell, Load environment variables . This method is more appropriate , modify ansible Parameters , For other uses ansible Of users are insensible .
[privilege_escalation]
become=True
become_method=sudo
become_user=root
#become_ask_pass=False
become_flags='-i'become=True( must ), Enable switching users .ansible Command line options correspond to --become or -b
become_method Support sudo perhaps su, If you use su Switching users ,become_flags Need to change to '-' or '-l'.ansible Command line options correspond to --become-method
become_user Fill in the user to switch .ansible Command line options correspond to --become-user
become_flags sudo perhaps su Command options .ansible There is no corresponding option on the command line , You have to write ansible.cfg The configuration file .
Method 2 : Manually load environment variables
Method 2.1:
You can choose to execute commands or scripts before , Execute first source /etc/profile. But this method needs to manually load environment variables every time it is executed
Method 2.2:
Execute on the remote machine echo "source /etc/profile" >> /etc/bashrc , Once and for all , But you need a remote machine to perform .
Method 3 : The command uses an absolute path
This method is the most stupid . Since you just can't get environment variables , Then you only need to use the command every time , Just enter the absolute path of the command .
If you just solve the problem temporarily , I don't want to toss the above two methods , You can use the absolute path first .
边栏推荐
- leetcode 139. Word Break 单词拆分(中等)
- Jupyter notebook操作
- pip uninstall all packages except builtin package
- postgresql 之 ilist
- 在宇宙的眼眸下,如何正确地关心东数西算?
- 百度地图API绘制点及提示信息
- v-for 中 key的作用和原理
- 成功解决:selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This versi
- Database considerations
- Jerry has opened a variety of decoding formats, and the waiting time from card insertion to playback is long [chapter]
猜你喜欢

The difference between V-IF and v-show

打败 二叉树!

Getting to know cloud native security for the first time: the best guarantee in the cloud Era

不要小看了积分商城,它的作用可以很大

Go语言三个高效编程的技巧
![[leetcode] 10. Regular expression matching](/img/a9/4cdf8d46068a2d7ea6ac212f416157.png)
[leetcode] 10. Regular expression matching

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

Explore cloud native databases and take a broad view of future technological development

Rongyun communication has "hacked" into the heart of the bank

Py之toad:toad的简介、安装、使用方法之详细攻略
随机推荐
C language ---18 function (user-defined function)
探索云原生数据库,纵观未来科技发展
10_那些格调很高的个性签名
港股上市公司公告 API 数据接口
greendao使用问题
Common singleton mode & simple factory
`Thymeleaf ` template engine comprehensive analysis
postgresql 之 ilist
融云通信“三板斧”,“砍”到了银行的心坎上
Jupiter notebook operation
Convolution kernel and characteristic graph visualization
Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]
[learn ZABBIX from scratch] I. Introduction and deployment of ZABBIX
21set classic case
[untitled]
Common sense knowledge points
数商云:加强供应商管理,助推航空运输企业与供应商高效协同
One click to generate University, major and even admission probability. Is it so magical for AI to fill in volunteer cards?
AntD checkbox,限制选中数量
Method of establishing unity thermodynamic diagram