diff --git a/README.md b/README.md index 8ffea4e..b2d4133 100644 --- a/README.md +++ b/README.md @@ -28,3 +28,12 @@ or hook ### Dictionary for auto-complete - Ansible module key dictionary + +### Ansible Vault support + +Set up a password in ~/vault_pass + +Bind keys: + + (global-set-key (kbd "C-c b") 'ansible::decrypt-buffer) + (global-set-key (kbd "C-c g") 'ansible::encrypt-buffer) diff --git a/ansible.el b/ansible.el index f679c25..6d14471 100644 --- a/ansible.el +++ b/ansible.el @@ -67,6 +67,11 @@ :type 'integer :group 'ansible) +(defcustom ansible::vault-password-file "~/.vault_pass.txt" + "Filename containing ansible-vault password" + :type 'file + :group 'ansible) + ;;;###autoload (defvar ansible::key-map (make-sparse-keymap) @@ -121,6 +126,36 @@ (f-files ansible::root-path (lambda (file) (s-matches? ".yml" (f-long file))) t)) nil)) +(defun ansible::vault-buffer (mode) + (let ((str (buffer-substring-no-properties (point-min) (point-max)))) + (delete-region (point-min) (point-max)) + (insert (ansible::vault mode str)))) + +(defun ansible::get-string-from-file (file-path) + "Return FILE-PATH's file content." + (with-temp-buffer + (insert-file-contents file-path) + (buffer-string))) + +(defun ansible::vault (mode str) + (let ((temp-file (make-temp-file "ansible-vault-ansible"))) + (write-region str nil temp-file 'append) + (let* ((command (format "ansible-vault %s --vault-password-file=%s %s" mode ansible::vault-password-file temp-file)) + (status (shell-command command)) + (output (ansible::get-string-from-file temp-file))) + (if (/= status 0) + (error "Error in ansible-vault running %s!" command) + (delete-file temp-file) + output)))) + +(defun ansible::decrypt-buffer () + (interactive) + (ansible::vault-buffer "decrypt")) + +(defun ansible::encrypt-buffer () + (interactive) + (ansible::vault-buffer "encrypt")) + (defconst ansible::dir (file-name-directory (or load-file-name buffer-file-name)))