Skip to content

Instantly share code, notes, and snippets.

@joshcampbell
Forked from zircote/init_ansible_role.sh
Created October 20, 2015 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshcampbell/130531c2688b6eb6e322 to your computer and use it in GitHub Desktop.
Save joshcampbell/130531c2688b6eb6e322 to your computer and use it in GitHub Desktop.
bash function to create a boilerplate ansible role (I am lazy)

Ansible Role Init.

Creates a boilerplate ansible roles, directories and main.yaml files. This is for the lazy, I am, this is why I becamse a developer.

how to use:

add the contencts of ansible.sh to your ~/.bash_profile of a separate file and source it from the ~/.bash_profile.

To use call the function:

$ init_ansible_role my_role
creating file: roles/my_role/defaults/main.yaml
creating file: roles/my_role/tasks/main.yaml
creating file: roles/my_role/vars/main.yaml
creating file: roles/my_role/handlers/main.yaml
creating file: roles/my_role/meta/main.yaml

This will create the following directory structure:

roles/
└── my_role
    ├── defaults
    │   └── main.yaml
    ├── files
    ├── handlers
    │   └── main.yaml
    ├── meta
    │   └── main.yaml
    ├── tasks
    │   └── main.yaml
    ├── templates
    └── vars
        └── main.yaml

If the file exists it will be skipped (don't want to overwrite existing files.)

#!/bin/sh
function init_ansible_role {
if [[ ! -n $1 ]]; then
echo no init
return
fi
mkdir -p roles/${1}/{defaults,tasks,files,templates,vars,handlers,meta}
for i in defaults tasks vars handlers meta; do
if [[ ! -f roles/${1}/${i}/main.yaml ]]; then
echo creating file: roles/${1}/${i}/main.yaml
echo "---
# Default Ansible YAML
" > roles/${1}/${i}/main.yaml
else
echo roles/${1}/${i}/main.yaml exists skipping
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment