Skip to main content

Configure Ansible with Nexus

Configure your Ansible Galaxy client to connect to and authenticate to a Nexus Ansible repository. You must create an Ansible repository in Nexus before configuring your client to connect to it. Refer to Create an Ansible Repository for more details.

Ansible Galaxy can be configured through the following methods:

  • Requirements File

  • Ansible Configuration File

Configuration Method

Best For

Requirements File

Installing specific collections, versions, or version ranges from Nexus

Ansible Configuration File

Defining reusable Nexus repository servers for Ansible Galaxy commands

Though both the methods support basic authentication which is username and password, Sonatype recommends to use token authentication method for enhanced security.

Requirements File

Use a requirements.yml file when you want to define one or more collections and the Nexus repository source for each collection.

Use the following syntax:

---
collections:
  - name: <namespace>.<collection_name>
    version: "<version>"
    source: <nexus_repository_url>

Where,

  • <namespace> - Collection namespace

  • <collection_name> - Collection name

  • <version> - Collection version or version range. Omit this field to install the latest available version

  • <nexus_repository_url> - Nexus Ansible repository URL

Example:

---
collections:
  - name: community.general
    version: "8.0.0"
    source: http://example.nexus.com/repository/ansible-proxy/

  - name: myorg.mycollection
    source: http://example.nexus.com/repository/ansible-hosted/

  - name: nginxinc.nginx_core
    version: ">=2.0.0,<3.0.0"
    source: http://example.nexus.com/repository/ansible-group/

For protected repositories, include a Base64-encoded token in the requirements file.

---
collections:
  - name: <namespace>.<collection_name>
    source: <nexus_repository_url>
    token: "<base64_token>"

Where,

  • <namespace> - Collection namespace

  • <collection_name> - Collection name

  • <nexus_repository_url> - Nexus Ansible repository URL

  • <base64_token> - Base64-encoded username:password value. Note that Ansible Galaxy Bearer Token Realm (AnsibleGalaxyToken) must be added to the Active realms as documented in Realms.

Example:

---
collections:
  - name: private.collection
    source: http://example.nexus.com/repository/ansible-private/
    token: "XXXXXXXXXXXXXXXXX"

Ansible Configuration File

Use an ansible.cfg file when you want to configure reusable Nexus repository servers for Ansible Galaxy commands.

[defaults]
collections_path = <collections_path>

[galaxy]
server_list = <server_name>

[galaxy_server.<server_name>]
url = <nexus_repository_url>
token = "<base64_token>"

Where,

  • <collections_path> - Local path where Ansible installs collections

  • <server_name> - Local name for the Nexus repository server configuration

  • <nexus_repository_url> - Nexus Ansible Galaxy repository URL

  • <base64_token> - Base64-encoded username:password value. Note that you need to add AnsibleGalaxyBearerTokenRealm to Active Realms.

Example:

[defaults]
collections_path = ./collections

[galaxy]
server_list = nexus_proxy, nexus_hosted

[galaxy_server.nexus_proxy]
url = http://example.nexus.com/repository/ansible-proxy/
token = "XXXXXXXXXXXXXXXXX"

[galaxy_server.nexus_hosted]
url = http://example.nexus.com/repository/ansible-hosted/
token = "XXXXXXXXXXXXXXXXX"