Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
@zoredache

This comment has been minimized.

Copy link

@zoredache zoredache commented Aug 10, 2017

You can also use ansible-galaxy init my_role.

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.