Scenario
You are standardizing how configuration is delivered across environments and want a repeatable way to reuse roles. Your goal is to search Galaxy for a proven Nginx role, install it, run it from a playbook, and then create a clean skeleton for an internal role that will live in your repo.
Treat third-party roles like dependencies. Validate behavior, pin versions when possible, and keep your project structure consistent so roles are discoverable and maintainable over time.
Objective
-
Create a project directory with a local
roles/path. - Search Ansible Galaxy for a community role and install one.
- Use the installed role from a simple site playbook.
-
Generate a new custom role skeleton with
ansible-galaxy init.
Concepts
- Role lifecycle: search, install, consume, then author.
-
Default Galaxy install paths versus project-local
roles/. -
Role execution via
roles:and the default entry point (tasks/main.yml). -
Role skeleton layout:
tasks,handlers,defaults,vars,templates, andfiles. - Reproducibility basics: version pinning and consistent structure to reduce drift.
Walkthrough
mkdir -p galaxy_lab/roles && cd galaxy_lab
This establishes a simple project layout baseline. Playbooks live at the project root and roles live under
./roles
(a common convention).
# Confirm structure:
ls -la
ls -la roles
ansible-galaxy search nginx
Search is the fast discovery step. In production, you also review README content, supported platforms, defaults, and whether the role is actively maintained.
Found roles matching 'nginx'
geerlingguy.nginx
nginxinc.nginx
...
ansible-galaxy install geerlingguy.nginx
By default, Galaxy installs into your user roles path (commonly
~/.ansible/roles). You can keep roles project-local when you need repeatable builds.
# Confirm the role exists (path may vary):
ls -la ~/.ansible/roles | grep geerlingguy.nginx
nano site.yml
# OR
vim site.yml
- hosts: all
become: true
roles:
- geerlingguy.nginx
This is the simplest role consumption pattern: list the role under
roles:
and Ansible runs its default entry point (tasks/main.yml).
ansible-playbook -i inventory.ini site.yml
This confirms the role installs packages, applies configuration, and manages the service. Use
--syntax-check
when you need fast parsing validation, and use
--check
when you need a dry-run view of changes.
# Quick validation options:
ansible-playbook -i inventory.ini site.yml --syntax-check
ansible-playbook -i inventory.ini site.yml --check
ansible-galaxy init roles/myrole
This is the baseline for internal roles: predictable structure, clear defaults, and a place for templates, files, and tests.
Build logic under
tasks/
and expose only necessary knobs through
defaults/.
# Inspect the generated structure:
find roles/myrole -maxdepth 2 -type f -print
Common breakpoints
Broaden the search term or search for the vendor namespace. For high-signal roles, validate the repo activity and supported platforms before installing.
Confirm the role path Ansible is using. If you want project-local roles, install into
./roles
and ensure your configuration points Ansible to that path.
Many service roles require root-level changes. Use
become: true
and ensure the target user has sudo access.
Inventory and role execution may be correct, but the node may be missing dependencies or have conflicting configuration. Inspect service state and logs after the run.
Cleanup checklist
This lab creates local playbook content and a generated role skeleton. Remove the project directory if you do not need it for later labs.
cd ..
rm -rf galaxy_lab
Your working directory is clean and the project artifacts are removed without impacting your global Ansible install.
Reference
-
ansible-galaxy search <term>: Searches Ansible Galaxy for roles matching a keyword. -
ansible-galaxy install <namespace.role>: Downloads and installs a role into the default roles path.-
<namespace.role>: Galaxy role identifier (for examplegeerlingguy.nginx).
-
-
ansible-galaxy init <path>: Generates a standard role skeleton at the specified path. -
ansible-playbook -i <inventory> <playbook>: Runs a playbook against the specified inventory.-
-i <inventory>: Inventory source file (for exampleinventory.ini).
-
-
find <path> -maxdepth <n> -type f -print: Lists files under a path to quickly inspect generated role structure.-
-maxdepth <n>: Limits recursion depth. -
-type f: Restricts results to files.
-
-
rm -rf <path>: Removes a directory tree (use with caution).-
-r: Recursively removes directories and contents. -
-f: Forces removal and suppresses prompts/errors for missing files.
-