bash 无法使用 ansible 获取 ~/.bashrc 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34133810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:00:48  来源:igfitidea点击:

can not source ~/.bashrc file with ansible

bashansible

提问by Ajeet Khan

I have a list of aliases in a file, .bash_aliases, which is being copied to remote servers with ansible playbook. The file is getting copied to the destination but the .bashrc(which in turns load the .bash_aliases) file is not getting loaded using the following ansible task.

我在文件 中有一个别名列表,该文件.bash_aliases正被复制到带有ansible playbook. 文件正在被复制到目标,但.bashrc(反过来加载.bash_aliases)文件没有使用以下 ansible 任务加载。

I have tried giving the executable argument

我试过给出可执行参数

  - name: source the .bashrc file
    shell: source ~/.bashrc
    args:
      executables: "/bin/bash"

Without argument

没有争论

  - name: source the .bashrc file
    shell: source ~/.bashrc

With raw module

带原始模块

  - name: source the .bashrc file
    raw: source ~/.bashrc

With command module - name: source the .bashrc file command: source ~/.bashrc

使用命令模块 - 名称:source .bashrc 文件命令:source ~/.bashrc

Nothing works!!! Any help

什么都没用!!!任何帮助

回答by Azifor

From reading your comments, you stated that you are trying to make permanate aliases and not for a particular session. Why not create those aliases inside of /etc/profile.d on the machines you need to have those particular aliases on instead of by user?

通过阅读您的评论,您表示您正在尝试创建永久别名而不是特定会话。为什么不在您需要使用这些特定别名的机器上的 /etc/profile.d 中创建这些别名,而不是由用户创建?

Also, from another post that popped up when I ran a google search on Ansible specifics as I am no Ansible expert... Not possible to source .bashrc with Ansible(thanks to @chucksmash for the link)

另外,当我在谷歌搜索 Ansible 细节时弹出的另一篇文章,因为我不是 Ansible 专家......无法使用 Ansible 获取 .bashrc(感谢@chucksmash 提供链接)

"Steve Midgley

“史蒂夫·米奇利

You have two options to use source with ansible. One is with the "shell:" command and /bin/sh (the ansible default). "source" is called "." in /bin/sh. So your command would be:

您有两种选择可以将 source 与 ansible 结合使用。一种是使用“shell:”命令和 /bin/sh(ansible 默认值)。“源”被称为“。” 在 /bin/sh 中。所以你的命令是:

-name: source bashrc
sudo: no
shell: . /home/username/.bashrc && [the actual command you want run]

Note you have to run a command after sourcing .bashrc b/c each ssh session is distinct - every ansible command runs in a separate ssh transaction.

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

Your second option is to force Ansible shell to use bash and then you can use the "source" command:\

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

name: source bashrc
sudo: no   
shell: source /home/username/.bashrc && [the actual command you want run]
args:
  executable: /bin/bash

Finally, I'll note that you may want to actually source "/etc/profile" if you're on Ubuntu or similar, which more completely simulates a local login."

最后,我会注意到,如果您使用的是 Ubuntu 或类似系统,您可能希望实际获取“/etc/profile”的来源,这更完全地模拟了本地登录。”

回答by tripleee

If you expect the variables you set in one Bash instance to be visible in other Bash instances, or in the Ansible instance which ran this Bash script, there is no way to do that; this is inherent to the Unix process model.

如果您希望在一个 Bash 实例中设置的变量在其他 Bash 实例中可见,或者在运行此 Bash 脚本的 Ansible 实例中可见,则没有办法做到这一点;这是 Unix 进程模型所固有的。

What you cando is set a variable and then run whichever tool needs for this value to be set.

可以做的是设置一个变量,然后运行任何需要设置该值的工具。

bash -c 'var="value" /path/to/other/tool'

This can be arbitrarily complex, though you're probably better off creating a separate external script if the task you need to perform could require anything more than the most trivial debugging.

这可以是任意复杂的,但如果您需要执行的任务可能需要比最琐碎的调试更多的任何东西,您最好创建一个单独的外部脚本。