无法使用Ansible来源.bashrc [英] Not possible to source .bashrc with Ansible

查看:220
本文介绍了无法使用Ansible来源.bashrc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can ssh to the remote host and do a source /home/username/.bashrc - everything works fine. However if I do:

- name: source bashrc
  sudo: no
  action: command source /home/username/.bashrc

我得到:

failed: [hostname] => {"cmd": ["source", "/home/username/.bashrc"], "failed": true, "rc": 2}
msg: [Errno 2] No such file or directory

我不知道我在做什么错...

I have no idea what I'm doing wrong...

推荐答案

您可以通过两种选择将ansible与source一起使用.一种是使用"shell:"命令和/bin/sh(默认为ans). 源"称为".在/bin/sh中.因此,您的命令将是:

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:

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

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

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.

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

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

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

最后,我要指出的是,如果您使用的是Ubuntu或类似版本,则可能希望实际获取"/etc/profile"的源代码.

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.

这篇关于无法使用Ansible来源.bashrc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆