当前位置:网站首页>[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 .
边栏推荐
- 数字臧品系统开发 NFT数字臧品系统异常处理源码分享
- Unit contour creation method
- Development of digital Tibetan product system NFT digital Tibetan product system exception handling source code sharing
- 鲲鹏arm服务器编译安装PaddlePaddle
- Second, the examinee must see | consolidate the preferred question bank to help the examinee make the final dash
- AQS初探
- 六月集训(第24天) —— 线段树
- pip uninstall all packages except builtin package
- SAP Marketing Cloud 功能概述(三)
- C language ---18 function (user-defined function)
猜你喜欢
随机推荐
Antd checkbox, limit the selected quantity
Redis interview questions
Halcon draw area into picture
win10系统问题
Digital business cloud: strengthen supplier management and promote efficient collaboration between air transport enterprises and suppliers
10_ Those high-profile personal signatures
融云通信“三板斧”,“砍”到了银行的心坎上
数字臧品系统开发 NFT数字臧品系统异常处理源码分享
[pytoch] quantification
pip uninstall all packages except builtin package
日常知识科普
greendao使用问题
4个不可不知的“安全左移”的理由
[learn ZABBIX from scratch] I. Introduction and deployment of ZABBIX
postgresql 之 ilist
二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]
百度地图API绘制点及提示信息
Go语言三个高效编程的技巧
R语言plotly可视化:可视化模型在整个数据空间的分类轮廓线(等高线)、meshgrid创建一个网格,其中每个点之间的距离由mesh_size变量表示、使用不同的形状标签表征、训练、测试及分类标签
[environment setup] zip volume compression
![根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]](/img/f7/8d026c0e4435fc8fd7a63616b4554d.png)








