@@ -37,6 +37,137 @@ vercomp() {
3737 fi
3838 if ((10#${ver1[i]} < 10#${ver2[i]}))
3939 then
40+ COLLECTION_DIR="${HOME}/.ansible/collections/ansible_collections/${COLLECTION_NAMESPACE}/${COLLECTION_NAME}"
41+
42+ TOX_ARGS=
43+
44+ if [ -n "${TOX_SCENARIO}" ]
45+ then
46+ TOX_ARGS="--scenario-name ${TOX_SCENARIO}"
47+ fi
48+
49+ TOX_OPTS="-e ${TOX_ANSIBLE}"
50+
51+ vercomp() {
52+
53+ [[ $1 == $2 ]] && return 0
54+ v1=$(echo "$1" | sed -e 's|-|.|g')
55+ v2=$(echo "$2" | sed -e 's|-|.|g')
56+
57+ local IFS=.
58+ local i ver1=($1) ver2=($2)
59+ # fill empty fields in ver1 with zeros
60+ for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
61+ do
62+ ver1[i]=0
63+ done
64+
65+ for ((i=0; i<${#ver1[@]}; i++))
66+ do
67+ if [[ -z ${ver2[i]} ]]
68+ then
69+ # fill empty fields in ver2 with zeros
70+ ver2[i]=0
71+ fi
72+ if ((10#${ver1[i]} > 10#${ver2[i]}))
73+ then
74+ return 1
75+ fi
76+ if ((10#${ver1[i]} < 10#${ver2[i]}))
77+ then
78+ return 2
79+ fi
80+ done
81+ return 0
82+ }
83+
84+ list_collections() {
85+
86+ if [ -f collections.yml ]
87+ then
88+ echo "required collection(s)"
89+
90+ collections=$(ansible-galaxy collection list --format json | grep -v -E "starting run|Validate TLS")
91+
92+ is_installed="false"
93+
94+ for collection in $(grep -v "#" collections.yml | grep "^ - name: " | awk -F ': ' '{print $2}')
95+ do
96+ required_version="$(grep -v "#" collections.yml | grep -A1 "^ - name: ${collection}" | grep "version: " 2> /dev/null | awk -F ': ' '{print $2}' | sed -e 's|=||' -e 's|>||' -e 's|"||g')"
97+
98+ while read repository
99+ do
100+ version=
101+ installed_version=
102+ required_version=
103+
104+ REPOSITORY=${repository}
105+ COLLECTION=${collection}
106+
107+ repo=$(echo ${collections} | \
108+ jq -r \
109+ --arg REPOSITORY "$REPOSITORY" \
110+ 'to_entries | map(select(.key == $REPOSITORY)) | from_entries')
111+
112+ # echo "$repo"
113+
114+ installed_version=$(echo ${repo} | \
115+ jq -r \
116+ --arg COLLECTION "$COLLECTION" \
117+ '.[] | with_entries(select(.key | contains($COLLECTION))) | select(.key != {}) | .[].version')
118+
119+ if [[ ! -z "${installed_version}" ]]
120+ then
121+ echo " - '${collection}' is installed in version ${installed_version} (in ${repository})."
122+ fi
123+ done < <(echo "${collections}" | jq -r 'keys[]')
124+ done
125+ fi
126+ }
127+
128+ ansible_collection() {
129+
130+ if [ -f collections.yml ]
131+ then
132+ list_collections
133+
134+ # force install of all collections
135+ ansible-galaxy collection install --force --requirements-file collections.yml > /dev/null
136+ fi
137+ }
138+
139+ install_collection() {
140+ local collection="${1}"
141+
142+ echo "Install the required collection '${collection}'"
143+ ansible-galaxy collection install ${collection} > /dev/null
144+ }
145+
146+ remove_collection() {
147+
148+ local collection="${1}"
149+
150+ namespace="$(echo "${collection}" | cut -d '.' -f1)"
151+ name="$(echo "${collection}" | cut -d '.' -f2)"
152+
153+ collection="${HOME}/.ansible/collections/ansible_collections/${namespace}/${name}"
154+
155+ rm \
156+ --recursive \
157+ --force \
158+ "${collection}" > /dev/null
159+ }
160+
161+ publish() {
162+
163+ TOKEN="${HOME}/.ansible/galaxy_token"
164+
165+ if [ -e "${TOKEN}" ]
166+ then
167+ ansible-galaxy import --token=$(cat "${TOKEN}") bodsch # "???"
168+ fi
169+ }
170+
40171 return 2
41172 fi
42173 done
0 commit comments