Skip to main content

A CLI tool for managing inventory YAML files

Project description

inventoryctl

inventoryctl is a CLI tool for safely and consistently managing inventory YAML files, designed to bridge the gap between human-edited configurations and automated processes. It ensures valid syntax, consistent formatting, and facilitates bulk operations and format conversions (e.g., to Ansible inventory or SSH config).

Installation

From PyPI

pipx install inventoryctl

Using uv

uv tool install inventoryctl

Features

  • CRUD Operations: Add, update, delete, and get hosts/groups safely.
  • Bulk Sync: Sync hosts from external JSON/YAML sources (e.g., Terraform outputs, cloud APIs) into your inventory.
  • Validation: Ensure your inventory follows the required structure.
  • Rendering: Convert your inventory into Ansible-compatible files or SSH configurations.
  • Formatting: Canonicalize your YAML files.
  • Metadata Support: Track source ownership of resources using _meta tags to safely mix manual and automated entries.

Usage

1. Resources Management

Add a Host

inventoryctl add host <NAME> <INVENTORY_FILE> --group <GROUP> --ansible-host <IP> [OPTIONS]

Options:

  • --var key=value: Set host variables (can be used multiple times).
  • --source <ID>: Mark this host as managed by a specific source (e.g., aws, terraform).
  • --force: Overwrite if exists.
  • --upsert: Update if exists, create if not.

Add a Group

inventoryctl add group <NAME> <INVENTORY_FILE> [--var key=value]

Update a Host

inventoryctl update host <NAME> <INVENTORY_FILE> [OPTIONS]

Options:

  • --group <GROUP>: Specify group (required if hostname is ambiguous).
  • --ansible-host <IP>: Update IP/Hostname.
  • --var key=value: Update or add variables.
  • --unset-var <KEY>: Remove variables.

Delete a Host

inventoryctl delete host <NAME> <INVENTORY_FILE> [--group <GROUP>] [--source <ID>]

Get a Host

inventoryctl get host <NAME> <INVENTORY_FILE>

List Hosts

inventoryctl list hosts <INVENTORY_FILE> [--group <GROUP>] [--source <ID>]

2. Bulk Operations (Sync)

Sync hosts from an external source (JSON/YAML) into a specific group. This is useful for pipelines filling the inventory.

inventoryctl sync hosts <INVENTORY_FILE> --group <GROUP> --source <SOURCE_ID> --input <INPUT_FILE> [--prune]
  • --input: Path to a JSON/YAML file containing a list of host objects.
  • --prune: Remove hosts in the target group/source that are NOT in the input.

Input Format Example:

[
  {
    "name": "web-01",
    "ansible_host": "10.0.0.1",
    "vars": { "region": "us-east-1" }
  }
]

2.1. Batch Operations (JSON-Based CRUD)

For high-performance bulk operations, use the batch command which processes JSON input directly without external dependencies:

Batch Host Operations

# From file
inventoryctl batch host <ACTION> <INVENTORY_FILE> --file hosts.json

# From stdin
cat hosts.json | inventoryctl batch host <ACTION> <INVENTORY_FILE>
echo '$BATCH_JSON' | inventoryctl batch host <ACTION> <INVENTORY_FILE>

Actions: add, update, delete

JSON Format for Hosts:

[
  {
    "hostname": "web-01",
    "ansible_host": "10.0.0.1",
    "ansible_user": "ubuntu",
    "ansible_port": 22,
    "groups": ["webservers", "production"],
    "vars": {
      "http_port": 80,
      "custom_var": "value"
    },
    "source": "api",
    "upsert": true,
    "force": false
  },
  {
    "hostname": "db-01",
    "ansible_host": "10.0.0.5",
    "ansible_user": "postgres",
    "groups": ["databases", "production"],
    "vars": {
      "db_port": 5432
    }
  }
]

Batch Group Operations

inventoryctl batch group <ACTION> <INVENTORY_FILE> --file groups.json

JSON Format for Groups:

[
  {
    "name": "webservers",
    "vars": {
      "http_port": 80,
      "app_env": "production"
    }
  },
  {
    "name": "databases",
    "vars": {
      "db_type": "postgresql"
    }
  }
]

Options:

  • --continue-on-error: Continue processing remaining items if one fails (default: true)
  • --file <path>: Read JSON from file instead of stdin

Exit Codes:

  • 0: All operations succeeded
  • 1: All operations failed
  • 2: Partial success (some operations failed)

3. Validation & Formatting

Validate Inventory Checks structure and integrity.

inventoryctl validate <INVENTORY_FILE>

Format Inventory Canonicalizes the YAML file (sorting, indentation).

inventoryctl format <INVENTORY_FILE>

4. Rendering

Render for Ansible Outputs a clean YAML inventory file compatible with Ansible (strips internal metadata).

inventoryctl render ansible <INVENTORY_FILE>

Render SSH Config Generates an SSH config file based on inventory data (ansible_host, ansible_user, ProxyJump, etc.).

inventoryctl render ssh <INVENTORY_FILE> > ~/.ssh/config.d/inventory_config

Inventory File Structure

inventoryctl manages a YAML file with the following structure:

inventory_groups:
  my-group:
    vars:
      ansible_user: ubuntu
    hosts:
      web-01:
        ansible_host: 192.168.1.10
        _meta:
          source: manual

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

inventoryctl-0.3.0.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

inventoryctl-0.3.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file inventoryctl-0.3.0.tar.gz.

File metadata

  • Download URL: inventoryctl-0.3.0.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for inventoryctl-0.3.0.tar.gz
Algorithm Hash digest
SHA256 20d79f0586e2f94df4ac87a4004f0ea247277ed93da9bfbf1a9ae90e97359a27
MD5 c8864fe003d0c14f2ba9fc1b35642ab4
BLAKE2b-256 7eec02538eaeb980755343e832adb362e35f20708cc60f36d889a0ded55a5e56

See more details on using hashes here.

Provenance

The following attestation bundles were made for inventoryctl-0.3.0.tar.gz:

Publisher: pypi-publish.yml on aabichou/inventoryctl

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file inventoryctl-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: inventoryctl-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for inventoryctl-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 10a666abb524839541625195b1c8f82eb77eab2107080305c545d52461484755
MD5 7891f8b05f4d5d1686632733bfb4e621
BLAKE2b-256 81746c14997b1f185519f71faf0a78d395277b7803cfa9c214d50b05f5de64da

See more details on using hashes here.

Provenance

The following attestation bundles were made for inventoryctl-0.3.0-py3-none-any.whl:

Publisher: pypi-publish.yml on aabichou/inventoryctl

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page