Struct Playbook

Source
pub struct Playbook { /* private fields */ }
Expand description

Ansible playbook executor with comprehensive configuration options.

The Playbook struct provides a fluent interface for configuring and executing Ansible playbooks. It supports all major ansible-playbook command-line options and provides type-safe configuration.

§Examples

§Basic Playbook Execution

use ansible::{Playbook, Play};

let mut playbook = Playbook::default();
playbook.set_inventory("hosts.yml");

// Run from file
let result = playbook.run(Play::from_file("site.yml"))?;
println!("Playbook result: {}", result);

§Advanced Configuration

use ansible::{Playbook, Play};

let mut playbook = Playbook::default();
playbook
    .set_inventory("production")
    .set_verbosity(2)
    .add_extra_var("env", "production")
    .add_extra_var("version", "1.2.3")
    .add_tag("deploy")
    .add_tag("config")
    .set_check_mode(true);

let result = playbook.run(Play::from_file("deploy.yml"))?;

§Environment Configuration

use ansible::Playbook;

let mut playbook = Playbook::default();
playbook
    .set_system_envs()
    .filter_envs(["HOME", "PATH", "USER"])
    .add_env("ANSIBLE_HOST_KEY_CHECKING", "False")
    .add_env("ANSIBLE_STDOUT_CALLBACK", "json");

Implementations§

Source§

impl Playbook

Source

pub fn set_system_envs(&mut self) -> &mut Self

Set environment variables from the current system environment.

This method copies all environment variables from the current process to be passed to the ansible-playbook command.

§Examples
use ansible::Playbook;

let mut playbook = Playbook::default();
playbook.set_system_envs();
Source

pub fn filter_envs<T, S>(&mut self, iter: T) -> &mut Self
where T: IntoIterator<Item = S>, S: AsRef<OsStr> + Display,

Filter environment variables to only include specified keys.

This method is useful for limiting which environment variables are passed to ansible-playbook commands.

§Examples
use ansible::Playbook;

let mut playbook = Playbook::default();
playbook
    .set_system_envs()
    .filter_envs(["HOME", "PATH", "USER"]);
Source

pub fn add_env( &mut self, key: impl Into<String>, value: impl Into<String>, ) -> &mut Self

Add a single environment variable.

This method adds or overwrites an environment variable that will be passed to the ansible-playbook command.

§Examples
use ansible::Playbook;

let mut playbook = Playbook::default();
playbook
    .add_env("ANSIBLE_HOST_KEY_CHECKING", "False")
    .add_env("ANSIBLE_STDOUT_CALLBACK", "json");
Source

pub fn arg<S: AsRef<OsStr> + Display>(&mut self, arg: S) -> &mut Self

Add a single command-line argument.

This method adds a raw command-line argument to the ansible-playbook command.

§Examples
use ansible::Playbook;

let mut playbook = Playbook::default();
playbook
    .arg("--verbose")
    .arg("--check");
Source

pub fn args<T, S>(&mut self, args: T) -> &mut Self
where T: IntoIterator<Item = S>, S: AsRef<OsStr> + Display,

Add multiple command-line arguments.

This method adds multiple raw command-line arguments to the ansible-playbook command.

§Examples
use ansible::Playbook;

let mut playbook = Playbook::default();
playbook.args(["--verbose", "--check", "--diff"]);
Source

pub fn set_inventory(&mut self, s: &str) -> &mut Self

Set the inventory file or directory.

This method specifies the inventory file or directory to use for host and group information.

§Examples
use ansible::Playbook;

let mut playbook = Playbook::default();
playbook.set_inventory("hosts.yml");
playbook.set_inventory("/etc/ansible/hosts");
playbook.set_inventory("production");
Source

pub fn set_output_json(&mut self) -> &mut Self

Configure output to use JSON format.

This method sets environment variables to configure ansible-playbook to output results in JSON format.

§Examples
use ansible::Playbook;

let mut playbook = Playbook::default();
playbook.set_output_json();
Source

pub fn set_verbosity(&mut self, level: u8) -> &mut Self

Set the verbosity level for playbook execution.

This method sets the verbosity level for ansible-playbook output.

§Examples
use ansible::Playbook;

let mut playbook = Playbook::default();
playbook.set_verbosity(2); // -vv
Source

pub fn add_extra_var( &mut self, key: impl Into<String>, value: impl Into<String>, ) -> &mut Self

Add an extra variable for playbook execution.

This method adds extra variables that will be passed to the playbook.

§Examples
use ansible::Playbook;

let mut playbook = Playbook::default();
playbook
    .add_extra_var("env", "production")
    .add_extra_var("version", "1.2.3");
Source

pub fn add_tag(&mut self, tag: impl Into<String>) -> &mut Self

Add a tag to limit playbook execution.

This method adds tags to limit which tasks are executed.

§Examples
use ansible::Playbook;

let mut playbook = Playbook::default();
playbook
    .add_tag("deploy")
    .add_tag("config");
Source

pub fn set_check_mode(&mut self, enabled: bool) -> &mut Self

Enable check mode (dry run).

This method enables check mode for the playbook execution.

§Examples
use ansible::Playbook;

let mut playbook = Playbook::default();
playbook.set_check_mode(true);
Source

pub fn run(&self, play: Play) -> Result<String>

Execute an Ansible playbook.

This method executes an Ansible playbook from either a file or string content. It handles temporary file creation for string content and cleanup afterwards.

§Arguments
  • play - The playbook source (file path or content)
§Returns

Returns the combined stdout and stderr output from the ansible-playbook command.

§Errors

Returns an error if:

  • The playbook file cannot be read or created
  • The ansible-playbook command fails to execute
  • The command returns a non-zero exit code
§Examples
§Running from File
use ansible::{Playbook, Play};

let mut playbook = Playbook::default();
playbook.set_inventory("hosts.yml");

let result = playbook.run(Play::from_file("site.yml"))?;
println!("Playbook result: {}", result);
§Running from Content
use ansible::{Playbook, Play};

let yaml_content = r#"
- hosts: all
  tasks:
    - name: Ensure nginx is installed
      package:
        name: nginx
        state: present
"#;

let mut playbook = Playbook::default();
let result = playbook.run(Play::from_content(yaml_content))?;

Trait Implementations§

Source§

impl Clone for Playbook

Source§

fn clone(&self) -> Playbook

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Playbook

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Playbook

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Playbook

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.