In my work environment I have already upgraded to a newer ansible version than what co-workers have. By accident I keep using features that their version does not have yet.
Is there a way to prevent this on my side in that I force ansible to pretend it is actually an older version?
You can use python virtual environments to handle multiple ansible versions. I use pyenv to manage virtualenvs. Here is how you can do it in bash on Linux:
Install pyenv:
You can change paths according to your liking if you don't want default paths.
git clone https://github.com/pyenv/pyenv.git ${HOME}/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ${HOME}/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ${HOME}/.bashrc
echo 'if command -v pyenv 1>/dev/null 2>&1; then eval "$(pyenv init -)"; fi' >> ${HOME}/.bashrc
Install pyenv virtualenv for user:
git clone https://github.com/pyenv/pyenv-virtualenv.git ${HOME}/.pyenv/plugins/pyenv-virtualenv
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
Initialize pyenv environment for current shell
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Install older ansible in one virtualenv:
pyenv virtualenv old_env
pyenv activate old_env
pip install ansible==2.4
Install newer ansible in other virtualenv:
pyenv virtualenv new_env
pyenv activate new_env
pip install ansible==2.7
Daily Usage:
You can switch between virtualenvs whenver you want. e.g. to switch to old ansible virtualenv and check ansible version, you can do like this:
pyenv activate old_env
ansible --version
Related
I'm having trouble setting up the rbenv paths
I follow the instructions as specified here: rbenv installation page
I run the command on a zsh terminal:
rbenv init
The terminal gives me the instruction to run
eval "$(rbenv init - zsh)"
I then close the restart the terminal and check to see if all is configured correctly by running:
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | bash
and I get the following failure:
Checking for rbenv shims in PATH: not found
If I try open the fils .zshrc I find only the following path written to it
export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
but no shims. Can I add the shims manually? I don't know what it should look like? Or is there any reason the command eval "$(rbenv init - zsh)" is not working properly?
Got it working: I need to run echo 'eval "$(rbenv init -)"' >> ~/.zshrc
With the recent release of pyenv 2.0.0, the documentation currently recommends the following configuration for a bash shell:
# inside ~/.bash_profile
eval "$(pyenv init --path)"
# inside ~/.bashrc
eval "$(pyenv init -)"
If I'm not allowed to edit .bashrc on my system (for reasons completely unrelated to pyenv), what would be the drawbacks of just putting both inside my profile, like this:
# inside ~/.bash_profile
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
I've been using it like this for a few hours, and I haven't noticed any issues (on MacOS, with a Homebrew installation of pyenv, and Python 3.6/3.7/3.8 virtual environments created with venv). I'm trying to understand what might be the drawbacks of configuring my system this way, and why the documentation makes such a big deal about using both .bash_profile and .bashrc.
Will configuring pyenv in this way cause problems for me in the future?
Bash only runs ~/.bash_profile when you start a login shell. When you start a new shell by typing bash or exec bash, its runs only ~/.bashrc. However, you can work around this easily by always starting new shells with bash -l or exec bash -l, which will make the new shell a login shell.
To find out whether it makes a difference whether you don't run eval "$(pyenv init -)" in non-login shells, let's have a look at what pyenv init - really does:
$ pyenv init -
export PYENV_SHELL=bash
source '/usr/local/Cellar/pyenv/2.0.0/libexec/../completions/pyenv.bash'
command pyenv rehash 2>/dev/null
pyenv() {
local command
command="${1:-}"
if [ "$#" -gt 0 ]; then
shift
fi
case "$command" in
rehash|shell)
eval "$(pyenv "sh-$command" "$#")"
;;
*)
command pyenv "$command" "$#"
;;
esac
}
Since export PYENV_SHELL=bash puts $PYENV_SHELL into the environment, it will get imported for your non-login shells, too. So, that doesn't make any difference.
As for the rest, it looks like the main thing you're missing out on is command line completion for pyenv. Other than that, there's just some special wrapping for pyenv shell and pyenv rehash.
So, as long as you don't use tab completion for pyenv nor use the commands pyenv shell and pyenv rehash in your non-login shells, then it should make no difference. But even if you do want to use those there, you can simply start the shell with bash -l or just run eval "$(pyenv init -)" manually.
This is a known topic, almost similar to this one.
However - why am I having the pyenv settings passed in ok from .profile at login only up to the virtualenv string.
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
eval "$(pyenv virtualenv-init -)"
After login, the above portion is read (pyenv is active in terminal) but not virtualenv. Manual activation of the venv produces the well-known error message.
$pyenv activate venv_name
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
I can solve this by explicitly sourcing .profile - then it works fine.
But why would .profile not be read or interpreted at once at login?
Modifying .bashrc takes no effect. Seems like it's not being read. But .profile is.
Global python is system (2.7).
Take care
Try pyenv init --path instead of pyenv init - and again putting it in bashrc.
Running (x)ubuntu 16.04 with sudo user ...
installed rbenv with local user + sudo plugin
cd ~
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
# sudo plugin
git clone git://github.com/dcarley/rbenv-sudo.git ~/.rbenv/plugins/rbenv-sudo
rbenv install 2.4.1
rbenv global 2.4.1
ruby -v
The shell command is not working with this setup, what am i missing?
Is there a different way than the shell command to set RBENV (rubyversion) for a github project for example?
EDIT1: it appears when RBENV shell is called manually through terminal it works, just not in a #!/bin/bash script
According to your edit, it sounds like the problem stems from not running the script from your interactive shell. Non-interactive shells that aren't invoked with the --login options don't source .bashrc or any other files. You can source .bashrc manually at the start of your shell script:
. ~/.bashrc
You could also just initialize rbenv and not any of your other configurations by adding this line to your shell script:
eval "$(rbenv init -)"
I installed ruby 2.0 into ~/.rbenv/versions last and now nothing but that is available
$ rbenv versions
system
*ruby-1.9.3-p392 (set by /apps/test_app/.ruby-version)
ruby-2.0.0-p0
$ ruby -v
ruby 2.0.0.p0
$ env | grep PATH
PATH=/home/cbron/.rbenv/shims:/home/cbron/.rbenv/bin
$cat ~/.bash_profile
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
$rbenv global
ruby-1.9.3-p392
$rbenv local
ruby-1.9.3-p392
$rbenv shell
rbenv: no shell-specific version configured
edit: now set the shell, still nothing.
$rbenv shell
ruby-1.9.3-p392
ruby -v still getting
ruby 2.0.0p0
I already sourced my bash_profile, even restarted the computer.
I had the same issue using zsh and this fixed it:
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshenv
$ echo 'eval "$(rbenv init -)"' >> ~/.zshenv
$ echo 'source $HOME/.zshenv' >> ~/.zshrc
$ exec $SHELL
So basically moving the lines from profile to env!
Extracted from rbenv readme:
rbenv shell
Sets a shell-specific Ruby version by setting the RBENV_VERSION
environment variable in your shell. This version overrides
application-specific versions and the global version.
$ rbenv shell jruby-1.7.1
When run without a version number, rbenv shell reports the current
value of RBENV_VERSION. You can also unset the shell version:
$ rbenv shell --unset
Note that you'll need rbenv's shell integration enabled (step 3 of the
installation instructions) in order to use this command. If you prefer
not to use shell integration, you may simply set the RBENV_VERSION
variable yourself:
$ export RBENV_VERSION=jruby-1.7.1
So in order to use it you need to specify the ruby version as rbenv shell argument (f.e. rbenv shell 2.0.0.p0, or set RBENV_VERSION (f.e. export RBENV_VERSION=2.0.0.p0)
TL;DR: just reinstall rbenv, unless you are as stubborn as me...
I had messed up permissions in the .rbenv folder, so the shims weren't loading because they didn't have execute permissions
chmod u+x ~/.rbenv/shims/*
Disclaimer
I had deeper permissions problems, rbenv wasn't even working, so I did other chmod u+x ... previously
chmod u+x ~/.rbenv/libexec/*
chmod u+x ~/.rbenv/**/bin/*
Probably easier and safer to just reinstall rbenv. But this was fun!