Ansibe module to encrypt a file using AES and RSA
Download or clone this repository and copy the module in ansible/plugins/modules
under your ansible's module directory.
Once copied, you can use this module in you plays like this:
- name: Encrypt file
file_crypt:
src: "{{ file_to_encrypt }}"
op: encrypt
rsa_key_raw: "{{ lookup('file', rsa_keys_dir + '/public_key.pem') }}"
src
: description: - Source file to encrypt or decrypt required: truedest
: description: - Optional destination path. Default is source path with a '.crypt' preffix. required: falseop
: description: - "encrypt" / "decrypt". required: truerm_src
: description: - If true, this module will delete the source file once the operation is finished. Default is false required: falsersa_key_raw
: description: - Raw public/private key to encrypt/decrypt the file. required: truersa_key_path
: description: - Path to the public/private key to encrypt/decrypt the file. required: true
This module works following this steps. To encrypt
a file:
- Loads an RSA key via path or raw.
- Generates a random AES key .
- Encrypts the file using AES algorithm.
- Encrypts AES key using RSA public key.
- Packs both the file encrypted and the key into a
tgz
file. - If rm_src option is
True
, this module removes the original file.
To decrypt
a file:
- Unpacks the
tgz
file. - Decrypts the AES key file using RSA private key.
- Decrypts the file using AES algorithm.
- If rm_src option is
True
, this module removes the packedtgz
file.
This module requires pycryptodome
installed to encrypt/decrypt.