Crate cdk_ansible

Crate cdk_ansible 

Source
Expand description

cdk-ansible is a development framework for defining Ansible applications, similar to AWS CDK.

NOTE: The basic implementation is completely different from AWS CDK.

cdk-ansible provides the following features:

  • Define Ansible Plays and Tasks using Rust code (wraps Ansible YAML files)
  • Enable parallel execution with ease (wraps the ansible-playbook command)

§Example

use ::anyhow::Result;
use ::cdk_ansible::{
    App, Stack, ExeParallel, ExePlay, ExeSequential, ExeSingle, Inventory,
    InventoryChild, InventoryRoot, OptU, Play, PlayOptions,
};

// Define a sample stack
struct SampleStack {
    exe_play: ExePlay,
}

impl SampleStack {
    fn new(host: &str) -> Self {
        // Define a sample play
        let play = Box::new(Play {
            name: "sample".into(),
            hosts: vec![host.to_owned()].into(),
            options: PlayOptions::default(),
            tasks: vec![
                // Add tasks later
            ],
        });

        Self {
            exe_play: ExeSequential(vec![
                ExeSingle(play.clone()),
                ExeSequential(vec![ExeSingle(play.clone()), ExeSingle(play.clone())]),
                ExeParallel(vec![
                    ExeSequential(vec![ExeSingle(play.clone()), ExeSingle(play.clone())]),
                    ExeSingle(play.clone()),
                    ExeParallel(vec![ExeSingle(play.clone()), ExeSingle(play.clone())]),
                ]),
            ]),
        }
    }
}

// Stack should implement the `Stack` trait
impl Stack for SampleStack {
    fn name(&self) -> &str {
        std::any::type_name::<Self>()
            .split("::")
            .last()
            .expect("Failed to get a stack name")
    }

    fn exe_play(&self) -> &ExePlay {
        &self.exe_play
    }
}

fn run() -> Result<()> {
    let mut app = App::new(std::env::args().collect());
    let inventory = Inventory {
        name: "inventory".into(), // generate 'inventory.yaml' file
        root: InventoryRoot {
            all: InventoryChild {
                hosts: OptU::Some([("localhost".into(), None)].into_iter().collect()),
                ..Default::default()
            },
        },
    };

    app.add_inventory(inventory)?;
    app.add_stack(Box::new(SampleStack::new("localhost")))?;

    // app.run()?  // replace `Ok(())` with `app.run()`
    Ok(())
}

fn main() {
    if let Err(e) = run() {
        eprintln!("Error: {e:?}");
        std::process::exit(1);
    }
}

§Tutorial

§Install cdk-ansible-cli

Re-exports§

pub use LazyExePlayL2::Parallel as LEPParallelL2;
pub use LazyExePlayL2::Sequential as LEPSequentialL2;
pub use LazyExePlayL2::Single as LEPSingleL2;
pub use ExePlay::Parallel as ExeParallel;
pub use ExePlay::Sequential as ExeSequential;
pub use ExePlay::Single as ExeSingle;

Modules§

prelude

Structs§

App
Main entry point for the cdk-ansible CLI.
AppL2
Main entry point for the cdk-ansible CLI.
HostInventoryVars
Define inventory_vars() method to a struct for pooling hosts. This method returns a vector of Result<HostInventoryVars>. Use from cdk_ansible_macro::AllInventoryVarsGen derive macro.
HostsL2
Inventory
InventoryChild
InventoryRoot
Play
Play Optional Values are defined in PlayOptions
PlayL2
PlayOptions
playbook keywords (play)
Playbook
StackName
Task
TaskOptions
playbook keyword (task)

Enums§

BoolOrString
A boolean or a string
BoolOrStringOrVecString
A boolean or a string or a vector of strings
ExePlay
Play execution definition
ExePlayL2
Define a relationship between PlayL2
ExePlaybook
Playbook execution definition for deployment
IntOrString
i64 or string
LEP
Enum to define the relationship between LazyPlayL2
LazyExePlayL2
Enum to define the relationship between LazyPlayL2
OptU
Option for an unset value
StringOrMap
StringOrPath
String or Path
StringOrVec
StringOrVecString
A string or a vector of strings

Traits§

HostInventoryVarsGenerator
This trait should be implemented by each host to generate inventory variables. cdk_ansible_macro::AllInventoryVarsGen derive macro will use this trait to generate inventory variables.
LazyPlayL2
Define play as lazy
Stack
副作用の無いコードを書くこと
StackL2
副作用の無いコードを書くこと
TaskModule
Task module trait

Functions§

get_host_inventory_vars
Generate a host inventory vars for ansible inventory Should be used at cdk_ansible_macro::AllInventoryVarsGen
get_host_inventory_vars_rc
Generate a host inventory vars for ansible inventory Should be used at cdk_ansible_macro::AllInventoryVarsGen
get_host_inventory_vars_ref_cell
Generate a host inventory vars for ansible inventory Should be used at cdk_ansible_macro::AllInventoryVarsGen

Type Aliases§

InventoryHosts
InventoryVars

Attribute Macros§

attr_hidden
attribute_env_vars_metadata
This attribute is used to generate environment variables metadata for [cdk_ansible_static::EnvVars].

Derive Macros§

AllInventoryVarsGen
FieldCount