The ansible-galaxy init command is used to create the files and directories used for roles. If you are not familiar with roles, check out our Getting Started with Ansible Roles article. In this example, a directory named "apache" will be created, the files and directories for the foo role is created.
~]$ ansible-galaxy init foo
- Role foo was created successfully
Target directory (--init-path)
In this example, the foo role would be created in the present working directory, as can be seen here.
~]$ ansible-galaxy init --help
. . .
--init-path INIT_PATH
The path in which the skeleton role will be created.
The default is the current working directory.
Notice that the --init-path option can be used to specify the directory where the role should reside, like this.
~]$ ansible-galaxy init foo --init-path /etc/ansible/roles
- Role foo was created successfully
Overwrite existing (--force)
If there is already a role named foo at /etc/ansible/roles, the --force option would overwrite the current foo role. This is typically something you wouldn't want to do.
~]$ ansible-galaxy init apache --init-path /etc/ansible/roles --force
- Role apache was created successfully
Here is what the contents of the foo directory will contain.
~]$ ls -l /etc/ansible/roles/apache
drwxrwxr-x. 2 root root 21 May 11 11:53 defaults
drwxrwxr-x. 2 root root 6 May 11 11:53 files
drwxrwxr-x. 2 root root 21 May 11 11:53 handlers
drwxrwxr-x. 2 root root 21 May 11 11:53 meta
-rw-rw-r--. 1 root root 1328 May 11 11:53 README.md
drwxrwxr-x. 2 root root 21 May 11 11:53 tasks
drwxrwxr-x. 2 root root 6 May 11 11:53 templates
drwxrwxr-x. 2 root root 37 May 11 11:53 tests
drwxrwxr-x. 2 root root 21 May 11 11:53 vars
Template / Skeleton
Let's say you will only plan to ever use the 'tasks' and 'vars' roles directories. In this scenario, it may make sense to create a roles template, like this.
~]$ ls -l /etc/ansible/roles/template
drwxrwxr-x. 2 root root 21 May 11 11:53 tasks
drwxrwxr-x. 2 root root 21 May 11 11:53 vars
When creating a role, the --role-skeleton option can be used to create the role from the template.
~]$ ansible-galaxy init foo --init-path /etc/ansible/roles --role-skeleton /etc/ansible/roles/template
- Role apache was created successfully
Did you find this article helpful?
If so, consider buying me a coffee over at