Galaxy
Galaxy Developer Guide
You can host collections and roles on Galaxy to share with the Ansible community. Galaxy content is formatted in pre-packaged units of work such as roles <playbooks_reuse_roles>, and collections <collections>. You can create roles for provisioning infrastructure, deploying applications, and all of the tasks you do everyday. Taking this a step further, you can create collections which provide a comprehensive package of automation that may include multiple playbooks, roles, modules, and plugins.
Creating collections for Galaxy
Collections are a distribution format for Ansible content. You can use collections to package and distribute playbooks, roles, modules, and plugins. You can publish and use collections through Ansible Galaxy.
See developing_collections for details on how to create collections.
Creating roles for Galaxy
Use the init command to initialize the base structure of a new role, saving time on creating the various directories and main.yml files a role requires
$ ansible-galaxy role init role_name
The above will create the following directory structure in the current working directory:
role_name/
README.md
defaults/
main.yml
files/
handlers/
main.yml
meta/
main.yml
tasks/
main.yml
templates/
tests/
inventory
test.yml
vars/
main.yml
If you want to create a repository for the role, the repository root should be role_name.
Force
If a directory matching the name of the role already exists in the current working directory, the init command will result in an error. To ignore the error use the --force option. Force will create the above subdirectories and files, replacing anything that matches.
Container enabled
If you are creating a Container Enabled role, pass --type container to ansible-galaxy role init. This will create the same directory structure as above, but populate it with default files appropriate for a Container Enabled role. For example, the README.md has a slightly different structure, the .travis.yml file tests the role using Ansible Container, and the meta directory includes a container.yml file.
Using a custom role skeleton
A custom role skeleton directory can be supplied as follows:
$ ansible-galaxy role init --role-skeleton=/path/to/skeleton role_name
When a skeleton is provided, init will:
- copy all files and directories from the skeleton to the new role
- any .j2 files found outside of a templates folder will be rendered as templates. The only useful variable at the moment is role_name
- The
.gitfolder and any.git_keepfiles will not be copied
Alternatively, ansible.cfg can be used to configure the role skeleton and files to ignore.
[galaxy]
role_skeleton = /path/to/skeleton
role_skeleton_ignore = ^.git$,^.*/.git_keep$,^\./CLAUDE\.md$
The role_skeleton_ignore option is a list of regular expressions to match files and directories to ignore. The regular expressions are matched against directory names and relative file paths. A file in the root in the skeleton directory has the path prefix ./ (for example, ./README.md), whereas a file in a directory in the skeleton uses the directory name as a prefix (for example, plugins/README.md).
Authenticate with Galaxy
Using the import, delete and setup commands to manage your roles on the Galaxy website requires authentication in the form of an API key, you must create an account on the Galaxy website.
To create an authentication token:
- Click
Collections > API Token. - Click
Load Tokenand then copy it. - Save your token in the path set in the
GALAXY_TOKEN_PATH.
Import a role
The import command requires that you authenticate with the API token. You can include it in your ansible.cfg file or use the --token command option. You are only allowed to remove roles where you have access to the repository in GitHub.
To import a new role:
$ ansible-galaxy role import github_user github_repo
By default, the command will wait for Galaxy to complete the import process, displaying the results as the import progresses:
Successfully submitted import request 41
Starting import 41: role_name=myrole repo=githubuser/ansible-role-repo ref=
Retrieving GitHub repo githubuser/ansible-role-repo
Accessing branch: devel
Parsing and validating meta/main.yml
Parsing galaxy_tags
Parsing platforms
Adding dependencies
Parsing and validating README.md
Adding repo tags as role versions
Import completed
Status SUCCESS : warnings=0 errors=0
See ansible-galaxy for other command options.
Delete a role
The delete command requires that you authenticate with the API token. You can include it in your ansible.cfg file or use the --token command option. You are only allowed to remove roles where you have access to the repository in GitHub.
Use the following to delete a role:
$ ansible-galaxy role delete github_user github_repo
This only removes the role from Galaxy. It does not remove or alter the actual GitHub repository.
Travis integrations
You can create an integration or connection between a role in Galaxy and Travis. Once the connection is established, a build in Travis will automatically trigger an import in Galaxy, updating the search index with the latest information about the role.
You create the integration using the setup command with your API token. You will also need an account in Travis, and your Travis token. Once you are ready, use the following command to create the integration:
$ ansible-galaxy role setup travis github_user github_repo xxx-travis-token-xxx
The setup command requires your Travis token, however the token is not stored in Galaxy. It is used along with the GitHub username and repo to create a hash as described in the Travis documentation. The hash is stored in Galaxy and used to verify notifications received from Travis.
The setup command enables Galaxy to respond to notifications. To configure Travis to run a build on your repository and send a notification, follow the Travis getting started guide.
To instruct Travis to notify Galaxy when a build completes, add the following to your .travis.yml file:
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
List Travis integrations
Use the --list option to display your Travis integrations:
$ ansible-galaxy role setup --list travis github_user github_repo xxx-travis-token-xxx
ID Source Repo
---------- ---------- ----------
2 travis github_user/github_repo
1 travis github_user/github_repo
Remove Travis integrations
Use the --remove option to disable and remove a Travis integration:
$ ansible-galaxy role setup --remove ID
Provide the ID of the integration to be disabled. You can find the ID by using the --list option.
Galaxy User Guide<span id="using_galaxy"></span>
Ansible Galaxy refers to the Galaxy website, a free site for finding, downloading, and sharing community developed collections and roles.
Use Galaxy to jump-start your automation project with great content from the Ansible community. Galaxy provides pre-packaged units of work such as roles <playbooks_reuse_roles>, and collections <collections>. The collection format provides a comprehensive package of automation that may include multiple playbooks, roles, modules, and plugins. See the Galaxy documentation for full details on Galaxy.
Finding collections on Galaxy
To find collections on Galaxy:
- Click
Collections > Collectionsin the left-hand navigation. - Type in your search term. You can filter by keyword, tags, and namespaces.
Galaxy presents a list of collections that match your search criteria.
See collections for complete details on installing and using collections.
Finding roles on Galaxy
To find standalone roles (that is roles that are not part of a collection):
- Click
Roles > Rolesin the left-hand navigation. - Type in your search term. You can filter by keyword, tags, and namespaces.
Galaxy presents a list of roles that match your search criteria.
You can optionally search the Galaxy database by tags, platforms, author and multiple keywords using the ansible-galaxy CLI command.
$ ansible-galaxy role search elasticsearch --author geerlingguy
The search command will return a list of the first 1000 results matching your search:
Found 6 roles matching your search:
Name Description
---- -----------
geerlingguy.elasticsearch Elasticsearch for Linux.
geerlingguy.elasticsearch-curator Elasticsearch curator for Linux.
geerlingguy.filebeat Filebeat for Linux.
geerlingguy.fluentd Fluentd for Linux.
geerlingguy.kibana Kibana for Linux.
Get more information about a role
Use the info command to view more detail about a specific role:
$ ansible-galaxy role info username.role_name
This returns everything found in Galaxy for the role:
Role: username.role_name
description: Installs and configures a thing, a distributed, highly available NoSQL thing.
active: True
commit: c01947b7bc89ebc0b8a2e298b87ab416aed9dd57
commit_message: Adding travis
commit_url: https://github.com/username/repo_name/commit/c01947b7bc89ebc0b8a2e298b87ab
company: My Company, Inc.
created: 2015-12-08T14:17:52.773Z
download_count: 1
forks_count: 0
github_branch: main
github_repo: repo_name
github_user: username
id: 6381
is_valid: True
issue_tracker_url:
license: Apache
min_ansible_version: 2.15
modified: YYYY-MM-DDTHH:MM:SS.000Z
namespace: username
open_issues_count: 0
path: /Users/username/projects/roles
role_type: ANS
stargazers_count: 0
travis_status_url: https://travis-ci.org/username/repo_name.svg?branch=main
Installing roles from Galaxy
The ansible-galaxy command comes bundled with Ansible, and you can use it to install roles from Galaxy or directly from a Git based SCM. You can also use it to create a new role, remove roles, or perform tasks on the Galaxy website.
The command line tool by default communicates with the Galaxy website API using the server address https://galaxy.ansible.com. If you run your own internal Galaxy server and want to use it instead of the default one, pass the --server option followed by the address of this galaxy server. You can set this option permanently by setting the Galaxy server value in your ansible.cfg file. See galaxy_server for details on setting the value in ansible.cfg .
Installing roles
Use the ansible-galaxy command to download roles from the Galaxy website
$ ansible-galaxy role install namespace.role_name
Setting where to install roles
By default, Ansible downloads roles to the first writable directory in the default list of paths ~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles. This installs roles in the home directory of the user running ansible-galaxy.
You can override this with one of the following options:
- Set the environment variable
ANSIBLE_ROLES_PATHin your session. - Use the
--roles-pathoption for theansible-galaxycommand. - Define
roles_pathin anansible.cfgfile.
The following provides an example of using --roles-path to install the role into the current working directory:
$ ansible-galaxy role install --roles-path . geerlingguy.apache
intro_configuration
All about configuration files
Installing a specific version of a role
When the Galaxy server imports a role, it imports any Git tags matching the Semantic Version format as versions. In turn, you can download a specific version of a role by specifying one of the imported tags.
To see the available versions for a role:
- Locate the role on the Galaxy search page.
- Click on the name to view more details, including the available versions.
To install a specific version of a role from Galaxy, append a comma and the value of a GitHub release tag. For example:
$ ansible-galaxy role install geerlingguy.apache,3.2.0
It is also possible to point directly to the Git repository and specify a branch name or commit hash as the version. For example, the following will install a specific commit:
$ ansible-galaxy role install git+https://github.com/geerlingguy/ansible-role-apache.git,0b7cd353c0250e87a26e0499e59e7fd265cc2f25
Installing multiple roles from a file
You can install multiple roles by including the roles in a requirements.yml file. The format of the file is YAML, and the file extension must be either .yml or .yaml.
Use the following command to install roles included in requirements.yml:
$ ansible-galaxy install -r requirements.yml
Again, the extension is important. If the .yml extension is left off, the ansible-galaxy CLI assumes the file is in an older, now deprecated, "basic" format.
Each role in the file will have one or more of the following attributes:
src
The source of the role. Use the format namespace.role_name, if downloading from Galaxy; otherwise, provide a URL pointing to a repository within a Git based SCM. See the examples below. This is a required attribute.scm
Specify the SCM. As of this writing only git or hg are allowed. See the examples below. Defaults to git.version:
The version of the role to download. Provide a release tag value, commit hash, or branch name. Defaults to the branch set as a default in the repository, otherwise defaults to the master.name:
Download the role to a specific name. Defaults to the Galaxy name when downloading from Galaxy, otherwise it defaults to the name of the repository.
Use the following example as a guide for specifying roles in requirements.yml:
# from galaxy
- name: yatesr.timezone
# from locally cloned Git repository (git+file:// requires full paths)
- src: git+file:///home/bennojoy/nginx
# from GitHub
- src: https://github.com/bennojoy/nginx
# from GitHub, overriding the name and specifying a specific tag
- name: nginx_role
src: https://github.com/bennojoy/nginx
version: main
# from GitHub, specifying a specific commit hash
- src: https://github.com/bennojoy/nginx
version: "ee8aa41"
# from a webserver, where the role is packaged in a tar.gz
- name: http-role-gz
src: https://some.webserver.example.com/files/main.tar.gz
# from a webserver, where the role is packaged in a tar.bz2
- name: http-role-bz2
src: https://some.webserver.example.com/files/main.tar.bz2
# from a webserver, where the role is packaged in a tar.xz (Python 3.x only)
- name: http-role-xz
src: https://some.webserver.example.com/files/main.tar.xz
# from Bitbucket
- src: git+https://bitbucket.org/willthames/git-ansible-galaxy
version: v1.4
# from Bitbucket, alternative syntax and caveats
- src: https://bitbucket.org/willthames/hg-ansible-galaxy
scm: hg
# from GitLab or other git-based scm, using git+ssh
- src: git@gitlab.company.com:mygroup/ansible-core.git
scm: git
version: "0.1" # quoted, so YAML doesn't parse this as a floating-point value
[!WARNING] Embedding credentials into a SCM URL is not secure. Make sure to use safe auth options for security reasons. For example, use SSH, netrc or http.extraHeader/`url.<base>.pushInsteadOf <https://git-scm.com/docs/git-config#Documentation/git-config.txt-urlltbasegtpushInsteadOf>`_ in Git config to prevent your credentials from being exposed in logs.
Installing roles and collections from the same requirements.yml file
You can install roles and collections from the same requirements files
---
roles:
# Install a role from Ansible Galaxy.
- name: geerlingguy.java
version: "1.9.6" # note that ranges are not supported for roles
collections:
# Install a collection from Ansible Galaxy.
- name: community.general
version: ">=7.0.0"
source: https://galaxy.ansible.com
Installing multiple roles from multiple files
For large projects, the include directive in a requirements.yml file provides the ability to split a large file into multiple smaller files.
For example, a project may have a requirements.yml file, and a webserver.yml file.
Below are the contents of the webserver.yml file:
# from github
- src: https://github.com/bennojoy/nginx
# from Bitbucket
- src: git+https://bitbucket.org/willthames/git-ansible-galaxy
version: v1.4
The following shows the contents of the requirements.yml file that now includes the webserver.yml file:
# from galaxy
- name: yatesr.timezone
- include: <path_to_requirements>/webserver.yml
To install all the roles from both files, pass the root file, in this case requirements.yml on the command line, as follows:
$ ansible-galaxy role install -r requirements.yml
Dependencies
Roles can also be dependent on other roles, and when you install a role that has dependencies, those dependencies will automatically be installed to the roles_path.
There are two ways to define the dependencies of a role:
- using
meta/requirements.yml - using
meta/main.yml
Using meta/requirements.yml
2.10
You can create the file meta/requirements.yml and define dependencies in the same format used for requirements.yml described in the Installing multiple roles from a file section.
From there, you can import or include the specified roles in your tasks.
Using meta/main.yml
Alternatively, you can specify role dependencies in the meta/main.yml file by providing a list of roles under the dependencies section. If the source of a role is Galaxy, you can simply specify the role in the format namespace.role_name. You can also use the more complex format in requirements.yml, allowing you to provide src, scm, version, and name.
Dependencies installed that way, depending on other factors described below, will also be executed before this role is executed during play execution. To better understand how dependencies are handled during play execution, see playbooks_reuse_roles.
The following shows an example meta/main.yml file with dependent roles:
---
dependencies:
- geerlingguy.java
galaxy_info:
author: geerlingguy
description: Elasticsearch for Linux.
company: "Midwestern Mac, LLC"
license: "license (BSD, MIT)"
min_ansible_version: 2.4
galaxy_tags:
- web
- system
- monitoring
- logging
- lucene
- elk
- elasticsearch
Roles listed as dependencies are subject to conditionals and tag filtering, and may not execute fully depending on what tags and conditionals are applied.
If the source of a role is Galaxy, specify the role in the format namespace.role_name:
dependencies:
- geerlingguy.apache
- geerlingguy.ansible
Alternately, you can specify the role dependencies in the complex form used in requirements.yml as follows:
dependencies:
- name: geerlingguy.ansible
- name: composer
src: git+https://github.com/geerlingguy/ansible-role-composer.git
version: 775396299f2da1f519f0d8885022ca2d6ee80ee8
[!NOTE] Galaxy expects all role dependencies to exist in Galaxy, and therefore dependencies to be specified in the
namespace.role_nameformat. If you import a role with a dependency where thesrcvalue is a URL, the import process will fail.
List installed roles
Use list to show the name and version of each role installed in the roles_path.
$ ansible-galaxy role list
- namespace-1.foo, v2.7.2
- namespace2.bar, v2.6.2
Remove an installed role
Use remove to delete a role from roles_path:
$ ansible-galaxy role remove namespace.role_name