# Ansible CLI Usage

This page describes the CLI commands to create, build, publish, install, and download collections after creating and configuring an Ansible Galaxy repository on Nexus.

## Create and Build Ansible Galaxy Collections

To create and build an Ansible Galaxy collection, take the following steps:

1. Initialize a new collection:

```
   ansible-galaxy collection init <namespace>.<collection_name>
   ```
   Where,
   
   - `<namespace>` \- collection namespace
   - `<collection_name>` \- collection name

2. Navigate to the collection directory:

```
   cd <namespace>/<collection_name>
   ```

3. Build the collection:

```
   ansible-galaxy collection build
   ```

Example:

```
   ansible-galaxy collection init myorg.mycollection
   cd myorg/mycollection
   ansible-galaxy collection build
   ```
   The example command creates a collection archive.

```
      #Output:
       myorg-mycollection-1.0.0.tar.gz
   ```

## Publish Ansible Galaxy Collections

After configuring authentication, publish Ansible Galaxy collections to a Nexus hosted repository using the `ansible-galaxy collection publish` command.

```
   ansible-galaxy collection publish ./<namespace>-<collection_name>-<version>.tar.gz \
     --api-key="<base64_token>" \
     --server="<nexus_repository_url>"
   ```
   Where,
   - `<namespace>` \- collection namespace
   - `<collection_name>` \- collection name
   - `<version>` \- collection version
   - `<base64_token>` \- Base64-encoded `username:password` value
   - `<nexus_repository_url>`\- Your hosted repository URL

Example:

```
   NEXUS_URL="http://example.nexus.com"
   REPO="ansible-hosted"
   TOKEN=$(echo -n "admin:admin123" | base64)

ansible-galaxy collection publish ./myorg-mycollection-1.0.0.tar.gz \
     --api-key="$TOKEN" \
     --server="$NEXUS_URL/repository/$REPO/"
   ```

## Upload Ansible Galaxy Collections with Curl

You can also upload a collection to a hosted repository by using `curl`.

```
   curl -X PUT \
     -u <username>:<password> \
     -T ./<namespace>-<collection_name>-<version>.tar.gz \
     "<nexus_repository_url>/api/v3/plugin/ansible/content/published/collections/artifacts/<namespace>-<collection_name>-<version>.tar.gz"
   ```
   Where,
   - `<username>` \- Your Nexus Repository username
   - `<password>` \- Your Nexus Repository password
   - `<namespace>` \- Collection namespace
   - `<collection_name>` \- Collection name
   - `<version>` \- Collection version
   - `<nexus_repository_url>` \- Nexus hosted repository URL

Example:

```
   curl -X PUT \
     -u admin:admin123 \
     -T ./myorg-mycollection-1.0.0.tar.gz \
     "http://nexus-host:8081/repository/ansible-hosted/api/v3/plugin/ansible/content/published/collections/artifacts/myorg-mycollection-1.0.0.tar.gz"
   ```

## Install Ansible Galaxy Collections

After configuring Ansible Galaxy to use Nexus, install collections using a requirements file.

```
   ansible-galaxy collection install -r <requirements_file>
   ```
   Where `<requirements_file>` is the path to requirements file.

Example:

```
   ansible-galaxy collection install -r requirements.yml
   ```

You can also install a collection directly from a Nexus repository.

```
   ansible-galaxy collection install <namespace>.<collection_name> \
     --api-key="<base64_token>" \
     --server="<nexus_repository_url>"
   ```
   Where,
   - `<namespace>` \- Collection namespace
   - `<collection_name>` \- Collection name
   - `<base64_token>` \- Base64-encoded `username:password` value
   - `<nexus_repository_url>` \- Nexus repository URL

Example:

```
   ansible-galaxy collection install community.general \
     --api-key="$(echo -n 'admin:admin123' | base64)" \
     --server="http://nexus-host:8081/repository/ansible-proxy/"
   ```

## Download Ansible Galaxy Collections

Download a specific collection version by using `curl` command directly.

```
   curl -O \
     -u <username>:<password> \
     "<nexus_repository_url>/api/v3/plugin/ansible/content/published/collections/artifacts/<namespace>-<collection_name>-<version>.tar.gz"
   ```
   Where,
   - `<username>` \- Your Nexus Repository username
   - `<password>` \- Your Nexus Repository password
   - `<nexus_repository_url>` \- Nexus hosted repository URL
   - `<namespace>` \- Collection namespace
   - `<collection_name>` \- Collection name
   - `<version>` \- Collection version

Example:

```
   curl -O \
     -u admin:admin123 \
     "http://nexus-host:8081/repository/ansible-hosted/api/v3/plugin/ansible/content/published/collections/artifacts/myorg-mycollection-1.0.0.tar.gz"
   ```
