当前位置:网站首页>[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 .
边栏推荐
- IList of PostgreSQL
- Jupiter notebook operation
- 根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]
- Jerry's test mic energy automatic recording automatic playback reference [article]
- Daily knowledge popularization
- Overview of SAP marketing cloud functions (IV)
- 4个不可不知的“安全左移”的理由
- SaaS management system solution of smart Park: enabling the park to realize information and digital management
- 10_那些格调很高的个性签名
- 【Pytorch】量化
猜你喜欢
![Maximum path sum in binary tree [handle any subtree, then handle the whole tree]](/img/d0/91ab1cc1851d7137a1cab3cf458302.png)
Maximum path sum in binary tree [handle any subtree, then handle the whole tree]

日常知识科普

Development of digital Tibetan product system NFT digital Tibetan product system exception handling source code sharing

智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

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

Go语言三个高效编程的技巧

In the eyes of the universe, how to correctly care about counting East and West?

Research on MySQL composite index

Method of inputting dots under letters in markdown/latex
随机推荐
[leetcode] 10. Regular expression matching
Maximum path sum in binary tree [handle any subtree, then handle the whole tree]
【无标题】
GO语言-goroutine协程的使用
Rongyun communication has "hacked" into the heart of the bank
[learn ZABBIX from scratch] I. Introduction and deployment of ZABBIX
10_那些格調很高的個性簽名
Kotlin coordination channel
IDEA连接mysql自定义生成实体类代码
OpenHarmony 1
MySQL log management, backup and recovery
常见的单例模式&简单工厂
数商云:加强供应商管理,助推航空运输企业与供应商高效协同
tongweb使用之端口冲突处理办法
How to solve the problem that iterative semi supervised training is difficult to implement in ASR training? RTC dev Meetup
Overview of SAP marketing cloud functions (III)
在宇宙的眼眸下,如何正确地关心东数西算?
MES在流程和离散制造企业的15个差别(下)
Telecommuting: camping at home office gadgets | community essay solicitation
GO语言-init()函数-包初始化