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
impl Playbook
Sourcepub fn set_system_envs(&mut self) -> &mut Self
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();Sourcepub fn filter_envs<T, S>(&mut self, iter: T) -> &mut Self
pub fn filter_envs<T, S>(&mut self, iter: T) -> &mut Self
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"]);Sourcepub fn add_env(
&mut self,
key: impl Into<String>,
value: impl Into<String>,
) -> &mut Self
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");Sourcepub fn arg<S: AsRef<OsStr> + Display>(&mut self, arg: S) -> &mut Self
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");Sourcepub fn args<T, S>(&mut self, args: T) -> &mut Self
pub fn args<T, S>(&mut self, args: T) -> &mut Self
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"]);Sourcepub fn set_inventory(&mut self, s: &str) -> &mut Self
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");Sourcepub fn set_output_json(&mut self) -> &mut Self
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();Sourcepub fn set_verbosity(&mut self, level: u8) -> &mut Self
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); // -vvSourcepub fn add_extra_var(
&mut self,
key: impl Into<String>,
value: impl Into<String>,
) -> &mut Self
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");Sourcepub fn add_tag(&mut self, tag: impl Into<String>) -> &mut Self
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");Sourcepub fn set_check_mode(&mut self, enabled: bool) -> &mut Self
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);Sourcepub fn run(&self, play: Play) -> Result<String>
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))?;