当前位置:网站首页>[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 .
边栏推荐
- The "little giant" specialized in special new products is restarted, and the "enterprise cloud" digital empowerment
- Unity 热力图建立方法
- 六月集训(第23天) —— 字典树
- 不要小看了积分商城,它的作用可以很大
- Baidu map API drawing points and tips
- 专精特新“小巨人”再启动,“企业上云”数字赋能
- 在CVS中恢复到早期版本
- Detailed explanation of redis data types
- Py之toad:toad的简介、安装、使用方法之详细攻略
- PgSQL queries the largest or smallest data of a field in a group
猜你喜欢

文本对比学习综述

Overview of SAP marketing cloud functions (III)

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

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

Unit contour creation method

`Thymeleaf ` template engine comprehensive analysis

从谭浩强《C程序设计》上摘录的ASCII码表(常用字符与ASCII代码对照表)
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

如何避免下重复订单

PgSQL queries the largest or smallest data of a field in a group
随机推荐
SSH keygen configuration does not require entering a password every time
Some basic database operations (providing the original database information)
v-if 和 v-show 的区别
初识云原生安全:云时代的最佳保障
The "little giant" specialized in special new products is restarted, and the "enterprise cloud" digital empowerment
【无标题】
智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理
ASCII code table extracted from tanhaoqiang's C program design (comparison table of common characters and ASCII codes)
MES在流程和离散制造企业的15个差别(下)
Overview of SAP marketing cloud functions (III)
Puzzle (016.2) finger painting Galaxy
Grendao usage problems
厨卫电器行业B2B交易协同管理平台开发,优化企业库存结构
puzzle(016.2)指画星河
六月集训(第24天) —— 线段树
Solution of channel management system for food and beverage industry: realize channel digital marketing layout
SAP Marketing Cloud 功能概述(四)
laravel下视图间共享数据
Win10 system problems
postgresql 之 ilist