i write one specific roles for local and dev environment that will drop and recreate the database from first server in dbserver group which mostly used as the master database.
group_vars/dbserver
[dbserver]
vagrant1 # master db
vagrant2 # slave db
after that, if i need to drop the database and also create the database again, basically i just need that command to be run on the first server in the group.
- name: drop database
mysql_db: name={{ targetdbname }} state=absent
when: targetdeploydb == "new"
delegate_to: "{{ item }}"
with_items: "{{ groups.dbserver }}"
run_once: true
- name: create database
mysql_db: name={{ targetdbname }} state=present
when: targetdeploydb == "new"
delegate_to: "{{ item }}"
with_items: "{{ groups.dbserver }}"
when: targetdeploydb == "new"
run_once: true
Here is the log when i run the playbook
TASK [laravel : drop database] *************************************************
changed: [vagrant1 -> vagrant1] => (item=vagrant1)
changed: [vagrant1 -> vagrant2] => (item=vagrant2)
TASK [laravel : create database] ***********************************************
changed: [vagrant1 -> vagrant1] => (item=vagrant1)
changed: [vagrant1 -> vagrant2] => (item=vagrant2)
Another way that i can think of is by using the master db hostname directly on delegate_to but it means i need to create another variable. in the other hand, i think it would be better to reduce the number of variable and make it more dynamic. Please advise