#cdk-ansible #ansible #cdk #command

bin+lib cdk-ansible-cli

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

19 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

#587 in Procedural macros

Download history 69/week @ 2025-02-01 528/week @ 2025-02-08 99/week @ 2025-02-15 320/week @ 2025-02-22 184/week @ 2025-03-01 10/week @ 2025-03-08 417/week @ 2025-03-15 108/week @ 2025-03-22 101/week @ 2025-03-29 39/week @ 2025-04-05 1/week @ 2025-04-12 148/week @ 2025-04-19

290 downloads per month

MIT license

100KB
2K SLoC

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
~266K SLoC