An unusual piece to execute commands from a custom shell

I am trying to run a relatively simple Ansible playbook to provide an Ubuntu VM through Vagrant. About three steps:

  • Updating and installing the necessary packages
  • Install rbenv in the specified user home.
  • Install the specified version of Ruby via rbenv for the specified user

The first two steps are good, but the third I fight. Using other slot machines as reference (one for rbenv and nvm ) I created the following base book:

--- - hosts: all vars: user: joe_bloggs ruby_version: 2.1.5 tasks: # # System # - name: Update apt cache sudo: yes apt: update_cache=yes cache_valid_time=86400 - name: Upgrade existing system packages sudo: yes apt: upgrade=dist - name: Install essential system packages sudo: yes apt: name={{ item }} state=latest with_items: - git - curl - openssl - build-essential - name: Add user sudo: yes user: name={{user}} shell=/bin/bash groups=sudo # # rbenv # - name: Install rbenv | Clone repo sudo: yes sudo_user: "{{ user }}" git: repo=https://github.com/sstephenson/rbenv.git dest=~/.rbenv accept_hostkey=yes update=yes - name: Install rbenv | Create plugins directory sudo: yes sudo_user: "{{ user }}" file: path=~/.rbenv/plugins/ mode=0755 state=directory - name: Install rbenv | Install ruby-build plugin sudo: yes sudo_user: "{{ user }}" git: repo=git://github.com/sstephenson/ruby-build.git dest=~/.rbenv/plugins/ruby-build accept_hostkey=yes - name: Install rbenv | Add path to profile sudo: yes sudo_user: "{{ user }}" lineinfile: line='export PATH="$HOME/.rbenv/bin:$PATH"' regexp="\$HOME\/\.rbenv\/bin:\$PATH" dest=~/.bashrc - name: Install rbenv | Enable shims in profile sudo: yes sudo_user: "{{ user }}" lineinfile: line='eval "$(rbenv init -)"' regexp="rbenv init \-" dest=~/.bashrc - name: Install Ruby | Install version sudo: yes sudo_user: "{{ user }}" shell: rbenv install {{ ruby_version }} executable=/bin/bash - name: Install Ruby | Set default version and rehash sudo: yes sudo_user: "{{ user }}" shell: rbenv global {{ ruby_version }} && rbenv rehash executable=/bin/bash 

Launch of the above play. I encountered an error:

 stderr: /bin/bash: rbenv: command not found 

Is there an elegant way to run a command through Ansible as a user using loaded shell and environment variables?

I tried to add the -i flag to sudo and commands like sudo -iu {{user}} rbenv install ... but I still failed.

I can start the Ruby installation by specifying the full path to the rbenv , but this method is not always suitable (some tools may not provide a single executable).

+5
source share
2 answers

If you are using indispensable 1.4 or higher, you can use the remote_user parameter to do this. Both at the player level and at the task level.

 --- - hosts: webservers remote_user: root tasks: - name: test connection ping: remote_user: yourname 

Refer - http://docs.ansible.com/playbooks_intro.html

0
source

I had the same problem when trying to install ruby-2.2.3 using rbenv using Ansible 2.1 on the firewall.

rbenv was installed on a user on ubuntu called rails , which does not have sudo privileges, but when ansible connects to my ubuntu box, it loses env. $ PATH defined in /home/rails/.bashrc, by default was only ansible_env.PATH

Assuming you installed rbenv, follow these steps:

 git clone git://github.com/sstephenson/rbenv.git .rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc source ~/.bashrc 

in order to get the path to work, I needed to install it manually, so for this specific task I did:

 - name: install rubies become_user: rails environment: PATH: "{{ ansible_env.PATH }}:/home/rails/.rbenv/shims:/home/rails/.rbenv/bin " shell: '{{ rbenv_shell }} -lc "rbenv install {{ item[0] }}"' with_together - "{{ rubies }}" - "{{ ruby_installed.results }}" ... 

This will add the path to your rbenv command to search for ansible_env.PATH and make it work as expected.

0
source

Source: https://habr.com/ru/post/1213494/


All Articles