Skip to content

Instantly share code, notes, and snippets.

@ekho
Created July 16, 2017 09:51
Show Gist options
  • Save ekho/a620f687fd000b7c271f8ff5f3d97939 to your computer and use it in GitHub Desktop.
Save ekho/a620f687fd000b7c271f8ff5f3d97939 to your computer and use it in GitHub Desktop.
ansible-galaxy update
#!/usr/bin/env bash
halt() {
echo $@ >&2
exit 1
}
reqyml=${1:-requirements.yml}
[ ! -f "${reqyml}" ] && halt "Requirements file not found: ${reqyml}"
echo "Using requirements file: ${reqyml}"
requirements=`cat ${reqyml} | shyaml get-values-0 | while read -r -d '' item; do echo "${item}" | shyaml get-value name "$(echo ${item} | shyaml get-value src)" | xargs -0 -n 1 | /usr/bin/grep -Ev '((git\+)?https?://|git@)' ; done`
echo -e "Found required roles in ${reqyml}:\n${requirements}"
ansible_cfg=$(ansible --version | /usr/bin/grep 'config file = ' | awk '{print $4;}')
[ -z "${ansible_cfg}" ] && halt "can not retrieve ansible config"
[ ! -f "${ansible_cfg}" ] && halt "ansible config does not exits: ${ansible_cfg}"
echo "Using ansible config: ${ansible_cfg}"
roles_path=$(/usr/bin/grep -E '^ *roles_path *= ' ansible.cfg | awk '{print $3}')
[ -z "${roles_path}" ] && halt "roles_path does not set, exiting ..."
roles_path=$(echo "${roles_path}" | awk -F':' '{print $1}')
echo "Found path ${roles_path} in roles_path"
echo "Removing existing roles"
for r in ${requirements}; do
if [ -e "${roles_path}/${r}" ]; then
echo "- removing role ${r} from ${roles_path}"
rm -rf "${roles_path}/${r}"
fi
done
echo "Installing roles"
ansible-galaxy install --roles-path ${roles_path} -r ${reqyml}
echo "All done"
@ekho
Copy link
Author

ekho commented Jul 16, 2017

Requires shyaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment