ansible: Quickly create new role structure

This article was published 5 years ago. Due to the rapidly evolving world of technology, some concepts may no longer be applicable.

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 ..

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 to Patrik Šíma Cancel reply