-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup-database.yml
153 lines (133 loc) · 5.25 KB
/
setup-database.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
---
# Installs MS SQL Express ~and SQL Server Management Studio (SSMS)~
- name: Install SQL Server Express
chocolatey.chocolatey.win_chocolatey:
name: sql-server-express
state: present
become: true
- name: Add Firewall Exception for CCM to Database
community.windows.win_firewall_rule:
name: CCM - SQL Service
action: allow
description: Allow inbound traffic from CCM to the database
localport: "1433"
protocol: tcp
- name: Add Firewall Exception for SQL Server Browser to Database
community.windows.win_firewall_rule:
name: CCM - SQL Browser
action: allow
description: Allow inbound traffic from CCM to the database
localport: "1434"
protocol: udp
- name: Set SQL LoginMode to allow SQL Logins and Enable Protocols
ansible.windows.win_powershell:
script: |
$Ansible.Changed = $false
$SqlString = (Get-ChildItem -Path 'HKLM:\Software\Microsoft\Microsoft SQL Server').Name |
Where-Object { $_ -like "HKEY_LOCAL_MACHINE\Software\Microsoft\Microsoft SQL Server\MSSQL*.SQLEXPRESS" }
$SqlVersion = $SqlString.Split("\") | Where-Object { $_ -like "MSSQL*.SQLEXPRESS" }
if ((Get-ItemProperty "HKLM:\Software\Microsoft\Microsoft SQL Server\$SqlVersion\MSSQLServer\" -Name LoginMode -ErrorAction SilentlyContinue).LoginMode -ne 2) {
New-ItemProperty "HKLM:\Software\Microsoft\Microsoft SQL Server\$SqlVersion\MSSQLServer\" -Name 'LoginMode' -Value 2 -Force
$Ansible.Changed = $true
}
Import-Module SQLPS
$Wmi = [Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer]::new()
foreach ($Protocol in @('Tcp', 'Np')) {
$Setting = $Wmi.GetSmoObject("ManagedComputer[@Name='$($env:ComputerName)']/ServerInstance[@Name='SQLEXPRESS']/ServerProtocol[@Name='$($Protocol)']")
if (-not $Setting.IsEnabled) {
$Setting.IsEnabled = $true
$Setting.Alter()
$Ansible.Changed = $true
}
}
if ($Ansible.Changed = $true) {
Restart-Service 'MSSQL$SQLEXPRESS'
Restart-Service 'SQLBrowser'
}
- name: Start SQLBrowser Service
ansible.windows.win_service:
name: SQLBrowser
start_mode: auto
state: started
- name: Setup Database
ansible.windows.win_powershell:
parameters:
LoginUser: "{{ database_username | default('ChocoUser') }}"
LoginPassword: "{{ database_password }}"
script: |
#requires -modules DbaTools
param(
[string]$LoginUser,
[string]$LoginPassword
)
$ErrorActionPreferece = "Stop"
$Ansible.Changed = $false
# For creating this database, we assume we're using a freshly set up SQLServerExpress instance
$SqlInstance = @{
SqlInstance = Connect-DbaInstance -SqlInstance "localhost\SQLEXPRESS" -TrustServerCertificate
}
$DbArgs = @{
Database = "ChocolateyManagement"
}
if (-not ($DB = Get-DbaDatabase @SqlInstance @DbArgs)) {
$DB = New-DbaDatabase @SqlInstance -Name $DbArgs.Database
$Ansible.Changed = $true
}
$Ansible.Result = @{
SqlInstance = $DB.SqlInstance
DatabaseName = $DB.Name
}
$LoginArgs = @{
Login = $LoginUser
}
if (-not ($Login = Get-DbaLogin @SqlInstance @LoginArgs)) {
$LoginArgs += @{
SecurePassword = $LoginPassword | ConvertTo-SecureString -AsPlainText -Force
DefaultDatabase = $DB.Name
PasswordExpirationEnabled = $false
PasswordPolicyEnforced = $false
}
$Login = New-DbaLogin @SqlInstance @LoginArgs
$Ansible.Changed = $true
} else {
$LoginArgs += @{
SecurePassword = $LoginPassword | ConvertTo-SecureString -AsPlainText -Force
}
$Login = Set-DbaLogin @SqlInstance @LoginArgs
if ($Login.PasswordChanged) {
$Ansible.Changed = $true
}
}
$UserArgs = @{
User = $LoginUser
}
if (-not ($User = Get-DbaDbUser @SqlInstance @DbArgs @UserArgs)) {
$User = New-DbaDbUser @SqlInstance @DbArgs -Username $UserArgs.User -Login $LoginArgs.Login
$Ansible.Changed = $true
}
foreach ($Role in @(
'db_datareader', 'db_datawriter', 'db_ddladmin'
# TODO: Check what database permissions are really required. May just grant CREATE TABLE?
)) {
if ($UserArgs.User -notin (Get-DbaDbRoleMember @SqlInstance @DbArgs -Role $Role).UserName) {
Add-DbaDbRoleMember @SqlInstance @DbArgs @UserArgs -Role $Role -Confirm:$false
$Ansible.Changed = $true
}
}
$Ansible.Result += @{
UserId = $Login.Name
UserPassword = $LoginPassword
}
register: _database_setup
- name: Validate Connection String Information
ansible.builtin.assert:
that:
- _database_setup.result.SqlInstance is defined
- _database_setup.result.DatabaseName is defined
- _database_setup.result.UserId is defined
- _database_setup.result.UserPassword is defined
quiet: true
- name: Set database_connection_string
ansible.builtin.set_fact:
database_connection_string: "server={{ _database_setup.result.SqlInstance }};database={{ _database_setup.result.DatabaseName }};User Id={{ _database_setup.result.UserId }};Password={{ _database_setup.result.UserPassword }}"
...