Use roles with Ansible Galaxy
Ansible Galaxy provides roles maintained by the Ansible community and open-sourced. They can easily be installed and used in playbooks.
Install role globally
Let’s use an Apache role from Ansible Galaxy to install and configure our Apache server:
- Search for the Apache role in Ansible Galaxy maintained by
- Install the role on your machine
- Update your
playbook.ymlto use this role instead of our own
Run your playbook and check everything is working. You should see a few more tasks being run - do not hesitate to visit the role’s source code to see how it’s done!
Override role’s default configuration
geerlingguy role’s configure Apache server to listen on port 80 by default. We want to listen on port
- Update your playbook to listen on port
- Note: there are several possibilities here, as you remember Ansible variables can be configured in lots of place - find the place where it will override the role’s default in the best way!
Run your playbook and check everything is working. Your Apache server should be available on port
Install role using
requirements.yml file to define the role(s) and version we want to use. Install the roles using
This setup is typically used on CI when testing our role to install all its dependencies. It’s much like a Python
Use Galaxy role alongside our own role
We now have a role to manage our Apache installation, but what about our static website content?
Update our previous
- Only include the tasks required to copy our
- Delete old tasks used to install Apache as they are now managed by our Galaxy Apache role
- Change the variable
Hello from static web [dev|prod]
Run your playbook and check everything is working.