首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用Ansible提供.bashrc源代码

无法使用Ansible提供.bashrc源代码
EN

Stack Overflow用户
提问于 2014-03-08 01:34:50
回答 8查看 74K关注 0票数 88

我可以ssh到远程主机并执行source /home/username/.bashrc -一切正常。但是,如果我这样做了:

代码语言:javascript
运行
复制
- name: source bashrc
  sudo: no
  action: command source /home/username/.bashrc

我得到了:

代码语言:javascript
运行
复制
failed: [hostname] => {"cmd": ["source", "/home/username/.bashrc"], "failed": true, "rc": 2}
msg: [Errno 2] No such file or directory

我不知道我做错了什么。

EN

回答 8

Stack Overflow用户

发布于 2014-12-18 16:11:35

在ansible中使用source有两种选择。一种是使用"shell:“命令和/bin/sh ( ansible默认设置)。"source“被称为".”在/bin/sh中。所以你的命令应该是:

代码语言:javascript
运行
复制
- name: source bashrc
  sudo: no   
  shell: . /home/username/.bashrc && [the actual command you want run]

注您必须在获取.bashrc b/c之后运行命令每个ssh会话都是不同的-每个ansible命令都在单独的ssh事务中运行。

您的第二个选择是强制Ansible shell使用bash,然后您可以使用"source“命令:

代码语言:javascript
运行
复制
- name: source bashrc
  sudo: no   
  shell: source /home/username/.bashrc && [the actual command you want run]
  args:
     executable: /bin/bash

最后,我要指出的是,如果您使用的是Ubuntu或类似的系统,那么您可能希望实际使用"/etc/profile“,它更完全地模拟了本地登录。

票数 93
EN

Stack Overflow用户

发布于 2014-03-08 05:36:32

所以command将只运行可执行文件。source本身不是一个可执行文件。(这是一个内置的shell命令)。有什么理由要source一个完整的环境变量吗?

在Ansible中包含环境变量还有其他方法。例如,environment指令:

代码语言:javascript
运行
复制
- name: My Great Playbook
  hosts: all
  tasks:
    - name: Run my command
      sudo: no
      action: command <your-command>
      environment:
          HOME: /home/myhome

另一种方法是使用shell Ansible模块:

代码语言:javascript
运行
复制
- name: source bashrc
  sudo: no
  action: shell source /home/username/.bashrc && <your-command>

代码语言:javascript
运行
复制
- name: source bashrc
  sudo: no   
  shell: source /home/username/.bashrc && <your-command>

在这些情况下,一旦运行Ansible步骤,shell实例/环境就会终止。

票数 25
EN

Stack Overflow用户

发布于 2015-08-16 07:23:25

我知道这个答案来得太晚了,但我已经看过足够多的代码,你可以使用sudo选项-i,如下所示:

代码语言:javascript
运行
复制
- name: source bashrc
  shell: sudo -iu {{ansible_user_id}} [the actual command you want run]

正如文档中所说的

代码语言:javascript
运行
复制
The -i (simulate initial login) option runs the shell specified by the password database entry of the target user as a login shell.  This means that login-specific
               resource files such as .profile or .login will be read by the shell.  If a command is specified, it is passed to the shell for execution via the shell's -c option.
               If no command is specified, an interactive shell is executed.  sudo attempts to change to that user's home directory before running the shell.  It also initializes
               the environment to a minimal set of variables, similar to what is present when a user logs in.  The Command environment section below documents in detail how the -i
               option affects the environment in which a command is run.
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22256884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档