Skip to content

Commit

Permalink
Merge pull request #7 from vinceatbluelabs/ansible_vault
Browse files Browse the repository at this point in the history
Add ansible-vault support
  • Loading branch information
k1LoW committed Nov 24, 2015
2 parents ccb7456 + 44ef568 commit e9b9431
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
35 changes: 35 additions & 0 deletions ansible.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)))

Expand Down

0 comments on commit e9b9431

Please sign in to comment.