Ansible

Polycrate Ansible integration: automatic snapshot variables, central inventory.yml, ansible-polycrate callback, auto-KUBECONFIG, and Docker Compose v2.

Ansible is the primary execution engine for Polycrate actions. The Polycrate Container ships with Ansible pre-installed — including kubernetes.core, community.docker, and community.general — and injects the workspace snapshot directly as top-level variables via a dedicated Ansible vars plugin.

Automatic Snapshot Variables

Before every polycrate run <block> <action>, Polycrate exports the resolved Snapshot and makes it automatically available to Ansible:

Variable Contents
workspace Full workspace configuration (workspace.name, workspace.blocks, workspace.secrets)
block Current block after inheritance and secrets.poly merge (block.name, block.config, block.artifacts.path, block.secrets)
action Active action (action.name, action.config including --set overrides)
transaction.id Unique transaction ID of the current run

Example: From Block to Rendered Target State

Template Block (blocks/traefik/block.poly):

name: traefik
version: 1.0.0

config:
  image: "traefik:v3.2"
  letsencrypt:
    email: "ops@acme.example"

actions:
  - name: install
    playbook: install.yml

Workspace Override (workspace.poly):

name: acme-production-1
organization: acme

blocks:
  - name: traefik
    config:
      image: "traefik:v3.3"
      letsencrypt:
        email: "certs@acme.example"

Playbook with Docker Compose v2 (blocks/traefik/install.yml):

- name: Deploy Traefik via Docker Compose v2
  hosts: edge
  become: true
  tasks:
    - name: Create target directory
      ansible.builtin.file:
        path: "/polycrate/{{ block.name }}"
        state: directory
        mode: "0755"

    - name: Render Compose file from Jinja2 template
      ansible.builtin.template:
        src: templates/compose.yml.j2
        dest: "/polycrate/{{ block.name }}/compose.yml"
        mode: "0644"

    - name: Start stack
      community.docker.docker_compose_v2:
        project_src: "/polycrate/{{ block.name }}"
        state: present
        remove_orphans: true

Inside the Jinja2 template, reference {{ block.config.image }} (here traefik:v3.3) and {{ block.config.letsencrypt.email }} directly.

Central Ansible Inventory (inventory.yml)

Place the Ansible inventory as a YAML file named inventory.yml in the workspace root. Polycrate automatically sets ANSIBLE_INVENTORY to this file — no inventory.from block is required:

# inventory.yml
all:
  vars:
    ansible_user: deploy
    ansible_ssh_port: 22
    ansible_python_interpreter: /usr/bin/python3
  hosts:
    edge-1:
      ansible_host: 192.0.2.10
    edge-2:
      ansible_host: 192.0.2.11
  children:
    edge:
      hosts:
        edge-1: {}
        edge-2: {}

Kubernetes and Helm in Playbooks (Auto-KUBECONFIG)

For Kubernetes blocks, Polycrate automatically sets KUBECONFIG and K8S_AUTH_KUBECONFIG inside the container to artifacts/secrets/kubeconfig.yml. Playbooks run on hosts: localhost and do not require explicit kubeconfig: parameters:

# blocks/my-app/install.yml
- name: Deploy Kubernetes app via Helm
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Ensure namespace exists
      kubernetes.core.k8s:
        state: present
        definition:
          apiVersion: v1
          kind: Namespace
          metadata:
            name: "{{ block.config.namespace }}"

    - name: Install or upgrade Helm chart
      kubernetes.core.helm:
        name: "{{ block.name }}"
        chart_ref: "{{ block.config.chart.ref }}"
        chart_version: "{{ block.config.chart.version }}"
        release_namespace: "{{ block.config.namespace }}"
        values: "{{ block.config.values | default({}) }}"
        wait: true

Secrets and Artifacts in Playbooks

Sensitive values such as database passwords do not belong as Ansible Vault references in block.poly, but in the encrypted secrets.poly (automatically merged into block.config before execution) or as files under artifacts/secrets/<block>/:

# secrets.poly (encrypted as secrets.poly.age)
blocks:
  - name: my-app
    config:
      database:
        password: "example-secret-password"
# In the playbook: use securely with no_log: true
- name: Create Kubernetes Secret
  kubernetes.core.k8s:
    state: present
    namespace: "{{ block.config.namespace }}"
    definition:
      apiVersion: v1
      kind: Secret
      metadata:
        name: "{{ block.name }}-db"
      stringData:
        password: "{{ block.config.database.password }}"
  no_log: true

For persistent output files, always use {{ block.artifacts.path }} (automatically resolved to /workspace/artifacts/blocks/<block> or the local host path).

Ansible Output Configuration

Inside the Polycrate container, the compact callback plugin ansible-polycrate is active by default (one line per host and task, full context on errors).

Persist in ~/.polycrate/polycrate.yml

# ~/.polycrate/polycrate.yml
ansible:
  stdout_callback: ansible-polycrate   # or ansible.builtin.default, ansible.builtin.dense
  callback_result_format: json         # json (compact) | yaml (verbose)
  display_ok_hosts: true
  display_skipped_hosts: false

Override per Invocation via CLI Flags

# Verbose YAML output for debugging
polycrate run my-block install --ansible-stdout-callback ansible.builtin.default --ansible-callback-result-format yaml

# Very compact output for large inventories
polycrate run my-block install --ansible-stdout-callback ansible.builtin.dense

# Prompt interactively for sudo password (ANSIBLE_BECOME_PASSWORD)
polycrate run my-block install -K

Best Practices

  1. Idempotency: Use declarative Ansible modules (kubernetes.core.k8s, ansible.builtin.template, community.docker.docker_compose_v2) instead of shell: scripts.
  2. No manual --kubeconfig: Rely on the CLI's automatic KUBECONFIG / K8S_AUTH_KUBECONFIG injection.
  3. Protect sensitive tasks: Mark tasks handling passwords or tokens with no_log: true.
  4. Pin additional collections: Install required Ansible Galaxy collections persistently via Dockerfile.poly.

See Also