Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ansible addrole helper script
#!/bin/sh
#
# creates Ansible folder structure for new role
#
# Marcin Orlowski <mail@marcinorlowski.com>
#
# 2016-11-15: Initial version
# 2016-11-16: Added uodate mode
# Added option to override 'roles' dir name
ROLE_DIR_DEFAULT="roles"
ROLE_DIR=${ROLE_DIR_DEFAULT}
FOLDER_NAMES="files handlers meta templates tasks vars defaults"
UPDATE_ROLE=0
showHelp() {
cat << ENDOFHELP
Usage: $0 [options] ROLE_NAME
-h -? : shows this help
-r <DIR_NAME>: sets target role folder (default '${ROLE_DIR_DEFAULT}')
-u : update existing role by creating missing folders (if any)
ENDOFHELP
}
while getopts "h?r:u" opt; do
case "$opt" in
h|\?)
showHelp
exit 0
;;
r) ROLE_DIR=$OPTARG
;;
u) UPDATE_ROLE=1
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
if [ $# -ne 1 ] ; then
showHelp
exit 1
fi
ROLE_NAME="$1"
DST="${ROLE_DIR}/${ROLE_NAME}"
if [ -d "${DST}" ] && [ ${UPDATE_ROLE} -ne 1 ] ; then
echo "Role '${ROLE_NAME}' already exists"
exit 1
fi
for i in ${FOLDER_NAMES}; do
FNAME="${DST}/${i}"
DO_PROCESS=1
if [ -e "${FNAME}" ] ; then
DO_PROCESS=${UPDATE_ROLE}
fi
if [ ${DO_PROCESS} -gt 0 ] ; then
mkdir -p "${FNAME}"
if [ $? -ne 0 ] ; then
echo "Failed to create '${FNAME}'"
exit 100
fi
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.