Helpful Commands for Working With Ansible Roles

When working with a locally installed Ansible Galaxy you have access to a bunch of useful commands such as ansible-galaxy list, ansible-galaxy remove, and others.

In our setup we don't have direct access to these. At least, not without adding a Makefile entry for each, or coming up with a more generic solution.

Fortunately it's not too often that I have ever needed these commands. But even so, there are some additions I would suggest you make, in order to make your life a bit easier.

    @docker run --rm \
        -v $(CURDIR):/crv-ansible \
        -w /crv-ansible \
        williamyeh/ansible:alpine3 \
        ansible-galaxy list

    @docker run --rm \
        -v $(CURDIR):/crv-ansible \
        -w /crv-ansible \
        williamyeh/ansible:alpine3 \
        ansible-galaxy remove $(role)

Both of these should look familiar by now.

The two key lines are:

  • ansible-galaxy list
  • ansible-galaxy remove $(role)

The first will list out all your install roles, both first and third party:

make list_roles
- codereviewvideos.test, (unknown version)
- codereviewvideos.common, (unknown version)
- singleplatform-eng.users, v1.2.5

Because we're using Ansible through Docker, we could skip the remove_role command, and just use rm -rf roles/{role_name_to_delete}, and we should be all good.

However Ansible does provide a command to do this for us, which you may wish to use:

make create_role role=codereviewvideos.test
- codereviewvideos.test was created successfully

make remove_role role=codereviewvideos.test
- successfully removed codereviewvideos.test

That's pretty helpful for maintaining your project.

Using the list_roles command is helpful for knowing exactly which third party roles your project depends upon.

Ansible requirements.yml

Even more useful is specifying all your required roles in requirements.yml. This way you can quickly get your Ansible setup up and running on a new computer.

touch requirements.yml

I'm not sure of a way to automatically add entries to requirements.yml. If you know of a way, please do let me know.

This means whenever we install_role or however you do this locally, you need to remember to add a corresponding entry into requirements.yml.

- src: geerlingguy.firewall
  version: 2.4.1

- src: singleplatform-eng.users
  version: v1.2.5

Yes, this is a pain.

You can find more info on this file's expected format in the documentation.

However, the list_roles command we added above makes this fairly straightforward. All we need to do is take the output provided by that command, and add a corresponding entry into requirements.yml as above.

Adding in an extra line to the Makefile:

    @docker run --rm \
        -v $(CURDIR):/crv-ansible \
        -w /crv-ansible \
        williamyeh/ansible:alpine3 \
        ansible-galaxy install -r requirements.yml

And then on any new computer:

make install_roles 

- downloading role 'firewall', owned by geerlingguy
- downloading role from
- extracting geerlingguy.firewall to /crv-ansible/roles/geerlingguy.firewall
- geerlingguy.firewall (2.4.1) was installed successfully
- downloading role 'users', owned by singleplatform-eng
- downloading role from
- extracting singleplatform-eng.users to /crv-ansible/roles/singleplatform-eng.users
- singleplatform-eng.users (v1.2.5) was installed successfully

As things get more complex, you may wish to add your own roles into separate Git repositories and pull them in as if they were third party modules. This is beyond the scope of this tutorial, but it's also not super complicated to do either.

Lastly, you should give the Ansible Galaxy command line reference a read if you haven't already done so.

In this tutorial we have covered how to add in some helpful additional shortcut / Makefile commands that will make your life easier.

We have also covered how to ensure your third party roles are always available when moving your Ansible setup to a new computer. This is helpful if sharing your project, using a continuous integration pipeline, or migrating from old to new hardware.