Best practices for writing better ansible playbooks
Some of the best practices for writing the clean and scalable ansible playbooks:
- Maintain all ansible code in git.
- Maintain the standard directory structure. Here is the reference: https://github.com/Indu-sharma/learningansible
- Use requirements.txt for python dependencies management and requirements.yml for ansible-galaxy modules/roles.
- Configure ansible_stdout_callback, log file path, forks in ansible.cfg.
- Always use -check mode for ansible playbooks for the dry run.
- Verify all your variables in advance with -m setup.
- Use ansible-galaxy init for your creating your roles directory/files structures.
- Within a role playbook, follow the sequence in the start
- Use assert to check that requirements are met
- Install packages
- Use Validate for Template config files
- Trigger handlers when config files have been changed
- Avoid using shell/command modules if possible; they are skipped in -check mode.
- Avoid using set_facts; they are not visible when using -m setup mode. Also, variables are not while using --start-at option.
- Use Tags as much as possibles.
- Don't hide changes(when using --diff/-check )
- Don't suppress failures.
- Ensure Idem-potency always.
- Don't hard code any variables in playbooks.
- Use Dynamic Inventory wherever possible
- Use strategy as Debug in the playbooks.
- Use Mitogen or Pipe-lining to speed up the playbooks run time.
- Use Molecule module to test the playbooks.
Comments