无法使用 Ansible 获取 .bashrc [英] Not possible to source .bashrc with Ansible
问题描述
我可以通过 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(ansible 默认值).源"被称为."在/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屋!