#cdk-ansible #ansible #cdk

cdk-ansible

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

18 releases

Uses new Rust 2024

new 0.1.2 Apr 25, 2025
0.1.1 Apr 24, 2025
0.0.19 Apr 3, 2025
0.0.18 Mar 21, 2025
0.0.13 Feb 23, 2025

#236 in Procedural macros

Download history 142/week @ 2025-02-02 612/week @ 2025-02-09 72/week @ 2025-02-16 341/week @ 2025-02-23 185/week @ 2025-03-02 142/week @ 2025-03-09 332/week @ 2025-03-16 73/week @ 2025-03-23 101/week @ 2025-03-30 23/week @ 2025-04-06 2/week @ 2025-04-13 271/week @ 2025-04-20

398 downloads per month
Used in cdkam

MIT license

55KB
840 lines

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 crate helps you to generate Ansible Playbook and Inventory files.
  • cdk-ansible-cli (cdk-ansible command) generates Rust packages for existing Ansible modules.

Install Command

binstall

binstall

cargo binstall cdk-ansible-cli

shell

See the latest release page and run the command like below.

# ex) vX.Y.Z
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/pollenjp/cdk-ansible/releases/download/vX.Y.Z/cdk-ansible-cli-installer.sh | sh

cargo install

cargo install cdk-ansible-cli

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

# specify module name like below.
#
# '<namespace>.<collection>.<module>' only generates the specified module.
cdk-ansible module --output-dir crates/ --module-name ansible.builtin.debug
# '<namespace>.<collection>' generates all modules in the collection.
cdk-ansible module --output-dir crates/ --module-name-regex 'ansible\.builtin\..*'
# '<namespace>' generates all modules in the namespace.
cdk-ansible module --output-dir crates/ --module-name-regex 'ansible\..*'
# If you don't specify `--module-name` or `--module-name-regex`,
# all modules accessible from your ansible environment will be generated.
# (This is the same as `--module-name-regex '*'`)
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}"'

Dependencies

~7–18MB
~258K SLoC