Skip to content

Commit

Permalink
Set up database replication without pacemaker
Browse files Browse the repository at this point in the history
TODO:
Add mssql_ha_reset_cert
Add idempotency to all templates
  • Loading branch information
spetrosi committed Apr 14, 2022
1 parent 02d2edb commit 43f0632
Show file tree
Hide file tree
Showing 20 changed files with 968 additions and 52 deletions.
2 changes: 2 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mssql_enable_sql_agent: null
mssql_install_fts: null
mssql_install_powershell: null
mssql_enable_ha: null
mssql_ha_configure: false
mssql_ha_reset_cert: false
mssql_tune_for_fua_storage: null
mssql_tls_enable: null
mssql_tls_cert: null
Expand Down
2 changes: 2 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
service:
name: mssql-server
state: restarted
when: not (__mssql_primary_restarted is changed or
__mssql_replica_restarted is changed)
70 changes: 70 additions & 0 deletions tasks/input_sql_file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# SPDX-License-Identifier: MIT
# This task files inputs sql file into MSSQL.
# If you feed a file with .j2 extension, it generates a template from it.
# If you feed a file with other extension, it just copies the file and runs it.
---
- name: Verify that the mssql_password variable is defined
assert:
that:
- mssql_password is not none
fail_msg: >-
You must define the mssql_password variable because MSSQL requires
the sa user to authenticate to input SQL files.
- name: Input {{ __mssql_input_sql_file }} to MSSQL
block:
# changed_when: false for idempotency because the file is always removed
- name: Create a tempfile for the SQL file on the host
tempfile:
state: file
register: __mssql_sql_tempfile
changed_when: false

# changed_when: false for idempotency because the file is always removed
- name: Copy the {{ __mssql_input_sql_file }} file to the host
copy:
src: "{{ __mssql_input_sql_file }}"
dest: "{{ __mssql_sql_tempfile.path }}"
mode: preserve
when: __mssql_input_sql_file is not search(".*\.j2")
changed_when: false

# changed_when: false for idempotency because the file is always removed
- name: Generate the {{ __mssql_input_sql_file }} template on the host
template:
src: "{{ __mssql_input_sql_file }}"
dest: "{{ __mssql_sql_tempfile.path }}"
when: __mssql_input_sql_file is search(".*\.j2")
changed_when: false

- name: Ensure that the mssql-server service is started
service:
name: mssql-server
state: started

- name: Prepare MSSQL and facts for logging in
include_tasks: verify_password.yml
vars:
__mssql_password: "{{ mssql_password }}"
when: __mssql_sqlcmd_login_cmd is none

- name: Input {{ __mssql_input_sql_file }} with the sqlcmd command
command: >-
{{ __mssql_sqlcmd_login_cmd }} -i {{ __mssql_sql_tempfile.path }} -b
register: __mssql_sqlcmd_input_file
changed_when: '"successfully" in __mssql_sqlcmd_input_file.stdout'

always:
- name: Print the output of the sqlcmd command if not empty
debug:
var: __mssql_sqlcmd_input_file.stdout_lines
changed_when: false

- name: Unset the __mssql_sqlcmd_input_file variable
set_fact:
__mssql_sqlcmd_input_file: ""

#- name: Remove the tempfile
# file:
# path: "{{ __mssql_sql_tempfile.path }}"
# state: absent

0 comments on commit 43f0632

Please sign in to comment.