Ansible-rs
A modern Rust wrapper library for Ansible command-line tools, built with Rust 2024 edition.
Features
- Type-safe - Leverages Rust's type system to prevent common configuration errors
- Modern API - Uses builder patterns and fluent interfaces
- Sync & Async Support - Full support for both synchronous and asynchronous operations
- Concurrent Execution - Run multiple Ansible commands concurrently with async/await
- Comprehensive error handling - Detailed error types for different failure modes
- Memory efficient - Optimized for minimal allocations and clones
- Rust 2024 edition - Uses the latest Rust features and idioms
Quick Start
Add this to your Cargo.toml:
[]
= "1.0.0"
# For async support
= { = "1.0.0", = ["async"] }
Synchronous Usage
use ;
Asynchronous Usage
use ;
async
Concurrent Operations
use AsyncAnsible;
use try_join;
async
Playbook Execution
Synchronous Playbooks
use ;
Asynchronous Playbooks
use ;
async
Sync to Async Conversion
use ;
async
Async Features
Enabling Async Support
Add the async feature to your Cargo.toml:
[]
= { = "1.0.0", = ["async"] }
= { = "1.0", = ["full"] }
Available Async Types
- AsyncAnsible - Asynchronous Ansible command execution
- AsyncPlaybook - Asynchronous Playbook execution
- AsyncAnsibleVault - Asynchronous Vault operations
- AsyncAnsibleConfig - Asynchronous configuration management
- AsyncAnsibleInventory - Asynchronous inventory management
Conversion Traits
- IntoAsync - Convert sync types to async
- FromAsync - Convert async types to sync
- AsyncExecute - Unified async execution interface
Performance Benefits
- Direct async implementation - No thread pool overhead
- Concurrent execution - Run multiple operations simultaneously
- Non-blocking I/O - Doesn't block the event loop
- Memory efficient - Optimized async operations
Examples
Run the async demo:
Run async tests:
Testing
This project includes a Docker-based testing environment with multiple target systems.
Prerequisites
- Docker and Docker Compose
- Ansible installed on your system
- Rust 1.85+ (for Rust 2024 edition support)
Running Tests
Using cargo-nextest (Recommended)
Install cargo-nextest:
Run tests with nextest:
# Quick tests
# Full test suite
# CI profile tests
# Use our test script
Traditional Testing
- Start the test environment:
- Or manually:
# Start containers
# Run examples
# Clean up
Test Environment
The test environment includes:
- Ubuntu 22.04 container (port 2222)
- CentOS 8 container (port 2223)
- Alpine Linux container (port 2224)
All containers are configured with:
- SSH access (user: testuser, pass: testpass)
- Python 3 for Ansible
- Sudo access for the test user
API Documentation
Ansible Commands
let mut ansible = default;
// Configuration
ansible.set_system_envs // Use system environment
.filter_envs // Filter specific env vars
.add_host // Add target hosts
.set_inventory // Set inventory file
.arg; // Add custom arguments
// Execute modules
ansible.ping?; // Ping module
ansible.setup?; // Setup/facts module
ansible.shell?; // Shell module
ansible.command?; // Command module
ansible.script?; // Script module
// Custom modules
let module = other;
ansible.run?;
Playbooks
let mut playbook = default;
// Configuration
playbook.set_inventory
.set_output_json // JSON output format
.arg; // Dry run mode
// Execute playbooks
playbook.run?;
playbook.run?;
Error Handling
The library provides comprehensive error types:
use ;
match ansible.ping
License
This project is licensed under the AGPL-3.0 License - see the LICENSE file for details.