ansible: add support for company-ansible and ansible-vault

New key bindings to encrypt decrypt ansible-vault encrypted files.
New layer variable ansible-auto-encrypt-descrypt to control seamless edition
of encrypted files.
This commit is contained in:
syl20bnr 2016-12-16 08:50:21 -05:00
parent 628562f0f7
commit 7b9143832d
4 changed files with 85 additions and 14 deletions

View file

@ -5,6 +5,10 @@
* Table of Contents :TOC_4_gh:noexport:
- [[#description][Description]]
- [[#install][Install]]
- [[#configuration][Configuration]]
- [[#ansible-vault][ansible-vault]]
- [[#password][Password]]
- [[#automatic-encryption-and-descryption][Automatic encryption and descryption]]
- [[#key-bindings][Key bindings]]
* Description
@ -15,8 +19,35 @@ To use this configuration layer, add it to your =~/.spacemacs=. You will need to
add =ansible= to the existing =dotspacemacs-configuration-layers= list in this
file.
* Configuration
** ansible-vault
*** Password
To use =ansible-vault= you have to provide the path to a file containing the
password to use somewhere in you =dotspacemacs/user-config= function.
For instance:
#+BEGIN_SRC emacs-lisp
(setq ansible::vault-password-file "path/to/pwd/file")
#+END_SRC
The default value is the ansible-vault default value: =~/.vault_pass.txt=.
*** Automatic encryption and descryption
This layer comes preconfigured with automatic encryption/decryption of
encrypted files using =ansible-vault= so it is possible to edit seamlessly
any encrypted files.
If you want to disable this feature then set the layer variable
=ansible-auto-encrypt-descrypt= to =nil=.
#+BEGIN_SRC emacs-lisp
(ansible :variables ansible-auto-encrypt-descrypt t)
#+END_SRC
* Key bindings
| Key Binding | Description |
|-------------+------------------------------------------|
| ~SPC m b d~ | encrypt the buffer using =ansible-vault= |
| ~SPC m b e~ | decrypt the buffer using =ansible-vault= |
| ~SPC m h a~ | looks up documentation using [[https://github.com/lunaryorn/ansible-doc.el][ansible-doc]] |

View file

@ -9,7 +9,14 @@
;;
;;; License: GPLv3
;; variables
(defvar ansible-auto-encrypt-descrypt t
"Set it to non-nil to seamlessly edit `ansible-vault' encrypted files.
If non-nil then encrypted files are automatically decrypted when opened and
encrypted when saved.")
;; detect filenames compatible with Ansible's recommended layout.
;; http://docs.ansible.com/playbooks_best_practices.html#directory-layout
(setq ansible/ansible-filename-re
"\\(site\.yml\\|roles/.+\.yml\\|group_vars/.+\\|host_vars/.+\\)")
(setq spacemacs--ansible-filename-re
".*\\(main\.yml\\|site\.yml\\|encrypted\.yml\\|roles/.+\.yml\\|group_vars/.+\\|host_vars/.+\\)")

View file

@ -8,12 +8,20 @@
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defun ansible/ansible-should-enable? ()
(defun spacemacs//ansible-should-enable? ()
(and (stringp buffer-file-name)
(string-match ansible/ansible-filename-re buffer-file-name)))
(string-match spacemacs--ansible-filename-re buffer-file-name)))
(defun ansible/ansible-maybe-enable ()
(when (ansible/ansible-should-enable?) (ansible 1)))
(defun spacemacs/ansible-maybe-enable ()
(when (spacemacs//ansible-should-enable?)
(ansible 1)))
(defun ansible/ansible-doc-maybe-enable ()
(when (ansible/ansible-should-enable?) (ansible-doc-mode 1)))
(defun spacemacs/ansible-company-maybe-enable ()
"Add the ansible company backend only for when ansible mode is active."
(when (spacemacs//ansible-should-enable?)
(add-to-list 'company-backends 'company-ansible)))
(defun spacemacs/ansible-doc-maybe-enable ()
(when (spacemacs//ansible-should-enable?)
(ansible-doc-mode 1)))

View file

@ -12,17 +12,32 @@
'(ansible
ansible-doc
company
(company-ansible :toggle (configuration-layer/package-usedp 'company))
jinja2-mode
yaml-mode))
(defun ansible/init-ansible ()
(use-package ansible
:defer t
:init (add-to-list 'auto-mode-alist
'("\\(group_vars/.+\\|host_vars/.+\\)" . yaml-mode))))
:init
(progn
(add-hook 'yaml-mode-hook 'spacemacs/ansible-maybe-enable)
(if ansible-auto-encrypt-descrypt
(add-hook 'ansible-hook 'ansible::auto-decrypt-encrypt)
(remove-hook 'ansible-hook 'ansible::auto-decrypt-encrypt))
(spacemacs/set-leader-keys-for-minor-mode 'ansible
"bd" 'ansible::decrypt-buffer
"be" 'ansible::encrypt-buffer))))
(defun ansible/init-ansible-doc ()
(use-package ansible-doc :defer t))
(use-package ansible-doc
:defer t
:init
(progn
(add-hook 'yaml-mode-hook 'spacemacs/ansible-doc-maybe-enable)
(spacemacs/set-leader-keys-for-minor-mode 'ansible-doc-mode
"ha" 'ansible-doc))
:config (spacemacs|hide-lighter ansible-doc-mode)))
(defun ansible/post-init-company ()
;; ansible-mode requires ac-user-dictionary-files. If the
@ -33,12 +48,22 @@
;; https://github.com/k1LoW/emacs-ansible/issues/2
(defvar ac-user-dictionary-files nil))
(defun ansible/init-company-ansible ()
(use-package company-ansible
:defer t
:init
;; append this hook at the end to execute it last so `company-backends'
;; variable is buffer local
(add-hook 'yaml-mode-hook 'spacemacs/ansible-company-maybe-enable t)))
(defun ansible/init-jinja2-mode ()
(use-package jinja2-mode
:mode ("\\.j2\\'" . jinja2-mode)
:defer t))
(defun ansible/post-init-yaml-mode ()
(spacemacs/set-leader-keys-for-major-mode 'yaml-mode "ha" 'ansible-doc)
(spacemacs/add-to-hook 'yaml-mode-hook '(ansible/ansible-maybe-enable
ansible/ansible-doc-maybe-enable)))
;; maybe move it to the layer owning `yaml-mode' at some point
(spacemacs/declare-prefix-for-mode 'yaml-mode "mh" "help")
(spacemacs/declare-prefix-for-mode 'yaml-mode "mb" "buffer")
(add-to-list 'auto-mode-alist
'("\\(group_vars/.+\\|host_vars/.+\\)" . yaml-mode)))