cdk-ansible 0.0.5

cdk-ansible is a tool to generate Ansible playbooks from Rust code.
Documentation

CDK Ansible by Rust

This project is under construction.

Crates.io

cdk-ansible is a CDK (Cloud Development Kit) for Ansible, and inspired by AWS CDK.

While Ansible's playbook and inventory files are written in YAML format, managing YAML templating can be challenging. cdk-ansible enables you to generate Ansible files using Rust as a type-safe programming language.

Features

  • cdk-ansible generates Ansible Playbook and Inventory files.

Usage

Init cdk-ansible project

Note: (Future feature) cdk-ansible project to create a new cdk-ansible's template project.

proj-root/
`-- cdk-ansible/
    `-- Cargo.toml          ... workspace cargo
                                define `workspace.dependencies.cdk-ansible`.

Create Ansible Module package for the workspace

cdk-ansible module --output-dir crates/ --module-name ansible.builtin.debug

# If you don't specify `--module-name`, all modules accessible from your ansible environment will be generated.
cdk-ansible module --output-dir crates/

# If you are using uv to manage your ansible project, move to the directory or specify the `--project` option.
uv --project /path/to/your/ansible-project run \
  cdk-ansible module --output-dir crates/ --module-name ansible.builtin.debug
proj-root/
`-- cdk-ansible/
  |-- Cargo.toml
  `-- crates/
      `-- cdkam_ansible/  ... auto-generated by `cdk-ansible module` command
          |-- Cargo.toml
          `-- src/
              |-- lib.rs
              |-- m/ansible/builtin/debug.rs
              `-- ...

Define your app

your-app project should be like simple-sample.

proj-root/
`-- cdk-ansible/
  `-- crates/
      |-- cdkam_ansible/
      `-- your-app/       ... Implement `cdk_ansible::Synthesizer` and call `cdk_ansible::run`

Synthesize Ansible files

cd cdk-ansible
cargo run --package your-app -- synth --output-dir ../ansible
proj-root/
|-- cdk-ansible/
`-- ansible/              ... Your ansible project
    |-- inventory/        ... auto-generated by `cdk-ansible synth` command
    |-- playbooks/        ... auto-generated by `cdk-ansible synth` command
    |-- ...
    `-- pyproject.toml

Because synth subcommand generates 'json' files, not yaml yet, you need to convert them to yaml manually.

cd ansible
find playbooks inventory -name "*.json" \
  | xargs -I{} bash -c \
    'set -eu; \
    filepath_json={}; \
    filepath_yaml="$${filepath_json%.json}.yaml"; \
    yq -p json -o yaml "$${filepath_json}" > "$${filepath_yaml}"'