Skip to content

Instantly share code, notes, and snippets.

@Noppy
Last active August 18, 2017 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noppy/62a8e9998518962815adbaffec63d77f to your computer and use it in GitHub Desktop.
Save Noppy/62a8e9998518962815adbaffec63d77f to your computer and use it in GitHub Desktop.
Create ansible-playbook directories.

NAME

create_ansible_dirs.sh - create ansible-playbook directories

##SYNOPSIS create_ansible_dirs.sh [role_name1] [role_name2] ...

##DESCRIPTION Create_ansible_dirs is creating directories for ansible best practices playbooks. the created directories will be in the structure such as the following.

<play-book RootDir> *Let's pre-create this directory befor exec shell!
   +/group_vers
   +/host_vars
   +site.yml
   +/roles
      +/tasks
         +main.yml
      +/handlers
         +main.yml
      +/templates
      +/files
      +/vars
         +main.yml
      +/defaults            
         +main.yml
      +/deta            
         +main.yml
#!/bin/bash
#------------------------------------------------
#NAME
# create_ansible_dirs.sh
# - create ansible-playbook directories
#
#SYNOPSIS
# create_ansible_dirs.sh [role_name1] [role_name2] ...
#
#DESCRIPTION
# Create_ansible_dirs is creating directories for
# ansible best practices playbooks. the created directories
# will be in the structure such as the following.
#
# <play-book RootDir> *Let's pre-create this directory befor exec shell!
# +/group_vers
# +/host_vars
# +site.yml
# +/roles
# +/tasks
# +main.yml
# +/handlers
# +main.yml
# +/templates
# +/files
# +/vars
# +main.yml
# +/defaults
# +main.yml
# +/deta
# +main.yml
#------------------------------------------------
#------------
# Function
#------------
function create_file(){
file_name="${1}"
dir_path="${2}"
# Set a file path
if [ -z "${2}" ]; then
file_path="${file_name}"
else
file_path="${dir_path}/${file_name}"
fi
# Create file
if [ ! -f ${file_path} ]; then
(cat > ${file_path}) << EOL
---
# file: ${file_path}
#
EOL
fi
}
#------------
# Main Rotine
#------------
# Confirmation of execution
echo "Is the directories may be created in the \"${PWD}\"?"
echo "Yes=> [Yy], No=>OtherKey"
read ans
if [[ ! "X${ans}" =~ ^X[Yy] ]]; then
echo "Abend."
exit
fi
# Create directories in the ansible-playbook base direcotry.
if [ ! -d group_vars ]; then mkdir group_vars; fi
if [ ! -d host_vars ]; then mkdir host_vars; fi
if [ ! -d roles ]; then mkdir roles; fi
# Create "site.yml"
create_file site.yml
# Create sub directories for roles
for role in $@
do
dir="roles/${role}"
if [ ! -d "${dir}" ]; then
mkdir "${dir}"
fi
# Create sub directries for a role.
for subdir in tasks handlers templates files vars defaults meta
do
if [ ! -d "${dir}/${subdir}" ]; then
mkdir "${dir}/${subdir}"
fi
done
# Create "main.yml"
for subdir in tasks handlers vars defaults meta
do
create_file "main.yml" "${dir}/${subdir}"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment