当前位置:网站首页>[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 .
边栏推荐
- 融云通信“三板斧”,“砍”到了银行的心坎上
- postgresql 之 ilist
- 在宇宙的眼眸下,如何正确地关心东数西算?
- Online text entity extraction capability helps applications analyze massive text data
- Jupyter notebook操作
- Kunpeng arm server compilation and installation paddlepaddle
- 【Pytorch】量化
- Database considerations
- [environment setup] zip volume compression
- Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire
猜你喜欢

unity 等高线创建方法

食品饮料行业渠道商管理系统解决方案:实现渠道数字化营销布局

从谭浩强《C程序设计》上摘录的ASCII码表(常用字符与ASCII代码对照表)

AntD checkbox,限制选中数量

Telecommuting: camping at home office gadgets | community essay solicitation

How to avoid placing duplicate orders

遠程辦公之:在家露營辦公小工具| 社區征文

Py之toad:toad的简介、安装、使用方法之详细攻略

Qunhui synchronizes with alicloud OSS

初识云原生安全:云时代的最佳保障
随机推荐
09_一种比较高效的记忆方法
一文搞定 UDP 和 TCP 高频面试题!
Explore cloud native databases and take a broad view of future technological development
4 reasons for "safe left shift"
取消冒泡
Jerrys timer0 uses the default pa13 to detect the pulse width [chapter]
根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]
简谈企业Power BI CI /CD 实施框架
MES在流程和离散制造企业的15个差别(下)
pgsql查询分组中某个字段最大或者最小的一条数据
Digital business cloud: strengthen supplier management and promote efficient collaboration between air transport enterprises and suppliers
Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]
CONDA and pip commands
P2PDB 白皮书
六月集训(第23天) —— 字典树
v-for 中 key的作用和原理
OpenHarmony 1
[pytoch] quantification
leetcode 139. Word Break 单词拆分(中等)
10 Ces autographes très stylisés.