+

Search Tips   |   Advanced Search

Galaxy User Guide

Ansible Galaxy refers to the Galaxy website, a free site for finding, downloading, and sharing community developed 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, and new in Galaxy 3.2, collections You can find roles for provisioning infrastructure, deploying applications, and all of the tasks you do everyday. The collection format provides a comprehensive package of automation that may include multiple playbooks, roles, modules, and plugins.


Finding collections on Galaxy

To find collections on Galaxy:

  1. Click the Search icon in the left-hand navigation.

  2. Set the filter to collection.

  3. Set other filters and press enter.

Galaxy presents a list of collections that match your search criteria.


Installing collections


Installing a collection from Galaxy

By default, ansible-galaxy collection install uses https://galaxy.ansible.com as the Galaxy server (as listed in the ansible.cfg file under GALAXY_SERVER). You do not need any further configuration.

See Configuring the ansible-galaxy client if you are using any other Galaxy server, such as Red Hat Automation Hub.

To install a collection hosted in Galaxy:

You can also directly use the tarball from your build:

The install command automatically appends the path ansible_collections to the one specified with the -p option unless the parent directory is already in a folder called ansible_collections.

When using the -p option to specify the install path, use one of the values configured in COLLECTIONS_PATHS, as this is where Ansible itself will expect to find collections. If you don't specify a path, ansible-galaxy collection install installs the collection to the first path defined in COLLECTIONS_PATHS, which by default is ~/.ansible/collections

You can also keep a collection adjacent to the current playbook, under a collections/ansible_collections/ directory structure.

See Collection structure for details on the collection directory structure.


Downloading a collection from Automation Hub

You can download collections from Automation Hub at the command line. Automation Hub content is available to subscribers only, so you must download an API token and configure your local environment to provide it before you can you download collections. To download a collection from Automation Hub with the ansible-galaxy command:

  1. Get your Automation Hub API token. Go to https://cloud.redhat.com/ansible/automation-hub/token/ and click Get API token from the version dropdown to copy your API token.

  2. Configure Red Hat Automation Hub server in the server_list option under the [galaxy] section in your ansible.cfg file.

    [galaxy]
    server_list = automation_hub
    
    [galaxy_server.automation_hub]
    url=https://cloud.redhat.com/api/automation-hub/
    auth_url=https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token
    token=my_ah_token
    

  1. Download the collection hosted in Automation Hub.

    ansible-galaxy collection install my_namespace.my_collection
    


See also

Getting started with Automation Hub

An introduction to Automation Hub


Installing an older version of a collection

You can only have one version of a collection installed at a time. By default ansible-galaxy installs the latest available version. If you want to install a specific version, you can add a version range identifier. For example, to install the 1.0.0-beta.1 version of the collection:

You can specify multiple range identifiers separated by ,. Use single quotes so the shell passes the entire command, including >, !, and other operators, along. For example, to install the most recent version that is greater than or equal to 1.0.0 and less than 2.0.0:

Ansible will always install the most recent version that meets the range identifiers you specify. You can use the following range identifiers:

By default ansible-galaxy ignores pre-release versions. To install a pre-release version, you must use the == range identifier to require it explicitly.


Install multiple collections with a requirements file

You can also setup a requirements.yml file to install multiple collections in one command. This file is a YAML file in the format:

The supported keys for collection requirement entries are name, version, source, and type.

The version key can take in the same range identifier format documented above. If you're installing a collection from a git repository instead of a built collection artifact, the version key refers to a git commit-ish.

The type key can be set to galaxy, url, file, and git. If type is omitted, the name key is used to implicitly determine the source of the collection.

Roles can also be specified and placed under the roles key. The values follow the same format as a requirements file used in older Ansible releases.

To install both roles and collections at the same time with one command, run the following:

Running ansible-galaxy collection install -r or ansible-galaxy role install -r will only install collections, or roles respectively.

Installing both roles and collections from the same requirements file will not work when specifying a custom collection or role install path. In this scenario the collections will be skipped and the command will process each like ansible-galaxy role install would.


Downloading a collection for offline use

To download the collection tarball from Galaxy for offline use:

  1. Navigate to the collection page.

  2. Click on Download tarball.

You may also need to manually download any dependent collections.


Installing a collection from a git repository

You can install a collection in a git repository by providing the URI to the repository instead of a collection name or path to a tar.gz file. The collection must contain a galaxy.yml file, which will be used to generate the would-be collection artifact data from the directory. The URI should be prefixed with git+ (or with git@ to use a private repository with ssh authentication) and optionally supports a comma-separated git commit-ish version (for example, a commit or tag).

Warning

Embedding credentials into a git URI is not secure. Make sure to use safe auth options for security reasons. For example, use SSH, netrc or http.extraHeader/url.<base>.pushInsteadOf in Git config to prevent your creds from being exposed in logs.

In a requirements.yml file, you can also use the type and version keys in addition to using the git+repo,version syntax for the collection name.

Git repositories can be used for collection dependencies as well. This can be helpful for local development and testing but built/published artifacts should only have dependencies on other artifacts.


Default repository search locations

There are two paths searched in a repository for collections by default.

The first is the galaxy.yml file in the top level of the repository path. If the galaxy.yml file exists it's used as the collection metadata and the individual collection will be installed.

The second is a galaxy.yml file in each directory in the repository path (one level deep). In this scenario, each directory with a galaxy.yml is installed as a collection.


Specifying the location to search for collections

If you have a different repository structure or only want to install a subset of collections, you can add a fragment to the end of your URI (before the optional comma-separated version) to indicate which path ansible-galaxy should inspect for galaxy.yml file(s). The path should be a directory to a collection or multiple collections (rather than the path to a galaxy.yml file).


Listing installed collections

To list installed collections, run ansible-galaxy collection list. See Listing collections for more details.


Configuring the ansible-galaxy client

By default, ansible-galaxy uses https://galaxy.ansible.com as the Galaxy server (as listed in the ansible.cfg file under GALAXY_SERVER).

You can use either option below to configure ansible-galaxy collection to use other servers (such as Red Hat Automation Hub or a custom Galaxy server):

To configure a Galaxy server list in ansible.cfg:

  1. Add the server_list option under the [galaxy] section to one or more server names.

  2. Create a new section for each server name.

  3. Set the url option for each server name.

  4. Optionally, set the API token for each server name. See API token for details.

The url option for each server name must end with a forward slash /. If you do not set the API token in your Galaxy server list, use the --api-key argument to pass in the token to the ansible-galaxy collection publish command.

For Automation Hub, you additionally need to:

  1. Set the auth_url option for each server name.

  2. Set the API token for each server name. Go to https://cloud.redhat.com/ansible/automation-hub/token/ and click :Get API token from the version dropdown to copy your API token.

The following example shows how to configure multiple servers:

You can use the --server command line argument to select an explicit Galaxy server in the server_list and the value of this argument should match the name of the server. To use a server not in the server list, set the value to the URL to access that server (all servers in the server list will be ignored). Also you cannot use the --api-key argument for any of the predefined servers. You can only use the api_key argument if you did not define a server list or if you specify a URL in the --server argument.

Galaxy server list configuration options

The GALAXY_SERVER_LIST option is a list of server identifiers in a prioritized order. When searching for a collection, the install process will search in that order, for example, automation_hub first, then my_org_hub, release_galaxy, and finally test_galaxy until the collection is found. The actual Galaxy instance is then defined under the section [galaxy_server.{{ id }}] where {{ id }} is the server identifier defined in the list. This section can then define the following keys:

As well as defining these server options in the ansible.cfg file, you can also define them as environment variables. The environment variable is in the form ANSIBLE_GALAXY_SERVER_{{ id }}_{{ key }} where {{ id }} is the upper case form of the server identifier and {{ key }} is the key to define. For example I can define token for release_galaxy by setting ANSIBLE_GALAXY_SERVER_RELEASE_GALAXY_TOKEN=secret_token.

For operations that use only one Galaxy server (for example, the publish, info, or install commands). the ansible-galaxy collection command uses the first entry in the server_list, unless you pass in an explicit server with the --server argument.

Once a collection is found, any of its requirements are only searched within the same Galaxy instance as the parent collection. The install process will not search for a collection requirement in a different Galaxy instance.


Finding roles on Galaxy

Search the Galaxy database by tags, platforms, author and multiple keywords. For example:

The search command will return a list of the first 1000 results matching your search:


Get more information about a role

Use the info command to view more detail about a specific role:

This returns everything found in Galaxy for the role:


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 following the address of this galaxy server. You can set permanently this option by setting the Galaxy server value in your ansible.cfg file to use it . For information on setting the value in ansible.cfg see GALAXY_SERVER.


Installing roles

Use the ansible-galaxy command to download roles from the Galaxy website

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:

The following provides an example of using --roles-path to install the role into the current working directory:


See also

Configuring Ansible

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:

  1. Locate the role on the Galaxy search page.

  2. Click on the name to view more details, including the available versions.

You can also navigate directly to the role using the /<namespace>/<role name>. For example, to view the role geerlingguy.apache, go to https://galaxy.ansible.com/geerlingguy/apache.

To install a specific version of a role from Galaxy, append a comma and the value of a GitHub release tag. For example:

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:


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:

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:

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 in Git config to prevent your creds 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, with some caveats.

While both roles and collections can be specified in one requirements file, they need to be installed separately. The ansible-galaxy role install -r requirements.yml will only install roles and ansible-galaxy collection install -r requirements.yml -p ./ will only install collections.


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:

The following shows the contents of the requirements.yml file that now includes the webserver.yml file:

To install all the roles from both files, pass the root file, in this case requirements.yml on the command line, as follows:


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

.. versionadded:: 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 Roles.

The following shows an example meta/main.yml file with dependent roles:

Tags are inherited down the dependency chain. In order for tags to be applied to a role and all its dependencies, the tag should be applied to the role, not to all the tasks within a role.

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:

Alternately, you can specify the role dependencies in the complex form used in requirements.yml as follows:

Galaxy expects all role dependencies to exist in Galaxy, and therefore dependencies to be specified in the namespace.role_name format. If you import a role with a dependency where the src value 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.


Remove an installed role

Use remove to delete a role from roles_path:


See also

Using collections

Shareable collections of modules, playbooks and roles

Roles

Reusable tasks, handlers, and other files in a known directory structure

Next Previous