-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_setup.ps1
149 lines (122 loc) · 4.44 KB
/
run_setup.ps1
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
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string]$InputPassword
)
Function InstallChocolatey{
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
refreshenv
}
Function SetupCygwinWithAnsible{
choco install -y cygwin "--no-desktop --quiet-mode --wait"
choco install -y cyg-get
cyg-get ansible
$SystemPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
$SystemPath += ";C:\tools\cygwin\bin"
[System.Environment]::SetEnvironmentVariable("Path", $SystemPath, "User")
refreshenv
$Options = "-l -c `"ansible --version`" "
Start-Process "C:\tools\cygwin\bin\bash" -ArgumentList $Options -Wait -NoNewWindow
}
Function SetupWinRMForAnsible{
$SetupScriptForAnsible = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
$File = "$env:temp\ConfigureRemotingForAnsible.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($SetupScriptForAnsible, $File)
powershell -ExecutionPolicy RemoteSigned $File -ForceNewSSLCert -SkipNetworkProfileCheck -Verbose
}
Function CheckWinRMConnectivity{
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[String]$UserName,
[Parameter(Mandatory)]
[String]$UserPassword,
[Parameter(Mandatory)]
[String]$TargetHost
)
$Password = ConvertTo-SecureString -String $UserPassword -AsPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password
$SessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
Invoke-Command -ComputerName $TargetHost -UseSSL -ScriptBlock { ipconfig } -Credential $Cred -SessionOption $SessionOption
}
Function RunCommand{
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[String]$FileName,
[String]$Arguments
)
$ProcessInfo = New-object System.Diagnostics.ProcessStartInfo
$ProcessInfo.CreateNoWindow = $true
$ProcessInfo.UseShellExecute = $false
$ProcessInfo.RedirectStandardOutput = $true
$ProcessInfo.RedirectStandardError = $true
$ProcessInfo.FileName = $FileName
$ProcessInfo.Arguments = $Arguments
$Process = New-Object System.Diagnostics.Process
$Process.StartInfo = $ProcessInfo
$Process.Start() | Out-Null
$Process.WaitForExit()
return $Process.StandardOutput.ReadToEnd()
}
Function GetCygPath{
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[String]$WinPath
)
$Options = "-l -c `"cygpath -u ${WinPath}`""
return RunCommand "C:\tools\cygwin\bin\bash" $Options | % { $_ -replace "`n", "" }
}
Function RunPlaybook{
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[String]$InventoryFile,
[Parameter(Mandatory)]
[String]$PlaybookFile
)
$CygInventoryFile = GetCygPath ${InventoryFile}
$CygPlaybookFile = GetCygPath ${PlaybookFile}
$Options = "-l -c `"ansible-playbook -i ${CygInventoryFile} ${CygPlaybookFile} `" "
echo $Options
Start-Process "C:\tools\cygwin\bin\bash" -ArgumentList $Options -Wait -NoNewWindow
}
Function CreateInventoryFile{
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[String]$UserName,
[Parameter(Mandatory)]
[String]$Password,
[Parameter(Mandatory)]
[String]$TargetHost,
[Parameter(Mandatory)]
[String]$FilePath
)
$Cotents = @"
[winrm_local]
${TargetHost}
[winrm_local:vars]
ansible_user=${UserName}
ansible_password=${Password}
ansible_connection=winrm
ansible_winrm_transport=basic
ansible_winrm_server_cert_validation=ignore
"@
Set-Content -Path $FilePath -Value $Cotents -Encoding Ascii
}
$UserName = "WDAGUtilityAccount"
$Password = $InputPassword
net user $UserName $Password
InstallChocolatey
SetupCygwinWithAnsible
SetupWinRMForAnsible
$TargetHost = "127.0.0.1"
CheckWinRMConnectivity $UserName $Password $TargetHost
$InventoryFile = $PSScriptRoot + '\hosts'
$PlaybookFile = $PSScriptRoot + '\dev_playbook.yml'
$InventoryFile = $InventoryFile.Replace("\", "\\\\")
$PlaybookFile = $PlaybookFile.Replace("\", "\\\\")
CreateInventoryFile $UserName $Password $TargetHost $InventoryFile
RunPlaybook $InventoryFile $PlaybookFile