使用 Ansible 将二进制文件添加到 PATH [英] Add binaries to PATH with Ansible

查看:50
本文介绍了使用 Ansible 将二进制文件添加到 PATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试安装 Kiex 版本管理器 Elixir 使用 Ansible 的编程语言.

这些是我使用的剧本:

- 名称:Kiex 安装主持人:网络收集事实:假remote_user:部署者任务:- 外壳:\curl -sSL https://raw.githubusercontent.com/taylor/kiex/master/install |bash -s- 名称:将 Kiex Bin 添加到路径行文件:目标:/home/deployer/.bashrc正则表达式:'^test -s'行:'[[ -s "$HOME/.kiex/scripts/kiex" ]] &&源$HOME/.kiex/scripts/kiex"'- 名称:重新加载路径外壳:源/home/deployer/.bashrc参数:可执行文件:/bin/bash- 外壳:回声 $PATH注册:pathul- 调试:变量:路径- 名称:Elixir 安装主持人:网络收集事实:假remote_user:部署者任务:- 外壳:回声 $PATH注册:pathul- 调试:变量:路径- 名称:安装 Elixir 版本命令:/home/deployer/.kiex/bin/kiex list参数:可执行文件:/bin/bashchdir:/home/deployer/- 名称:将 Elixir 版本设置为默认值外壳:kiex 默认 1.4

Kiex 安装成功,如果我登录到远程机器,我只需使用 kiex 命令即可运行它.我可以这样做,因为我在~/.kiex/scripts/kiex"中获取了二进制文件.当我回显 $PATH 变量时,它会显示其中的 kiex 二进制文件路径 /home/deployer/.kiex/bin:

$ 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

但是中的kiexkiex list甚至/home/deoployer/.kiex/bin/kiex listElixir 安装 上面显示的播放失败并显示以下消息:

TASK [设置 Elixir 版本为默认] *******************************************致命:[local-web-2]:失败!=>{"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":[],警告":[]}

还有pathul 变量注册了通过ansible 回显路径的结果不包含/home/deployer/.kiex/bin:

"stdout": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/游戏"

如何通过 Ansible 使 kiex 命令正常工作?

解决方案

只需使用完整的绝对路径,就像您在 Install Elixir Version 任务中尝试的那样,但请注意您有一个错字,在示例和您发布的解释中:

<块引用>

command:/home/deoployer/.kiex/bin/kiex list

[ ] 甚至 /home/deoployer/.kiex/bin/kiex 列表 [ ] 失败[s]

应该是deployer,就像第一部一样,而不是deoployer.

如果您提供正确的路径,Ansible 没有其他原因会失败并显示kiex: not found"消息.

<小时>

关于其他任务的说明:

  • 引用man bash:

    <块引用>

    当一个不是登录 shell 的交互式 shell 启动时,bash 从 ~/.bashrc 读取并执行命令,如果该文件存在.

    所以当您使用 Ansible 执行任务时,您的 ~/.bashrc 甚至不会被读取,因为它不是一个交互式会话.

    例如,这就是为什么您的 pathul 变量不包含在 ~/.bashrc 中应用的更改的原因.

  • 以下两个任务运行单独的 bash 进程.第一个任务中的环境对第二个任务的环境没有影响:

    - 名称:重新加载路径外壳:源/home/deployer/.bashrc参数:可执行文件:/bin/bash- 外壳:回声 $PATH注册:pathul

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

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

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": []}

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"

How can I make the kiex command work properly via Ansible?

解决方案

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

[ ] even the /home/deoployer/.kiex/bin/kiex list [ ] fail[s]

It should likely be deployer, like in the first play, not deoployer.

There is no reason otherwise for Ansible to fail with "kiex: not found" message, if you provide the correct path.


Explanations regarding other tasks:

  • Quoting man bash:

    When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists.

    So your ~/.bashrc is not even read when you execute tasks with Ansible, because it's not an interactive session.

    This is for example why your pathul variable does not contain changes applied in the ~/.bashrc.

  • 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屋!

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