'Ansible shell command doesn't seem to have access to the user's shell environment

Im trying to use a ruby installed via RBENV. I have confirmed that RBENV works as desired and produces the correct ruby version which can be called via the terminal

ruby -v returns: ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-linux] which is the desired version

bundler -v returns: Bundler version 2.3.10 which is also the desired version.

However the following tasks in my playbook fail

- block:

  - name: 'set ruby version'
    shell: echo eval "$(rbenv init -)" >> ~/.zshrc

  - name: 'set global ruby verison'
    shell: 'rbenv global {{ ruby_version }}'

  - name: 'restart shell'
    shell: $shell

  - name: 'Bundle install'
    shell:
      chdir: "{{ app_path }}"
      cmd: bundle install

  become: yes
  become_user: '{{ deploy_user}}'

I get the following output when trying to execute these tasks in my playbook

fatal: [45.77.186.234]: FAILED! => {"changed": true, "cmd": "bundle install", "delta": "0:00:00.001967", "end": "2022-03-25 00:33:48.231712", "msg": "non-zero return code", "rc": 127, "start": "2022-03-25 00:33:48.229745", "stderr": "/bin/sh: 1: bundle: not found", "stderr_lines": ["/bin/sh: 1: bundle: not found"], "stdout": "", "stdout_lines": []}

On the docs page for ansible.builtin.shell the 3rd bullet point under synopsis indicates that the shell command is executed using the user's shell environment. I can't see anywhere that I'm supposed to define which shell specifically, I wonder if my use of ZSH is where the problem is?

At this point I've tried everything I can think of, and now Im asking for help. How do I run a shell command against the user's shell environment?



Solution 1:[1]

I ended up having to execute chained zsh commands to get this to work for me.

- name: Install fastlane gem
  command: zsh -lc "source ~/.zshrc && gem install fastlane"

I didn't have to do this before I updated my MacOS system to 12.3.1, for what it's worth. It seems like some default behavior changed for shell execution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1