6 Tips to level-up your Ansible code
Write cleaner Ansible code with these less commonly known tricks
Ansible is the go-to configuration-as-code tool I pull from the old DevOps toolbox anytime I need to install some new piece of software. It's incredibly easy to get started with theirs.YAML syntax and can take my existing scripts and config files without needing to throw them completely out.
Over time, if you’re like me, you start to wonder if there’s a better way to do this, or that seems like a lot of lines of code to write for this simple little thing. Here are some tips and tricks I’ve picked up along the way that’s helped create higher quality Ansible code.
Tip #1 – Use more roles
Although you can write your playbooks in one single YAML
file, it’s very much considered a bad practice. And for good reason: It’s harder for someone new to read and it’s harder to maintain. The first thing you need to think about when designing your playbooks (ie. for deploying your web app) is trying to split the various components in roles, no matter how small a particular role may be. For instance, in a typical web app playbook you would have these roles:
- Common
- Nginx
- App server
- Database