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-playbookcommand)
§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§
Structs§
- App
- Main entry point for the cdk-ansible CLI.
- AppL2
- Main entry point for the cdk-ansible CLI.
- Host
Inventory Vars - Define
inventory_vars()method to a struct for pooling hosts. This method returns a vector ofResult<HostInventoryVars>. Use fromcdk_ansible_macro::AllInventoryVarsGenderive macro. - HostsL2
- Inventory
- Inventory
Child - Inventory
Root - Play
- Play
Optional Values are defined in
PlayOptions - PlayL2
- Play
Options - playbook keywords (play)
- Playbook
- Stack
Name - Task
- Task
Options - playbook keyword (task)
Enums§
- Bool
OrString - A boolean or a string
- Bool
OrString OrVec String - A boolean or a string or a vector of strings
- ExePlay
- Play execution definition
- ExePlay
L2 - Define a relationship between
PlayL2 - ExePlaybook
- Playbook execution definition for deployment
- IntOr
String - i64 or string
- LEP
- Enum to define the relationship between
LazyPlayL2 - Lazy
ExePlay L2 - Enum to define the relationship between
LazyPlayL2 - OptU
- Option for an unset value
- String
OrMap - String
OrPath - String or Path
- String
OrVec - String
OrVec String - A string or a vector of strings
Traits§
- Host
Inventory Vars Generator - This trait should be implemented by each host to generate inventory variables.
cdk_ansible_macro::AllInventoryVarsGenderive macro will use this trait to generate inventory variables. - Lazy
Play L2 - Define play as lazy
- Stack
- 副作用の無いコードを書くこと
- StackL2
- 副作用の無いコードを書くこと
- Task
Module - 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§
Attribute Macros§
- attr_
hidden - attribute_
env_ vars_ metadata - This attribute is used to generate environment variables metadata for [
cdk_ansible_static::EnvVars].