From 59acdd8c6848a03c47ae56d99799d2b0a6891b39 Mon Sep 17 00:00:00 2001 From: Jesse Pretorius Date: Thu, 28 Jun 2018 17:07:14 +0100 Subject: [PATCH] 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 --- defaults/main.yml | 2 ++ tasks/watcher_db_setup.yml | 26 +++++++++++++++++ ...test-vars.yml => os_watcher-overrides.yml} | 3 -- tests/test-install-watcher.yml | 29 ++----------------- 4 files changed, 30 insertions(+), 30 deletions(-) rename tests/{test-vars.yml => os_watcher-overrides.yml} (90%) diff --git a/defaults/main.yml b/defaults/main.yml index e90dbe9..a45cda4 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -50,6 +50,8 @@ watcher_system_comment: watcher system user watcher_system_user_home: "/var/lib/{{ watcher_system_user_name }}" ## 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_database: watcher diff --git a/tasks/watcher_db_setup.yml b/tasks/watcher_db_setup.yml index 32c99f7..259f5eb 100644 --- a/tasks/watcher_db_setup.yml +++ b/tasks/watcher_db_setup.yml @@ -13,6 +13,32 @@ # See the License for the specific language governing permissions and # 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 command: "{{ watcher_bin }}/watcher-db-manage --config-file /etc/watcher/watcher.conf create_schema" become: yes diff --git a/tests/test-vars.yml b/tests/os_watcher-overrides.yml similarity index 90% rename from tests/test-vars.yml rename to tests/os_watcher-overrides.yml index 336e0f1..2f5fecd 100644 --- a/tests/test-vars.yml +++ b/tests/os_watcher-overrides.yml @@ -14,9 +14,6 @@ # limitations under the License. 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_rabbitmq_port: "{{ rabbitmq_port }}" watcher_rabbitmq_servers: "{{ rabbitmq_servers }}" diff --git a/tests/test-install-watcher.yml b/tests/test-install-watcher.yml index dc4d5f6..01dcac2 100644 --- a/tests/test-install-watcher.yml +++ b/tests/test-install-watcher.yml @@ -30,12 +30,14 @@ - pkg-config - libvirt-dev when: inventory_hostname in groups['watcher_all'] + - name: Ensure rabbitmq vhost rabbitmq_vhost: name: "{{ watcher_rabbitmq_vhost }}" state: "present" delegate_to: "{{ hostvars[groups['rabbitmq_all'][0]]['ansible_host'] }}" when: inventory_hostname == groups['watcher_all'][0] + - name: Ensure rabbitmq user rabbitmq_user: user: "{{ watcher_rabbitmq_userid }}" @@ -48,35 +50,8 @@ delegate_to: "{{ hostvars[groups['rabbitmq_all'][0]]['ansible_host'] }}" when: inventory_hostname == groups['watcher_all'][0] 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: - role: "os_watcher" vars_files: - common/test-vars.yml - - test-vars.yml