Last active
March 31, 2022 17:27
-
-
Save joshzcold/b2e3f5a1aa577a7562a717e2952092b2 to your computer and use it in GitHub Desktop.
Way faster ansible-galaxy handling with some parallelization and parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# ./ur [ansible-galaxy dependency name] -> install the 1 dependency | |
# ./ur playbooks/play.yml -> parse and install dependencies found in playbook | |
# ./ur -> find all dependencies in ansible-requirements.yml and install them in parallel | |
# | |
#!/usr/bin/env bash | |
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) | |
function ansible-update() { | |
require_block=$(grep -B 2 -Pw "(?<=name:\s)$1$" ansible-requirements.yml) | |
src=$(echo "$require_block" | grep -Po "(?<=src: ).*") | |
version=$(echo "$require_block" | grep -Po "(?<=version: ).*") | |
name=$(echo "$require_block" | grep -Po "(?<=name: ).*") | |
set -x | |
ansible-galaxy install "$src,$version,$name" -f | |
{ set +x; } 2>/dev/null | |
} | |
function find-roles() { | |
for var in "$@"; do | |
if [ -f "$var" ]; then | |
[ ! -z "$DEBUG" ] && echo | |
[ ! -z "$DEBUG" ] && echo "----- $var -----" | |
cat $var | awk ' | |
/^.*include_role/,/name:\s[A-Za-z0-9_-]+/ { | |
if( $0 ~ /name:/ ){ | |
print gensub(/.*name:\s([A-Za-z0-9_-]+).*/, "\\1", "g", $0) | |
} | |
} | |
/^\s+roles:$/,/.*[^roles]\w:$/ { | |
if($0 ~ /\s+-\s[A-Za-z0-9_-]+/){ | |
if($0 ~ /role:/){ | |
print gensub(/.*-\srole:\s([A-Za-z0-9_-]+).*/, "\\1", "g", $0) | |
next | |
} | |
if($0 ~ /^\s+- [A-Za-z0-9_-]+/){ | |
print gensub(/.*-\s([A-Za-z0-9_-]+).*/, "\\1", "g", $0) | |
} | |
} | |
if($0 ~ /^\s+-\s{.*role:/){ | |
print gensub(/.*role:\s([A-Za-z0-9_-]+).*/, "\\1", "g", $0) | |
} | |
} | |
' | sort -u | |
[ ! -z "$DEBUG" ] && echo | |
fi | |
done | |
} | |
export -f ansible-update find-roles | |
if [ -z "$@" ];then | |
names=$(grep -Po "(?<=name:\s).*$" ansible-requirements.yml) | |
parallel ansible-update ::: "$names" | |
else | |
for var in "$@"; do | |
if [ -f "$var" ]; then | |
lines="$(find-roles "$var")" | |
list="$(echo "$lines" | awk 'NF')" | |
echo "$list" | |
parallel ansible-update ::: "$list" | |
elif [ ! -d "$var" ]; then | |
ansible-update $var | |
fi | |
done | |
fi | |
# zsh completion for roles | |
# #!/usr/bin/env zsh | |
# _ur_completions() { | |
# COMPREPLY=($(compgen -W "$(grep -oP "(?<=name: ).*" ansible-requirements.yml)")) | |
# } | |
# | |
# complete -o bashdefault -o default -F _ur_completions ur |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment