# Komodo Stack Role

This role automates the creation of stacks in Komodo via its REST API.

## Quick Start

The included `provision_komodo_stacks.yml` playbook automatically discovers all stacks from your `docker-compose/` folder structure and creates them in Komodo:

```bash
ansible-playbook -i inventories/production.yml playbooks/provision_komodo_stacks.yml
```

This will scan for all `compose.yaml` files in the structure: `docker-compose/{server}/{app}/compose.yaml` and create corresponding stacks in Komodo.

## Requirements

- A running Komodo instance with API access
- Valid Komodo API token stored in `vault_komodo_api_token`
- Existing server and repository configured in Komodo

## Role Variables

### Required Variables

- `komodo_stack_name`: Name for the stack (typically the app name, e.g., "changedetection")
- `komodo_server_name`: Name of the server in Komodo where the stack should deploy
- `komodo_repo_name`: Name of the repository in Komodo containing the stack definition
- `komodo_run_directory`: Path within the repository to the compose.yaml file (e.g., `docker-compose/apps-1/changedetection`)

### Optional Variables

- `komodo_host`: Komodo instance URL (default: `https://komodo.{{ domain }}`)
- `komodo_api_token`: API token for authentication (default: `{{ vault_komodo_api_token }}`)

## Example Usage

### Automatic Discovery (Recommended)

Use the provided playbook to automatically discover and create all stacks from your repository:

```bash
ansible-playbook -i inventories/production.yml playbooks/provision_komodo_stacks.yml
```

This scans the `docker-compose/` directory and creates stacks for each app found in the structure:

- `docker-compose/{server}/{app}/compose.yaml`

Example structure:

```
docker-compose/
  apps-1/
    changedetection/
      compose.yaml
    turn/
      compose.yaml
  media-1/
    arr/
      compose.yaml
```

## How It Works

1. Validates all required variables are provided
2. Fetches list of servers from Komodo API and finds the server ID by name
3. Fetches list of repositories from Komodo API and finds the repo ID by name
4. Checks if a stack with the same name already exists
5. Creates the stack if it doesn't exist (skips if it does)

## API Token Setup

Store your Komodo API token in an Ansible vault file:

```yaml
# inventories/group_vars/all/vault.yml
vault_komodo_api_token: 'your-api-token-here'
```

Generate an API token in Komodo: Settings → API Keys → Create New Token

## Notes

- The role is idempotent - it won't create duplicate stacks
- Server and repo must already exist in Komodo before running this role
- Uses retry logic for API calls to handle temporary network issues
