我可以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
我不知道我做错了什么......
答案 0 :(得分:74)
您有两个选项可以使用ansible源代码。一个是“shell:”命令和/ bin / sh(ansible默认值)。 “来源”被称为“。”在/ 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”,这样可以更完整地模拟本地登录。
答案 1 :(得分:17)
所以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实例/环境将终止。
答案 2 :(得分:3)
我在尝试让virtualenvwrapper在Ubuntu服务器上运行时遇到了同样的问题。我这样使用Ansible:
- name: Make virtual environment
shell: source /home/username/.bashrc && makevirtualenv virenvname
args:
executable: /bin/bash
但源命令无效。
最终我发现.bashrc文件在文件顶部有几行,在Ansible调用时阻止源工作:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
我在.bashrc中注释掉了这些行,之后一切都按预期运行。
答案 3 :(得分:1)
我尝试了列出的答案,但是在通过rbenv安装ruby时,这些对我没有用。我必须从/root/.bash_profile
PATH=$PATH:$HOME/bin:$HOME/.rbenv/bin:$HOME/.rbenv/plugins/ruby-build/bin
export PATH
eval "$(rbenv init -)"
最后,我提出了这个
- shell: sudo su - root -c 'rbenv install -v {{ ruby_version }}'
可以在任何命令中使用它。
- shell: sudo su - root -c 'your command'
答案 4 :(得分:0)
我发现这是最佳解决方案:
- name: Source .bashrc
shell: . .bashrc
become: true
您可以通过添加(默认:root)来更改用户:
- name: Source .bashrc
shell: . .bashrc
become: true
become-user: {your_remote_user}
此处有更多信息:Ansible become
答案 5 :(得分:0)
我在 ansible 2.4.1.0 中尝试了上面的所有选项,但直到另外两个都没有人起作用,以下是重现此情况的详细信息。
$ cat ~/.bash_aliases
alias ta="echo 'this is test for ansible interactive shell'";
这是可测试:
- name: Check the basic string operations
hosts: 127.0.0.1
connection: local
tasks:
- name: Test Interactive Bash Failure
shell: ta
ignore_errors: True
- name: Test Interactive Bash Using Source
shell: source ~/.bash_aliases && ta
args:
executable: /bin/bash
ignore_errors: yes
- name: Test Interactive Bash Using .
shell: . ~/.bash_aliases && ta
ignore_errors: yes
- name: Test Interactive Bash Using /bin/bash -ci
shell: /bin/bash -ic 'ta'
register: result
ignore_errors: yes
- debug: msg="{{ result }}"
- name: Test Interactive Bash Using sudo -ui
shell: sudo -ui hearen ta
register: result
ignore_errors: yes
- name: Test Interactive Bash Using ssh -tt localhost /bin/bash -ci
shell: ssh -tt localhost /bin/bash -ci 'ta'
register: result
ignore_errors: yes
这是结果:
$ ansible-playbook testInteractiveBash.yml
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [Check the basic string operations] ************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************
ok: [127.0.0.1]
TASK [Test Interactive Bash Failure] ****************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "ta", "delta": "0:00:00.001341", "end": "2018-10-31 10:11:39.485897", "failed": true, "msg": "non-zero return code", "rc": 127, "start": "2018-10-31 10:11:39.484556", "stderr": "/bin/sh: 1: ta: not found", "stderr_lines": ["/bin/sh: 1: ta: not found"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [Test Interactive Bash Using Source] ***********************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "source ~/.bash_aliases && ta", "delta": "0:00:00.002769", "end": "2018-10-31 10:11:39.588352", "failed": true, "msg": "non-zero return code", "rc": 127, "start": "2018-10-31 10:11:39.585583", "stderr": "/bin/bash: ta: command not found", "stderr_lines": ["/bin/bash: ta: command not found"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [Test Interactive Bash Using .] ****************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": ". ~/.bash_aliases && ta", "delta": "0:00:00.001425", "end": "2018-10-31 10:11:39.682609", "failed": true, "msg": "non-zero return code", "rc": 127, "start": "2018-10-31 10:11:39.681184", "stderr": "/bin/sh: 1: ta: not found", "stderr_lines": ["/bin/sh: 1: ta: not found"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [Test Interactive Bash Using /bin/bash -ci] ****************************************************************************************************************************************
changed: [127.0.0.1]
TASK [debug] ****************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
"msg": {
"changed": true,
"cmd": "/bin/bash -ic 'ta'",
"delta": "0:00:00.414534",
"end": "2018-10-31 10:11:40.189365",
"failed": false,
"rc": 0,
"start": "2018-10-31 10:11:39.774831",
"stderr": "",
"stderr_lines": [],
"stdout": "this is test for ansible interactive shell",
"stdout_lines": [
"this is test for ansible interactive shell"
]
}
}
TASK [Test Interactive Bash Using sudo -ui] *********************************************************************************************************************************************
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "sudo -ui hearen ta", "delta": "0:00:00.007906", "end": "2018-10-31 10:11:40.306128", "failed": true, "msg": "non-zero return code", "rc": 1, "start": "2018-10-31 10:11:40.298222", "stderr": "sudo: unknown user: i\nsudo: unable to initialize policy plugin", "stderr_lines": ["sudo: unknown user: i", "sudo: unable to initialize policy plugin"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [Test Interactive Bash Using ssh -tt localhost /bin/bash -ci] **********************************************************************************************************************
hearen@localhost's password:
changed: [127.0.0.1]
PLAY RECAP ******************************************************************************************************************************************************************************
127.0.0.1 : ok=8 changed=6 unreachable=0 failed=0
有两个可行的选择:
shell: /bin/bash -ic 'ta'
shell: ssh -tt localhost /bin/bash -ci 'ta'
,但这一项需要在本地输入密码。答案 6 :(得分:0)
我花了2美分,将问题~/.nvm/nvm.sh
引入~/.profile
,然后按照另一个答案中的建议使用sudo -iu
。
相对于Ubuntu 16.04.5于2018年1月试过
- name: Installing Nvm
shell: >
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
args:
creates: "/home/{{ ansible_user }}/.nvm/nvm.sh"
tags:
- nodejs
- name: Source nvm in ~/.profile
sudo: yes
sudo_user: "{{ ansible_user }}"
lineinfile: >
dest=~/.profile
line="source ~/.nvm/nvm.sh"
create=yes
tags:
- nodejs
register: output
- name: Installing node
command: sudo -iu {{ ansible_user }} nvm install --lts
args:
executable: /bin/bash
tags:
- nodejs
答案 7 :(得分:0)
许多回复都建议使用〜/ .bashrc源,但是主要问题是ansible shell不是交互式的,并且〜/ .bashrc实现默认情况下会忽略非交互式shell(检查其开始位置)。
我发现以ssh交互式登录后以用户身份执行命令的最佳解决方案是:
- hosts: all
tasks:
- name: source user profile file
#become: yes
#become_user: my_user # in case you want to become different user (make sure acl package is installed)
shell: bash -ilc 'which python' # example command which prints
register: which_python
- debug:
var: which_python
bash:“-i”表示交互式shell,因此.bashrc不会被忽略 “ -l”表示可获取完整用户个人资料的登录shell
答案 8 :(得分:-2)
正确的方法应该是:
- hosts: all
tasks:
- name: source bashrc file
shell: "{{ item }}"
with_items:
- source ~/.bashrc
- your other command
注意:它在ansible 2.0.2
版本中的测试