使用Ansible将二进制文件添加到PATH [英] Add binaries to PATH with Ansible
问题描述
I'm trying to install the Kiex Version manager for the Elixir programming language using Ansible.
这些是我为此使用的戏剧:
These are the plays I use for this:
- name: Kiex Installation
hosts: web
gather_facts: false
remote_user: deployer
tasks:
- shell: \curl -sSL https://raw.githubusercontent.com/taylor/kiex/master/install | bash -s
- name: Add Kiex Bin to Path
lineinfile:
dest: /home/deployer/.bashrc
regexp: '^test -s'
line: '[[ -s "$HOME/.kiex/scripts/kiex" ]] && source "$HOME/.kiex/scripts/kiex"'
- name: Reload Path
shell: source /home/deployer/.bashrc
args:
executable: /bin/bash
- shell: echo $PATH
register: pathul
- debug:
var: pathul
- name: Elixir Installation
hosts: web
gather_facts: false
remote_user: deployer
tasks:
- shell: echo $PATH
register: pathul
- debug:
var: pathul
- name: Install Elixir Version
command: /home/deployer/.kiex/bin/kiex list
args:
executable: /bin/bash
chdir: /home/deployer/
- name: Set Elixir Version as Default
shell: kiex default 1.4
Kiex的安装成功,如果我登录到远程计算机,只需使用kiex
命令就可以运行它.之所以可以这样做,是因为我的二进制文件来自〜/.kiex/scripts/kiex".当我回显$PATH
变量时,它会在其中显示kiex二进制路径/home/deployer/.kiex/bin
:
The Installation of Kiex is a success and if I log in to the remote Machine I am able to run it simply by using the kiex
command. I can do this because I sourced the binaries in "~/.kiex/scripts/kiex". When I echo the $PATH
variable it shows the kiex binaries path /home/deployer/.kiex/bin
in it:
$ echo $PATH
/home/deployer/.kiex/bin:/home/deployer/.kiex/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
但是在上面显示的 Elixir安装中的kiex
,kiex list
甚至/home/deoployer/.kiex/bin/kiex list
都失败,并显示以下消息:
However the kiex
, kiex list
and even the /home/deoployer/.kiex/bin/kiex list
in the Elixir Installation Play shown above fail with the message:
TASK [Set Elixir Version as Default] *******************************************
fatal: [local-web-2]: FAILED! => {"changed": true, "cmd": "kiex default 1.4", "delta": "0:00:00.002042", "end": "2017-01-26 22:13:32.898082", "failed": true, "rc": 127, "start": "2017-01-26 22:13:32.896040", "stderr": "/bin/sh: 1: kiex: not found", "stdout": "", "stdout_lines": [], "warnings": []}
另外,通过ansible注册通过回声路径结果的pathul
变量也不包含/home/deployer/.kiex/bin
:
Also the pathul
variable that registered the result of echoing the path via ansible doesn't contain /home/deployer/.kiex/bin
:
"stdout": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
如何通过Ansible使kiex
命令正常工作?
How can I make the kiex
command work properly via Ansible?
推荐答案
只需使用完整的绝对路径,就像您在Install Elixir Version
任务中尝试过的一样,但请注意,在示例中和在您发布的说明:
Just use the full, absolute path, like you tried in the Install Elixir Version
task, but mind that you have a typo, both, in the example and in the explanation you posted:
command: /home/deoployer/.kiex/bin/kiex list
[]甚至/home/deoployer/.kiex/bin/kiex list
[]失败[s]
[ ] even the /home/deoployer/.kiex/bin/kiex list
[ ] fail[s]
应该像第一次播放一样是deployer
,而不是deoployer
.
It should likely be deployer
, like in the first play, not deoployer
.
如果您提供正确的路径,则没有其他原因使Ansible失败并显示" kiex:not found "消息.
There is no reason otherwise for Ansible to fail with "kiex: not found" message, if you provide the correct path.
关于其他任务的说明:
-
引用
man bash
:
启动不是登录shell的交互式shell时,bash从
~/.bashrc
读取并执行命令(如果该文件存在).
When an interactive shell that is not a login shell is started, bash reads and executes commands from
~/.bashrc
, if that file exists.
因此,当您使用Ansible执行任务时,甚至不会读取您的~/.bashrc
,因为它不是交互式会话.
So your ~/.bashrc
is not even read when you execute tasks with Ansible, because it's not an interactive session.
例如,这就是为什么您的pathul
变量不包含在~/.bashrc
中应用的更改的原因.
This is for example why your pathul
variable does not contain changes applied in the ~/.bashrc
.
以下两个任务运行单独的bash进程.第一项任务中的环境对第二项任务的环境没有影响:
The following two tasks run separate bash processes. The environment sourced in the first task has no influence on the environment of the second:
- name: Reload Path
shell: source /home/deployer/.bashrc
args:
executable: /bin/bash
- shell: echo $PATH
register: pathul
这篇关于使用Ansible将二进制文件添加到PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!