Before i start, let me give a bit detail on my ansible directory structure
- environment
- production # inventory file for production servers
- group_vars
- host_vars
- hosts
- staging # inventory file for staging environment
- group_vars
- host_vars
- hosts
- production # inventory file for production servers
- roles
- nginx
- mysql
- etc
- deploy_app.yml
so right now, if i want to deploy one apps to staging, i can just simply run this playbook command:
ansible-playbook -i environments/staging/hosts deploy-app.yml
for example, i defined the path for my app1 directory in staging/group_vars/webserver to "/var/www/staging.app1.com". but i also need to deploy to the same server for app2 but with different directory. Is it possible to use conditional variable in group_vars?
So if i run:
ansible-playbook -i environments/staging/hosts deploy-app.yml --extra-vars "app_name=app1"
it will deploy to staging server on /var/www/staging.app1.com and:
ansible-playbook -i environments/staging/hosts deploy-app.yml --extra-vars "app_name=app2"
it will deploy to staging server on /var/www/staging.app2.com
i know that the easiest way could just define app_dir variable when running playbook, but if possible, i prefer to define the app_name then app_dir will be used based on the specified app_name.
if app_name = app1, then app_dir = /var/www/staging.app1.com
elif app_name = app2, then app_dir = /var/www/staging.app2.com
Please advise kindly