#ansible #generate #cdk #inventory #playbooks #kit #cloud

cdk-ansible

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

7 releases

new 0.0.10 Feb 12, 2025
0.0.9 Feb 12, 2025

#167 in Audio

Download history 343/week @ 2025-02-04 541/week @ 2025-02-11

884 downloads per month

MIT license

49KB
613 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–17MB
~247K SLoC