2

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?

Razvan El
  • 177
  • 3
  • 12

1 Answers1

2

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
    
techraf
  • 64,883
  • 27
  • 193
  • 198
  • I did have deployer in my playbook. The typo was just in the stackoverflow sample. Regarding the ~/.bashrc not being read, I did suspect there was something wrong about it. Where should I put `[[ -s "$HOME/.kiex/scripts/kiex" ]] && source "$HOME/.kiex/scripts/kiex"'` ? – Razvan El Jan 27 '17 at 09:19
  • I don't know. It depends on the os and distribution. And I advised to use the full path. – techraf Jan 27 '17 at 09:28
  • I use Ubuntu Xenial. – Razvan El Jan 27 '17 at 09:44
  • 1
    I repeat: for what you have shown in the question use the full path. I have no idea what you are planning to do with the Elixir or how you are planning to use it. – techraf Jan 27 '17 at 09:47