Last active
April 12, 2018 22:45
-
-
Save Canta/8b44b84f8bb699f0a53840574e0d887e to your computer and use it in GitHub Desktop.
Fix include/include_tasks with tags for ansible v2.5
This file contains 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
#!/bin/bash | |
# This dirty script is supposed to run on your ansible directory. | |
# It could obviously be done better, but it's just a single use script, so I don't really care. | |
# Worked fine with bash 4.2 | |
# Anyway, check its syntax (at least the first line, the one setting the RESULT var), and use with caution. | |
# The script implements what's suggested here: | |
# http://docs.ansible.com/ansible/2.5/porting_guides/porting_guide_2.5.html#dynamic-includes-and-attribute-inheritance | |
# What it does is: | |
# 1 - Find all "include" or "include_tasks" tasks in the "roles" directory. | |
# 2 - Detect the tags assigned. If no tags, the task is ignored. | |
# 3 - Creating an ansible block to wrap the file to be included, and assigning the detected tags to that block. | |
# 4 - Replacing the (to be) include file content with the created content (the block wrap). | |
RESULT=$(grep -R -n "include: \|include_tasks: " roles/ | grep -v "Possible values include" | grep -v "#" | grep -v "{{" | tr ":" " " | sed -e 's/\ -\ /\ /') | |
TASKFILES=$(printf "$RESULT" | awk '{print $1}') | |
INCLUDES=($(printf "$RESULT" | awk '{print $4}')) | |
LINENUMBERS=($(printf "$RESULT" | awk '{print $2}')) | |
declare -A BLOCKS | |
i=0 | |
enter=$'\n' | |
for taskfile in $TASKFILES; do | |
# need to get the include file name. | |
#echo "taskfile $taskfile" | |
name=$(echo $taskfile | tr "/" " " | awk '{print $NF}') | |
empty="" | |
taskfile_path=$(echo "${taskfile/$name/$empty}") | |
name_include=$(echo ${INCLUDES[$i]} | tr -d "\"" | tr -d "'") | |
full_include=$(echo $taskfile_path$name_include) | |
# now, need to get the tags applied to the include task. | |
include_line=$(echo ${LINENUMBERS[$i]} | tr -d "\"" | tr -d "'") | |
tamanio=$(cat $taskfile | wc -l) | |
tail_lines=$(($tamanio - $include_line)) | |
tags="" | |
content=$(tail -n$tail_lines $taskfile) | |
IFS=$enter | |
for l in $content ; do | |
if [[ $l =~ tags ]]; then | |
tags=$(echo "$l" | sed -e 's/tags\:/\ /' | tr -d " ") | |
fi | |
l=$(echo "$l" | tr -d " " | tr -d "\t") | |
if [[ -z $l ]]; then | |
break 1 | |
fi | |
done | |
# We got the tags. Just need to create the blocks now. | |
IFS=$enter | |
content=$(cat $full_include | grep -v "\-\-\-" | sed "s/^$/\ \ /") | |
spaces=" " | |
tabbed_content="" | |
IFS=$enter | |
for c in $(echo "$content"); do | |
tabbed_content+="$spaces$c$enter" | |
done | |
if [[ -n $tags ]]; then | |
block="---$enter" | |
block+="- block:$enter$tabbed_content" | |
block+=" tags: $tags" | |
#echo "$block$enter" | |
#echo "include: $full_include" | |
index="$full_include" | |
BLOCKS[$index]="$block" | |
#echo "$block" > $full_include | |
#exit 0 | |
#echo "$taskfile $linea $tags" | |
fi | |
i=$(($i+1)) | |
done | |
echo "${#BLOCKS[@]} blocks detected" | |
for block in "${!BLOCKS[@]}"; do | |
#echo "${BLOCKS[$block]}$enter" | |
echo "${BLOCKS[$block]}" > $block | |
#exit 0 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment