Ansible Galaxy is a repository for Ansible roles that allows users to share and reuse automation code. With Ansible Galaxy, you can easily install pre-built roles from the community and publish your own roles for others to use.
This guide covers:
✅ How to install roles from Ansible Galaxy
✅ Where Ansible Galaxy installs roles
✅ Using a downloaded role in a playbook
✅ How to publish your own role to Ansible Galaxy
✅ Generating and using an API token for Ansible Galaxy
To install a role from Ansible Galaxy, use the following command:
ansible-galaxy role install <role-name>For example, to install the bsmeding.docker role, run:
ansible-galaxy role install bsmeding.dockerBy default, Ansible Galaxy installs roles in the following directory:
~/.ansible/roles/You can verify the installed roles by listing the directory:
ls ~/.ansible/roles/Once installed, you can use the role in an Ansible playbook. Here’s an example docker-playbook.yaml that uses the bsmeding.docker role:
---
- hosts: all
become: true
roles:
- bsmeding.dockerRun the playbook with:
ansible-playbook -i inventory.ini docker-playbook.yamlIf you want to share your own role on Ansible Galaxy, follow these steps:
Create a GitHub repository with a name following the format: ansible-role-<role-name> (e.g., ansible-role-docker).
Your repository should have the following structure:
ansible-role-docker/
│-- defaults/ # Default variables
│-- files/ # Static files
│-- handlers/ # Handlers for services
│-- meta/ # Role metadata
│-- tasks/ # Task definitions
│-- templates/ # Jinja2 templates
│-- vars/ # Role-specific variables
│-- README.md # Documentation
You can generate this structure using:
ansible-galaxy role init ansible-role-dockerPush your role to GitHub:
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/<your-github-username>/ansible-role-docker.git
git push -u origin mainTo upload your role to Ansible Galaxy, use:
ansible-galaxy role import <github-username> <repo-name> --token <your-api-token>For example:
ansible-galaxy role import myusername ansible-role-docker --token myapitoken123To upload a role, you need an API token from Ansible Galaxy. Follow these steps:
- Go to Ansible Galaxy
- Log in with your GitHub account.
- Click on your profile and go to API Tokens.
- Click Create Token.
- Copy the generated token and use it in the
ansible-galaxy role importcommand.
Note: Keep your API token secure and do not share it publicly.
To update an installed role, use:
ansible-galaxy role install <role-name> --forceFor example:
ansible-galaxy role install bsmeding.docker --forceYou can install multiple roles using a requirements.yml file:
- src: bsmeding.docker
- src: geerlingguy.nginxInstall all roles listed in the file with:
ansible-galaxy role install -r requirements.ymlAnsible Galaxy simplifies role management by allowing users to install, share, and maintain reusable roles. By following this guide, you can:
✅ Install and use roles from Ansible Galaxy
✅ Understand where roles are stored
✅ Publish your own roles to Ansible Galaxy
✅ Manage API tokens for authentication
Start leveraging Ansible Galaxy today to streamline your automation workflows! 🚀