-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gen-Inventory.ps1
60 lines (48 loc) · 2.24 KB
/
Gen-Inventory.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
param(
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$EC2NAME,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$DOMAINNAME,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$DOMAINSUFFIX,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$USERNAME
)
# Check and create .\Deploy\inventory directory if it doesn't exist
$deployPath = ".\Deploy"
if (-not (Test-Path -Path $deployPath)) {
New-Item -ItemType Directory -Path $deployPath
}
# Copy requirements.yml template to .\Deploy
$templateRequirementsPath = ".\ansible-init\requirements.yml"
$deployRequirementsFilePath = Join-Path $deployPath "requirements.yml"
Copy-Item -Path $templateRequirementsPath -Destination $deployRequirementsFilePath -Force
# Copy ansible.cfg template to .\Deploy
$templateAnsibleCfgPath = ".\ansible-init\ansible.cfg"
$deployAnsibleCfgFilePath = Join-Path $deployPath "ansible.cfg"
Copy-Item -Path $templateAnsibleCfgPath -Destination $deployAnsibleCfgFilePath -Force
# Copy deploy.yml template to .\Deploy
$templateDeployPath = ".\templates\deploy.yml"
$deployFilePath = Join-Path $deployPath "deploy.yml"
Copy-Item -Path $templateDeployPath -Destination $deployFilePath -Force
# Copy inventory.yml template to .\Deploy\inventory
$templatePath = ".\templates\inventory.yml"
$deployFilePath = Join-Path $deployPath "inventory.yml"
Copy-Item -Path $templatePath -Destination $deployFilePath -Force
# Function to replace placeholder in file
function Replace-PlaceholderInFile {
param($filePath, $placeholder, $value)
Write-Output "replacing $placeholder with $value"
(Get-Content $filePath) -replace "<<<$placeholder>>>", $value | Set-Content $filePath
}
# Replace placeholders with parameter values
Replace-PlaceholderInFile -filePath $deployFilePath -placeholder "EC2NAME" -value $EC2NAME
Replace-PlaceholderInFile -filePath $deployFilePath -placeholder "DOMAINNAME" -value $DOMAINNAME
Replace-PlaceholderInFile -filePath $deployFilePath -placeholder "DOMAINSUFFIX" -value $DOMAINSUFFIX
Replace-PlaceholderInFile -filePath $deployFilePath -placeholder "USERNAME" -value $USERNAME
# Usage Example
# $params = @{
# EC2NAME = "myec2vm"
# USERNAME = "adminuser"
# }
# $params | .\Gen-Config.ps1