ansible: Quickly create new role structure

Update: Instead of this, just use ansible-galaxy init demo.role

This is based on the Ansible documentation for creating roles.

Create your role name

mkdir demo.role && cd demo.role

Create all the folders we need

mkdir tasks handlers templates files vars meta

Create our empty main.yml files

echo "---" > {handlers,meta,tasks,vars}/main.yml

Alternatively, save the following as /usr/local/bin/ansible-gen-role, chmod +x /usr/local/bin/ansible-gen-role and then use ansible-gen-role (it depends on a zsh feature for multiple redirects)


mkdir $1 && cd $1
mkdir tasks handlers templates files vars meta
echo "---" > {handlers,meta,tasks,vars}/main.yml
cd ..

Michael is a polyglot software engineer, committed to reducing complexity in systems and making them more predictable. Working with a variety of languages and tools, he shares his technical expertise to audiences all around the world at user groups and conferences. You can follow @mheap on Twitter

Thoughts on this post

Alexander 2016-08-06

Thanks a lot! For me it’s much more easier to use your script!

Patrik Šíma 2017-06-23

Three years and still very usefull. Thanks!

Leave a comment?

Leave a Reply