-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansible-version.j2
100 lines (89 loc) · 2.6 KB
/
ansible-version.j2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
set -e
ANSIBLE_BIN_PATH="{{ ANSIBLE_BIN_PATH }}"
ANSIBLE_BASEDIR="{{ ANSIBLE_BASEDIR }}"
ANSIBLE_SELECTED_VERSION="{{ ANSIBLE_SELECTED_VERSION }}"
#Function: help
print_help() {
echo """$0
Usage:
$0 installed
$0 versions
$0 path <version>
$0 set <version>
Options:
installed Show available installed ansible virtualenv
versions Show current virtualenv version
path <version> Print binary path of specifc version
set <version> Set selected version for ansible virtualenv
"""
exit 0
}
show_installed(){
ansible_link="$(readlink ${ANSIBLE_BASEDIR}/bin/ansible)"
version="$(echo $ansible_link | sed 's|'"$ANSIBLE_BASEDIR"'||g ; s|/venv/bin/||g ; s|ansible||g; s|/||g')"
echo "installed version: \"$version\""
}
show_versions(){
cd $ANSIBLE_BASEDIR
for version in *
do
[[ $version =~ ^(ansible-version)$ ]] && continue
[[ $version =~ ^(bin)$ ]] && continue
versions_list+="'${version}' "
done
echo "$versions_list"
}
# Verify version
verify_version(){
versions_list="$(show_versions)"
if ! [[ ${versions_list} == *"'${ANSIBLE_SELECTED_VERSION}'"* ]]; then
echo "The desired version \"${ANSIBLE_SELECTED_VERSION}\" is not in the version list."
echo "available version: $versions_list"
exit 1
fi
if ! [ -d $ANSIBLE_BASEDIR/${ANSIBLE_SELECTED_VERSION}/venv/bin/ ]; then
echo "Your virtualenv seems to be not installed or incorrect reference. \"$ANSIBLE_BASEDIR/${ANSIBLE_SELECTED_VERSION}/venv/bin/\" is not a valid directory"
exit 1
fi
}
print_path(){
verify_version
echo "${ANSIBLE_BASEDIR}/${ANSIBLE_SELECTED_VERSION}/venv/bin/"
}
setup_links(){
verify_version
for bin in ansible ansible-doc ansible-galaxy ansible-playbook ansible-pull ansible-vault
do
echo "Ensuring symlink $ANSIBLE_BASEDIR/${ANSIBLE_SELECTED_VERSION}/venv/bin/$bin :is pointing to ${ANSIBLE_BASEDIR}/bin/${bin}"
ln -sf $ANSIBLE_BASEDIR/${ANSIBLE_SELECTED_VERSION}/venv/bin/$bin ${ANSIBLE_BASEDIR}/bin/${bin}
done
}
case $1 in
"installed")
show_installed
;;
"versions")
echo "current installed version: $(show_versions)"
;;
"path")
[ -z "$2" ] && echo "path requires a version as an argument." && exit 1
version="$2"
export ANSIBLE_SELECTED_VERSION="$2"
print_path
;;
"set")
version="$2"
[ -z "$2" ] && echo "set requires a version as an argument." && exit 1
export ANSIBLE_SELECTED_VERSION="$2"
setup_links
;;
'')
print_help
;;
*)
echo "$0: Unkown option '$1'"
print_help
;;
esac
exit 0