Struct AnsibleVault

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

Ansible Vault encryption and decryption utility.

The AnsibleVault struct provides a comprehensive interface for managing encrypted files and strings using Ansible Vault. It supports all major vault operations including encryption, decryption, viewing, editing, and rekeying.

§Examples

§Basic File Operations

use ansible::AnsibleVault;

let mut vault = AnsibleVault::new();
vault.set_vault_password_file("vault_pass.txt");

// Encrypt a file
vault.encrypt("secrets.yml")?;

// Decrypt a file
vault.decrypt("secrets.yml")?;

// View encrypted content
let content = vault.view("secrets.yml")?;
println!("Content: {}", content);

§String Encryption

use ansible::AnsibleVault;

let mut vault = AnsibleVault::new();
vault.set_vault_password_file("vault_pass.txt");

// Encrypt a string
let encrypted = vault.encrypt_string("my_secret_password")?;
println!("Encrypted: {}", encrypted);

§Multiple Vault IDs

use ansible::AnsibleVault;

let mut vault = AnsibleVault::new();
vault.set_vault_id("prod@vault_pass.txt");

// Operations will use the specified vault ID
vault.encrypt("production_secrets.yml")?;

§Rekeying Files

use ansible::AnsibleVault;

let mut vault = AnsibleVault::new();
vault
    .set_vault_password_file("old_pass.txt")
    .set_new_vault_password_file("new_pass.txt");

// Change the encryption key
vault.rekey("secrets.yml")?;

Implementations§

Source§

impl AnsibleVault

Source

pub fn new() -> Self

Create a new AnsibleVault instance

Source

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

Set vault ID for encryption/decryption

Source

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

Set vault password file

Source

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

Set new vault password file for rekeying operations

Source

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

Add a custom argument

Source

pub fn args<I, S>(&mut self, args: I) -> &mut Self
where I: IntoIterator<Item = S>, S: Into<String>,

Add multiple arguments

Source

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

Set environment variables from the system

Source

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

Add an environment variable

Source

pub fn create(&self, file_path: impl Into<String>) -> Result<String>

Create and encrypt a new file

Source

pub fn encrypt(&self, file_path: impl Into<String>) -> Result<String>

Encrypt an existing file

Source

pub fn decrypt(&self, file_path: impl Into<String>) -> Result<String>

Decrypt a file

Source

pub fn view(&self, file_path: impl Into<String>) -> Result<String>

Decrypt and view a file without modifying it

Source

pub fn edit(&self, file_path: impl Into<String>) -> Result<String>

Edit an encrypted file

Source

pub fn rekey(&self, file_path: impl Into<String>) -> Result<String>

Re-encrypt a file with a new password

Source

pub fn encrypt_string( &self, string_to_encrypt: impl Into<String>, ) -> Result<String>

Encrypt a string and output it in a format suitable for inclusion in YAML

Source

pub fn encrypt_string_with_name( &self, string_to_encrypt: impl Into<String>, var_name: impl Into<String>, ) -> Result<String>

Encrypt a string with a variable name

Source

pub fn encrypt_string_prompt(&self) -> Result<String>

Encrypt a string and prompt for input

Source

pub fn encrypt_string_stdin( &self, stdin_name: impl Into<String>, ) -> Result<String>

Encrypt a string with stdin input

Source

pub fn decrypt_to_file( &self, input_file: impl Into<String>, output_file: impl Into<String>, ) -> Result<String>

Decrypt a file to a specific output location

Source

pub fn encrypt_to_file( &self, input_file: impl Into<String>, output_file: impl Into<String>, ) -> Result<String>

Encrypt a file to a specific output location

Source

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

Set the vault ID used for encryption (when multiple vault IDs are available)

Source

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

Ask for vault password interactively

Source

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

Enable verbose output

Source

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

Set multiple levels of verbosity

Source

pub fn get_config(&self) -> &CommandConfig

Get a reference to the command configuration (for testing)

Trait Implementations§

Source§

impl Clone for AnsibleVault

Source§

fn clone(&self) -> AnsibleVault

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 AnsibleVault

Source§

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

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

impl Default for AnsibleVault

Source§

fn default() -> Self

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

impl Display for AnsibleVault

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.