As per the "Best Practices" chapter in Ansible's documentation, I have separate inventory files for my development and production environments. While both environments get the same software stack installed, there are some differences: cron jobs are only installed on production hosts, symlinks to VirtualBox shared folders should only exist in the dev environment. I currently use a dev and a production role in addition to "common", and import their main.yml
depending on the inventory_hostname
variable.
This feels clumsy, because there's now a lot of folders and files for only a few additional tasks, and using inventory_hostname
to distinguish between development and production environments violates the DRY principle because whether we're in dev or prod is now decided by the choice of inventory file and by the inventory_hostname
variable. Is there a more elegant way to handle this?
If you are using https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#alternative-directory-layout you can put a variable
env
in dev/group_vars/all.yml and prod/group_vars/all.yml.If not you could add this to the end of the inventory file: