Skip to content

Instantly share code, notes, and snippets.

@martinboy
Created January 21, 2017 10:17
Show Gist options
  • Save martinboy/4f4d63201c112f9775d19138f0628178 to your computer and use it in GitHub Desktop.
Save martinboy/4f4d63201c112f9775d19138f0628178 to your computer and use it in GitHub Desktop.
Create a directory structure and some files for an ansible role
#!/bin/sh
# Create a directory structure and some files
# for an ansible role
# Defaults
DEFAULT_ROLE_PATH=.
# Check input params
if [ -z "$1" ]; then
echo "Missing argument role name"
exit 1
fi
ROLE=$1
ROLE_PATH=${2-$DEFAULT_ROLE_PATH}/$ROLE
mkdir -p $ROLE_PATH/{tasks,handlers,templates,files,vars,meta,defaults}
# Create role skeleton
# defaults/main.yml
cat <<EOF > $ROLE_PATH/defaults/main.yml
# Role variables defaults
---
${ROLE}_enabled: yes # Enable the role
EOF
# tasks/main.yml
cat <<EOF > $ROLE_PATH/tasks/main.yml
---
- include: _run.yml
when: ${ROLE}_enabled
tags: [${ROLE}]
EOF
# tasks/_run.yml
cat <<EOF > $ROLE_PATH/tasks/_run.yml
---
- include: _configure.yml
# Include all role tasks here
# Ex.
# - include: task1.yml
# when: ${ROLE}_task1_enabled
EOF
# tasks/_configure.yml
cat <<EOF > $ROLE_PATH/tasks/_configure.yml
---
# Role pre-configure task
# Ex.
# - name: configure | Create directories
# file: path=/path/to/dir state=directory recurse=yes
EOF
# vars/main.yml
cat <<EOF > $ROLE_PATH/vars/main.yml
# Role constants
---
EOF
# feel the rest of files
echo "---" | tee $ROLE_PATH/{handlers,meta}/main.yml > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment