简体   繁体   English

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

[英]can not source ~/.bashrc file with ansible

I have a list of aliases in a file, .bash_aliases , which is being copied to remote servers with ansible playbook . 我在.bash_aliases文件中有一个别名列表,该文件将通过.bash_aliases 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. 该文件已被复制到目标位置,但使用以下ansible任务未加载.bashrc (反过来加载.bash_aliases )文件。

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 使用命令模块-名称:.bashrc文件的源命令:源〜/ .bashrc

Nothing works!!! 没用!!! Any help 任何帮助

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详细信息进行google搜索时弹出的另一篇文章... 无法通过Ansible来获取.bashrc (感谢@chucksmash提供链接)

"Steve Midgley “史蒂夫·米格利

You have two options to use source with ansible. 您可以通过两种方式使用带ansible的源。 One is with the "shell:" command and /bin/sh (the ansible default). 一种是使用“ shell:”命令和/ bin / sh(默认为ans)。 "source" is called "." “来源”称为“。” in /bin/sh. 在/ bin / sh中。 So your command would be: 因此,您的命令将是:

-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”的源代码,这样可以更完全地模拟本地登录。”

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; 如果您希望在一个Bash实例中设置的变量在其他Bash实例或运行该Bash脚本的Ansible实例中可见,则无法执行此操作; this is inherent to the Unix process model. 这是Unix过程模型所固有的。

What you can do 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. 这可能会非常复杂,但是如果您需要执行的任务可能需要比最简单的调试更多的工作,那么最好创建一个单独的外部脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM