ansible/
lib.rs

1//! # Ansible-rs
2//!
3//! A modern, type-safe Rust wrapper library for Ansible command-line tools.
4//!
5//! This library provides a safe, ergonomic interface for executing Ansible commands,
6//! playbooks, and managing Ansible configurations from Rust applications. It supports
7//! all major Ansible tools including `ansible`, `ansible-playbook`, `ansible-vault`,
8//! `ansible-config`, and `ansible-inventory`.
9//!
10//! ## Features
11//!
12//! - **πŸ”’ Type-safe** - Leverages Rust's type system to prevent common configuration errors
13//! - **πŸš€ Modern API** - Uses builder patterns and fluent interfaces for ergonomic usage
14//! - **πŸ›‘οΈ Comprehensive error handling** - Detailed error types for different failure modes
15//! - **⚑ Memory efficient** - Optimized for minimal allocations and clones
16//! - **πŸ”§ Smart build system** - Automatic Ansible detection and installation during build
17//! - **🌍 Cross-platform** - Supports Linux, macOS, and BSD systems
18//! - **πŸ“š Rust 2024 edition** - Uses the latest Rust features and idioms
19//! - **πŸ§ͺ 100% test coverage** - Comprehensive test suite with property-based testing
20//!
21//! ## System Requirements
22//!
23//! - **Supported platforms**: Linux, macOS, FreeBSD, OpenBSD, NetBSD
24//! - **Ansible**: Version 2.9 or higher (automatically detected and installed)
25//! - **Python**: Version 3.6 or higher (required by Ansible)
26//!
27//! The build script will automatically detect and attempt to install Ansible if not present.
28//! See the [installation guide](https://docs.ansible.com/ansible/latest/installation_guide/) for manual installation.
29//!
30//! ## Quick Start
31//!
32//! ### Basic Ansible Commands
33//!
34//! ```rust,no_run
35//! use ansible::{Ansible, Module};
36//!
37//! let mut ansible = Ansible::default();
38//! ansible
39//!     .set_system_envs()
40//!     .filter_envs(["HOME", "PATH"])
41//!     .add_host("all")
42//!     .set_inventory("./hosts");
43//!
44//! // Execute a ping
45//! let result = ansible.ping()?;
46//! println!("Ping result: {}", result);
47//!
48//! // Execute a shell command
49//! let result = ansible.shell("uptime")?;
50//! println!("Uptime: {}", result);
51//! # Ok::<(), ansible::AnsibleError>(())
52//! ```
53//!
54//! ### Playbook Execution
55//!
56//! ```rust,no_run
57//! use ansible::{Playbook, Play};
58//!
59//! let mut playbook = Playbook::default();
60//! playbook.set_inventory("./hosts");
61//!
62//! // Run from file
63//! let result = playbook.run(Play::from_file("site.yml"))?;
64//! println!("Playbook result: {}", result);
65//!
66//! // Run from string content
67//! let yaml_content = r#"
68//! ---
69//! - hosts: all
70//!   tasks:
71//!     - name: Ensure nginx is installed
72//!       package:
73//!         name: nginx
74//!         state: present
75//! "#;
76//! let result = playbook.run(Play::from_content(yaml_content))?;
77//! # Ok::<(), ansible::AnsibleError>(())
78//! ```
79//!
80//! ### Ansible Vault Operations
81//!
82//! Manage encrypted files and strings with Ansible Vault:
83//!
84//! ```rust,no_run
85//! use ansible::AnsibleVault;
86//!
87//! let mut vault = AnsibleVault::new();
88//! vault.set_vault_password_file("vault_pass.txt");
89//!
90//! // Encrypt a file
91//! vault.encrypt("secrets.yml")?;
92//!
93//! // Decrypt a file
94//! vault.decrypt("secrets.yml")?;
95//!
96//! // Encrypt a string
97//! let encrypted = vault.encrypt_string("my_secret_password")?;
98//! println!("Encrypted: {}", encrypted);
99//! # Ok::<(), ansible::AnsibleError>(())
100//! ```
101//!
102//! ### Configuration Management
103//!
104//! Query and manage Ansible configuration:
105//!
106//! ```rust,no_run
107//! use ansible::{AnsibleConfig, ConfigFormat, PluginType};
108//!
109//! let mut config = AnsibleConfig::new();
110//!
111//! // List all configuration options
112//! let config_list = config.list()?;
113//! println!("Configuration: {}", config_list);
114//!
115//! // Dump configuration in JSON format
116//! config.set_format(ConfigFormat::Json);
117//! let config_dump = config.dump()?;
118//! println!("Config dump: {}", config_dump);
119//!
120//! // List specific plugin types
121//! config.set_plugin_type(PluginType::Callback);
122//! let callbacks = config.list()?;
123//! # Ok::<(), ansible::AnsibleError>(())
124//! ```
125//!
126//! ### Inventory Management
127//!
128//! Parse and query Ansible inventories:
129//!
130//! ```rust,no_run
131//! use ansible::{AnsibleInventory, InventoryFormat};
132//!
133//! let mut inventory = AnsibleInventory::new();
134//! inventory
135//!     .set_inventory("hosts.yml")
136//!     .set_format(InventoryFormat::Json);
137//!
138//! // List all hosts
139//! let hosts = inventory.list()?;
140//! println!("Hosts: {}", hosts);
141//!
142//! // Get specific host information
143//! let host_info = inventory.host("web01")?;
144//! println!("Host info: {}", host_info);
145//!
146//! // Parse inventory data
147//! let inventory_data = inventory.parse_inventory_data()?;
148//! for (group_name, group) in &inventory_data.groups {
149//!     println!("Group {}: {} hosts", group_name, group.hosts.len());
150//! }
151//! # Ok::<(), ansible::AnsibleError>(())
152//! ```
153//!
154//! ### System Requirements Validation
155//!
156//! Check system compatibility and Ansible installation:
157//!
158//! ```rust,no_run
159//! use ansible::{validate_system, get_system_info, PlatformValidator};
160//!
161//! // Quick validation
162//! validate_system()?;
163//!
164//! // Detailed system information
165//! let system_info = get_system_info()?;
166//! println!("{}", system_info);
167//!
168//! if system_info.is_fully_supported() {
169//!     println!("All Ansible features are available!");
170//! } else {
171//!     println!("Missing features: {:?}", system_info.missing_features());
172//! }
173//!
174//! // Check individual components
175//! if PlatformValidator::is_platform_supported() {
176//!     println!("Platform is supported");
177//! }
178//! # Ok::<(), ansible::AnsibleError>(())
179//! ```
180//!
181//! ## Error Handling
182//!
183//! The library provides comprehensive error handling with detailed error types:
184//!
185//! ```rust,no_run
186//! use ansible::{Ansible, AnsibleError};
187//!
188//! let result = Ansible::default().ping();
189//! match result {
190//!     Ok(output) => println!("Success: {}", output),
191//!     Err(AnsibleError::CommandFailed { message, exit_code, stdout, stderr }) => {
192//!         eprintln!("Command failed: {}", message);
193//!         if let Some(code) = exit_code {
194//!             eprintln!("Exit code: {}", code);
195//!         }
196//!         if let Some(stderr) = stderr {
197//!             eprintln!("Error: {}", stderr);
198//!         }
199//!     }
200//!     Err(AnsibleError::UnsupportedPlatform(msg)) => {
201//!         eprintln!("Platform not supported: {}", msg);
202//!     }
203//!     Err(e) => eprintln!("Other error: {}", e),
204//! }
205//! ```
206//!
207//! ## Environment Variables
208//!
209//! Control build-time behavior with environment variables:
210//!
211//! - `SKIP_ANSIBLE_CHECK=1` - Skip Ansible installation check during build
212//! - `ANSIBLE_AUTO_INSTALL=true` - Enable automatic Ansible installation
213//! - `CI=1` - Disable interactive installation prompts in CI environments
214//! - `DOCS_RS=1` - Skip all checks when building documentation
215//!
216//! ## Module Organization
217//!
218//! The library is organized into several modules:
219//!
220//! - Core Ansible command execution and module management
221//! - Ansible playbook execution and management
222//! - [`vault`] - Ansible Vault encryption and decryption operations
223//! - [`config`] - Ansible configuration querying and management
224//! - [`inventory`] - Inventory parsing and host management
225//! - [`platform`] - System requirements validation and platform detection
226//! - [`errors`] - Comprehensive error types and handling
227//! - [`command_config`] - Low-level command configuration utilities
228//!
229//! ## Examples
230//!
231//! See the `examples/` directory for complete working examples:
232//!
233//! - `examples/basic.rs` - Basic Ansible commands
234//! - `examples/playbook.rs` - Playbook execution
235//! - `examples/new_features_demo.rs` - Vault, config, and inventory features
236//! - `examples/system_check.rs` - System requirements validation
237//! - `examples/test_build.rs` - Build script functionality testing
238
239mod ansible;
240pub mod command_config;
241pub mod config;
242pub mod errors;
243pub mod inventory;
244pub mod platform;
245mod playbook;
246pub mod vault;
247
248// εΌ‚ζ­₯ζ”―ζŒζ¨‘ε—οΌˆε―ι€‰οΌ‰
249#[cfg(feature = "async")]
250pub mod async_support;
251
252// Re-export main types for convenience
253pub use ansible::{
254    Ansible, Module,
255    PackageState, ServiceState, FileState, UserState, GroupState
256};
257pub use playbook::{Playbook, Play};
258pub use errors::{AnsibleError, Result};
259pub use vault::{AnsibleVault, VaultError};
260pub use config::{AnsibleConfig, ConfigFormat, PluginType};
261pub use inventory::{AnsibleInventory, InventoryFormat, InventoryData, InventoryGroup, InventoryMeta};
262pub use platform::{PlatformValidator, SystemInfo, validate_system, get_system_info};
263
264// εΌ‚ζ­₯η±»εž‹ι‡ζ–°ε―Όε‡ΊοΌˆε―ι€‰οΌ‰
265#[cfg(feature = "async")]
266pub use async_support::{
267    AsyncAnsible, AsyncPlaybook, AsyncPlay, AsyncAnsibleVault,
268    AsyncAnsibleConfig, AsyncAnsibleInventory,
269    AsyncResult, AsyncExecute, IntoAsync, FromAsync
270};