Skip to content

Instantly share code, notes, and snippets.

@goneri

goneri/ansible-venv.fish

Last active Jun 8, 2021
Embed
What would you like to do?
Quickly prepare your ansible venv for your collections. https://goneri.lebouder.net/2021/06/02/ansible-collections-and-venv/
#!/usr/bin/env fish
# License: GPLv3+
# Usage:
# To start a venv with the default python3 and Ansible devel:
# . ~/bin/ansible-venv.fish
#
# To start a venv with a specific version:
# . ~/bin/ansible-venv.fish 3.4 stable-2.9
if test (count $argv) -eq 2
set python_version $argv[1]
set ansible_branch $argv[2]
else
set python_version (python3 -c 'import sys;print("%d.%d" % (sys.version_info.major, sys.version_info.minor))')
set ansible_branch "devel"
end
set _max_depth (count (string split -- / $PWD))
set base_dir $PWD
while test $_max_depth -gt 2
echo $base_dir
if test -f $base_dir/galaxy.yml
echo galaxy.yml found in $base_dir
set collection_name (cat $base_dir/galaxy.yml |shyaml get-value name)
set collection_namespace (cat $base_dir/galaxy.yml |shyaml get-value namespace)
set venv_name venv-$collection_name.$collection_namespace-py$python_version-$ansible_branch
break
end
echo $base_dir/requirements.txt
if test -f $base_dir/requirements.txt
set venv_name venv-(basename $base_dir)-py$python_version-$ansible_branch
echo $venv_name
break
end
set _max_depth (math $_max_depth-1)
set base_dir $base_dir/..
end
if test -z $venv_name
echo "no galaxy.yml or requirements.txt file found, sorry... :-("
exit 1
end
echo "~/tmp/$venv_name"
set venv_path ~/tmp/$venv_name
if test -d $venv_path
echo Reusing the existing venv at $venv_path
else
echo Preparing a venv with $python_version and ansible $ansible_branch in $venv_path
if [ "$python_version" = "2.7" ]
virtualenv -p python2.7 $venv_path
else
python$python_version -mvenv $venv_path
end
$venv_path/bin/pip install pip install git+https://github.com/ansible/ansible.git@$ansible_branch
$venv_path/bin/pip install -r $base_dir/requirements.txt -r $base_dir/test-requirements.txt
end
source $venv_path/bin/activate.fish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment