Add binaries to PATH with Ansible

自作多情 提交于 2019-12-02 04:23:45

问题


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?


回答1:


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
    


来源:https://stackoverflow.com/questions/41884288/add-binaries-to-path-with-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!