Editing ansible files in Emacs


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

Indent

Another problem to see with eyes. Every programming language is created for 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
  • 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
    • 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
  • 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
  • My pretty solution to search variable in playbook which we work