The main purpouse of this document is to define a guide of Best Practices that should be adopted when building huge automation projects in Ansible. As the things can run wild when teams start scripting tasks (each one doing the things on its way), we found necessary to define some Rules of Convenience to be followed by everyone involved, so the things can be set in a little more stable shape.
Follow this and you will be happy. Seriously.
- Use Ansible to automate complicated or recurrent tasks
- Use Ansible to change configuration values on devices or applications
- Use Ansible for provisioning new environments
- Plan the tasks you need to automate
- Plan how to make them reusable
- Share your plans with your team
- Make the necessary changes
- Count to 10 and start writing
- What you'll do can be reused? Pick a
role
; - The
role
doesn't yet exist? Create a new one; - The new
role
is generic and can be reused? No? So, change it!
When you need a module, start looking for:
- Official (
ansible-modules
) - Extra (
ansible-modules-extra
) - Community (modules maintained by community) If you have not found what you want, you can:
- Contribute (help evolving an existing official, extra or community module)
- Create (yes, make your own
module.py
) - Use
shell
/command
/raw
modules (the least priority; use only when EXTREMELLY necessary)
- When possible, never fix values inside of a
playbook
- The same is valid for the
roles
- Use
variables
, that can be passed by the user from outside - Variables can be passed from a large variety of places and files
- Variables are cool ;)
- Some files are static, and it's OK to copy them just as they are
- But a lot of files has configurations and settings - and they can change
- Use jinja2 to create template files (
file.j2
) - Use
template
module for templating tasks
- Running a
playbook
should define a desired state - Running the same
playbook
again should still guarantee the same state - Running the same
playbook
again² should still guarantee the same state - Running the same
playbook
again³ should still guarantee the same state - Yeah, I think you got it...
- When you create a new
module
, share it with the community - This way you help other people, and they can even improove your job
- Remember that you are using modules that they brought to you
- When you first create a
playbook
, you need to test it - When you first create a
role
, you need to test it - When you first create a
module
, you need to test it - When you change anything, you need to test it all again
- Do automatic unitary tests
- Do automatic recursive tests
- Document
playbooks
- Document
roles
- Document
modules
- Document
variables
- Document all, even the
inventories
- And... Are you changing something? Do document! Your team isn't always aware of what you've done.