Move database creation into role

There is no record for why we implement the database creation outside
of the role in the playbook, when we could do it inside the role.

Implementing it inside the role allows us to reduce the quantity of
group_vars duplicated from the role, and allows us to better document
the required variables in the role. The delegation can still be done
as it is done in the playbook too.

In this patch we implement a new variable called 'watcher_db_setup_host'
which is used in the role to allow delegation of the database setup
task to any host, but defaults to the first member of the galera_all
host group. We also document the variable 'watcher_galera_address' which
has been used for a long time, but never documented. A bunch of unused
variables have also been removed.

Change-Id: I3b845ee913e3abc70bbc59b85142ebd55b92de51
This commit is contained in:
Jesse Pretorius 2018-06-28 17:07:14 +01:00 committed by Jesse Pretorius (odyssey4me)
parent 0757754274
commit 59acdd8c68
4 changed files with 30 additions and 30 deletions

View File

@ -50,6 +50,8 @@ watcher_system_comment: watcher system user
watcher_system_user_home: "/var/lib/{{ watcher_system_user_name }}" watcher_system_user_home: "/var/lib/{{ watcher_system_user_name }}"
## DB ## DB
watcher_db_setup_host: "{{ ('galera_all' in groups) | ternary(groups['galera_all'][0], 'localhost') }}"
watcher_galera_address: "{{ galera_address | default('127.0.0.1') }}"
watcher_galera_user: watcher watcher_galera_user: watcher
watcher_galera_database: watcher watcher_galera_database: watcher

View File

@ -13,6 +13,32 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
- name: Create DB for service
mysql_db:
login_user: "{{ galera_root_user }}"
login_password: "{{ galera_root_password }}"
login_host: "{{ watcher_galera_address }}"
name: "{{ watcher_galera_database }}"
state: "present"
delegate_to: "{{ watcher_db_setup_host }}"
no_log: True
- name: Grant access to the DB for the service
mysql_user:
login_user: "{{ galera_root_user }}"
login_password: "{{ galera_root_password }}"
login_host: "{{ watcher_galera_address }}"
name: "{{ watcher_galera_user }}"
password: "{{ watcher_galera_password }}"
host: "{{ item }}"
state: "present"
priv: "{{ watcher_galera_database }}.*:ALL"
delegate_to: "{{ watcher_db_setup_host }}"
with_items:
- "localhost"
- "%"
no_log: True
- name: Perform a Watcher DB schema - name: Perform a Watcher DB schema
command: "{{ watcher_bin }}/watcher-db-manage --config-file /etc/watcher/watcher.conf create_schema" command: "{{ watcher_bin }}/watcher-db-manage --config-file /etc/watcher/watcher.conf create_schema"
become: yes become: yes

View File

@ -14,9 +14,6 @@
# limitations under the License. # limitations under the License.
watcher_developer_mode: True watcher_developer_mode: True
watcher_galera_address: "{{ hostvars[groups['galera_all'][0]]['ansible_host'] }}"
watcher_galera_database: watcher
watcher_galera_user: watcher
watcher_galera_password: "secrete" watcher_galera_password: "secrete"
watcher_rabbitmq_port: "{{ rabbitmq_port }}" watcher_rabbitmq_port: "{{ rabbitmq_port }}"
watcher_rabbitmq_servers: "{{ rabbitmq_servers }}" watcher_rabbitmq_servers: "{{ rabbitmq_servers }}"

View File

@ -30,12 +30,14 @@
- pkg-config - pkg-config
- libvirt-dev - libvirt-dev
when: inventory_hostname in groups['watcher_all'] when: inventory_hostname in groups['watcher_all']
- name: Ensure rabbitmq vhost - name: Ensure rabbitmq vhost
rabbitmq_vhost: rabbitmq_vhost:
name: "{{ watcher_rabbitmq_vhost }}" name: "{{ watcher_rabbitmq_vhost }}"
state: "present" state: "present"
delegate_to: "{{ hostvars[groups['rabbitmq_all'][0]]['ansible_host'] }}" delegate_to: "{{ hostvars[groups['rabbitmq_all'][0]]['ansible_host'] }}"
when: inventory_hostname == groups['watcher_all'][0] when: inventory_hostname == groups['watcher_all'][0]
- name: Ensure rabbitmq user - name: Ensure rabbitmq user
rabbitmq_user: rabbitmq_user:
user: "{{ watcher_rabbitmq_userid }}" user: "{{ watcher_rabbitmq_userid }}"
@ -48,35 +50,8 @@
delegate_to: "{{ hostvars[groups['rabbitmq_all'][0]]['ansible_host'] }}" delegate_to: "{{ hostvars[groups['rabbitmq_all'][0]]['ansible_host'] }}"
when: inventory_hostname == groups['watcher_all'][0] when: inventory_hostname == groups['watcher_all'][0]
no_log: true no_log: true
- name: Create DB for service
mysql_db:
login_user: "{{ galera_root_user }}"
login_password: "{{ galera_root_password }}"
login_host: "{{ watcher_galera_address }}"
name: "{{ watcher_galera_database }}"
state: "present"
delegate_to: "{{ hostvars[groups['galera_all'][0]]['ansible_host'] }}"
when: inventory_hostname == groups['watcher_all'][0]
no_log: true
- name: Grant access to the DB for the service
mysql_user:
login_user: "{{ galera_root_user }}"
login_password: "{{ galera_root_password }}"
login_host: "{{ watcher_galera_address }}"
name: "{{ watcher_galera_database }}"
password: "{{ watcher_galera_password }}"
host: "{{ item }}"
state: "present"
priv: "{{ watcher_galera_database }}.*:ALL"
with_items:
- "localhost"
- "%"
delegate_to: "{{ hostvars[groups['rabbitmq_all'][0]]['ansible_host'] }}"
when: inventory_hostname == groups['watcher_all'][0]
no_log: true
roles: roles:
- role: "os_watcher" - role: "os_watcher"
vars_files: vars_files:
- common/test-vars.yml - common/test-vars.yml
- test-vars.yml