我可以ssh到远程主机并执行source /home/username/.bashrc
-一切正常。但是,如果我这样做了:
- 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
我不知道我做错了什么。
发布于 2014-12-18 16:11:35
在ansible中使用source有两种选择。一种是使用"shell:“命令和/bin/sh ( ansible默认设置)。"source“被称为".”在/bin/sh中。所以你的命令应该是:
- 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“命令:
- name: source bashrc
sudo: no
shell: source /home/username/.bashrc && [the actual command you want run]
args:
executable: /bin/bash
最后,我要指出的是,如果您使用的是Ubuntu或类似的系统,那么您可能希望实际使用"/etc/profile“,它更完全地模拟了本地登录。
发布于 2014-03-08 05:36:32
所以command
将只运行可执行文件。source
本身不是一个可执行文件。(这是一个内置的shell命令)。有什么理由要source
一个完整的环境变量吗?
在Ansible中包含环境变量还有其他方法。例如,environment
指令:
- name: My Great Playbook
hosts: all
tasks:
- name: Run my command
sudo: no
action: command <your-command>
environment:
HOME: /home/myhome
另一种方法是使用shell
Ansible模块:
- name: source bashrc
sudo: no
action: shell source /home/username/.bashrc && <your-command>
或
- name: source bashrc
sudo: no
shell: source /home/username/.bashrc && <your-command>
在这些情况下,一旦运行Ansible步骤,shell实例/环境就会终止。
发布于 2015-08-16 07:23:25
我知道这个答案来得太晚了,但我已经看过足够多的代码,你可以使用sudo选项-i
,如下所示:
- name: source bashrc
shell: sudo -iu {{ansible_user_id}} [the actual command you want run]
正如文档中所说的
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.
https://stackoverflow.com/questions/22256884
复制相似问题