I this blog post, I’ll talk about how to make easier to search and edit ansible playbooks in Emacs. As you know compare to others π working with Emacs, always bring so many additional features.
Let’s talk a bit them.
Playbooks are compose of YAML files. Thanks to god this format is better than XML and JSON. However, still, there is so much pain while writing it.
For example;
- Pharantesis
- Indent
- Quotes (double quotes, much more quotes, so many quotes)
Solutions for those problems are here
Paracentesis and quotes
- smartparens–mode
- Automatically create pair for curly
- Gives few shortcuts to quick move to paracentesis
1(add-hook 'yaml-mode-hook #'smartparens-mode)
Indent
Another problem to see with eyes. Every programming language is created for the computer. Nobody cares about humans.Β YAML is also kind of human killer format that thinks that we are using magnifying glass. If you don’t want to be that guy just do what I say π
- highlight-indentation-mode
- It makes easier to see indentation problems
- It makes easier to see indentation problems
- indent-for-tab-command
- This is easy solution to indent in ansible playbooks
- It gives region and individual indentation options just pressing tab
- It comes build in if you don’t have check maybe you remap your tab key
Something more
- yasnippet
- Probably you heard and use it before while programming in Emacs, basically yas offer so fast and easy code writing with snippets. By using yas you can create so many snippets that would make your life easier
- emacs-ansible(alias is ansible in melpa repository)
- https://github.com/k1LoW/emacs-ansible
- In this Github repo there are defined keywords for ansible. It additionally contains so many snippets that make easier to write ansible-playbooks. If you are not familiar with you check just this link to
1(add-hook 'yaml-mode-hook '(lambda () (ansible 1))) # If you install it from package-manager
- I offer to download both. The reason of that you have to show to emacs where snippets are, If you don’t want to download extra you can find that package inside of ~.emacs.d/elpa/ansible-RANDOM-NUMBER
1 |
(yas-load-directory "~/path/of/snippets/") |
- ansible-galaxy offer good architect for working with ansible files probably you are using it. But when the time has come to find value of {{ where_is_this_fucking_variable }} that defined in *default*(or somewhereelse) directory it makes me sad. You can use zrgrep but it takes long effort and so much keypress in that situation. I wrote simple search-in-playbook command that works any kind of playbook.
- Example structure
1234567891011121314151617181920212223242526272829303132333435.βββ playbook1βΒ Β βββ README.mdβΒ Β βββ defaultsβΒ Β βΒ Β βββ main.ymlβΒ Β βββ filesβΒ Β βββ handlersβΒ Β βΒ Β βββ main.ymlβΒ Β βββ metaβΒ Β βΒ Β βββ main.ymlβΒ Β βββ tasksβΒ Β βΒ Β βββ main.ymlβΒ Β βββ templatesβΒ Β βββ testsβΒ Β βΒ Β βββ inventoryβΒ Β βΒ Β βββ test.ymlβΒ Β βββ varsβΒ Β βββ main.ymlβββ playbook2βββ README.mdβββ defaultsβΒ Β βββ main.ymlβββ filesβββ handlersβΒ Β βββ main.ymlβββ metaβΒ Β βββ main.ymlβββ tasksβΒ Β βββ main.ymlβββ templatesβββ testsβΒ Β βββ inventoryβΒ Β βββ test.ymlβββ varsβββ main.yml - My pretty solution to search variable in role which you are currently edit
1 2 3 4 |
(defun search-in-role () "Seach in role folder" (interactive) (helm-do-ag (expand-file-name ".." projectile-project-root))) |