Update ssh on windows pc.yml

This commit is contained in:
WickedYoda committed 2026-10-21 13:15:28 -05:00
1 parent 20dddf4feb
commit 69802266ae
1 file changed
+126 -40
+126 -40
View File
@@ -48,40 +48,106 @@
}
notify: Restart OpenSSH server
- name: Ensure SSH public-key authentication is enabled
ansible.windows.win_lineinfile:
path: C:\ProgramData\ssh\sshd_config
regexp: '^\s*#?\s*PubkeyAuthentication\s+'
line: PubkeyAuthentication yes
notify: Restart OpenSSH server
- name: Configure SSH public-key authentication and SFTP
ansible.windows.win_powershell:
script: |
$ErrorActionPreference = 'Stop'
$configPath = Join-Path $env:ProgramData 'ssh\sshd_config'
$original = [System.IO.File]::ReadAllText($configPath)
$lines = [System.Collections.Generic.List[string]]::new()
Get-Content -LiteralPath $configPath | ForEach-Object { $lines.Add([string]$_) }
- name: Enable the administrators authorized-keys configuration
ansible.windows.win_lineinfile:
path: C:\ProgramData\ssh\sshd_config
regexp: '^\s*#?\s*Match\s+Group\s+administrators\s*$'
line: Match Group administrators
notify: Restart OpenSSH server
function Set-GlobalDirective {
param([string]$Pattern, [string]$Value)
$matchIndex = -1
for ($index = 0; $index -lt $lines.Count; $index++) {
if ($lines[$index] -match '^\s*Match\s+') {
$matchIndex = $index
break
}
}
- name: Configure the authorized key file for administrator accounts
ansible.windows.win_lineinfile:
path: C:\ProgramData\ssh\sshd_config
regexp: '^\s*#?\s*AuthorizedKeysFile\s+__PROGRAMDATA__/ssh/administrators_authorized_keys\s*$'
line: ' AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys'
insertafter: '^Match Group administrators$'
notify: Restart OpenSSH server
$limit = if ($matchIndex -ge 0) { $matchIndex } else { $lines.Count }
for ($index = 0; $index -lt $limit; $index++) {
if ($lines[$index] -match $Pattern) {
$lines[$index] = $Value
return
}
}
- name: Configure the SFTP subsystem
ansible.windows.win_lineinfile:
path: C:\ProgramData\ssh\sshd_config
regexp: '^\s*#?\s*Subsystem\s+sftp\s+.*$'
line: Subsystem sftp sftp-server.exe
if ($matchIndex -ge 0) {
$lines.Insert($matchIndex, $Value)
}
else {
$lines.Add($Value)
}
}
Set-GlobalDirective '^\s*#?\s*PubkeyAuthentication\s+\S+' 'PubkeyAuthentication yes'
Set-GlobalDirective '^\s*#?\s*Subsystem\s+sftp\s+.*$' 'Subsystem sftp sftp-server.exe'
$administratorMatch = -1
for ($index = 0; $index -lt $lines.Count; $index++) {
if ($lines[$index] -match '^\s*#?\s*Match\s+Group\s+administrators\s*$') {
$administratorMatch = $index
break
}
}
if ($administratorMatch -lt 0) {
$lines.Add('')
$lines.Add('Match Group administrators')
$lines.Add(' AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys')
}
else {
$lines[$administratorMatch] = 'Match Group administrators'
$authorizedKeysIndex = -1
for ($index = $administratorMatch + 1; $index -lt $lines.Count; $index++) {
if ($lines[$index] -match '^\s*Match\s+') { break }
if ($lines[$index] -match '^\s*#?\s*AuthorizedKeysFile\s+') {
$authorizedKeysIndex = $index
break
}
}
if ($authorizedKeysIndex -ge 0) {
$lines[$authorizedKeysIndex] = ' AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys'
}
else {
$lines.Insert($administratorMatch + 1, ' AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys')
}
}
$updated = [string]::Join("`r`n", $lines) + "`r`n"
if ($updated -ne $original) {
[System.IO.File]::WriteAllText($configPath, $updated, [System.Text.UTF8Encoding]::new($false))
$Ansible.Changed = $true
}
else {
$Ansible.Changed = $false
}
notify: Restart OpenSSH server
- name: Add the Flint4 public key for Windows administrators
ansible.windows.win_lineinfile:
path: C:\ProgramData\ssh\administrators_authorized_keys
line: "{{ flint4_ssh_public_key }}"
create: true
ansible.windows.win_powershell:
parameters:
Flint4PublicKey: "{{ flint4_ssh_public_key }}"
script: |
param([string]$Flint4PublicKey)
$ErrorActionPreference = 'Stop'
$authorizedKeys = Join-Path $env:ProgramData 'ssh\administrators_authorized_keys'
if (-not (Test-Path -LiteralPath $authorizedKeys)) {
New-Item -Path $authorizedKeys -ItemType File -Force | Out-Null
}
$existingKeys = @(Get-Content -LiteralPath $authorizedKeys)
if ($existingKeys -notcontains $Flint4PublicKey) {
Add-Content -LiteralPath $authorizedKeys -Value $Flint4PublicKey -Encoding ascii
$Ansible.Changed = $true
}
else {
$Ansible.Changed = $false
}
- name: Restrict administrator authorized-key file permissions
ansible.windows.win_powershell:
@@ -99,18 +165,38 @@
changed_when: false
- name: Allow SSH from domain and private networks
ansible.windows.win_firewall_rule:
name: Ansible OpenSSH Server TCP 22
description: Allow SSH and SFTP management from domain and private networks.
localport: 22
protocol: tcp
direction: in
action: allow
profiles:
- Domain
- Private
enabled: true
state: present
ansible.windows.win_powershell:
script: |
$ErrorActionPreference = 'Stop'
$displayName = 'Ansible OpenSSH Server TCP 22'
$rule = Get-NetFirewallRule -DisplayName $displayName -ErrorAction SilentlyContinue |
Select-Object -First 1
if (-not $rule) {
New-NetFirewallRule -DisplayName $displayName `
-Description 'Allow SSH and SFTP management from domain and private networks.' `
-Direction Inbound -Action Allow -Protocol TCP -LocalPort 22 `
-Profile Domain,Private | Out-Null
$Ansible.Changed = $true
}
else {
$currentProfiles = @(([string]$rule.Profile -split ',\s*') | Sort-Object)
$expectedProfiles = @('Domain', 'Private')
$profilesDiffer = (@(Compare-Object $currentProfiles $expectedProfiles).Count -gt 0)
$needsUpdate = (-not $rule.Enabled) -or
($rule.Action.ToString() -ne 'Allow') -or
($rule.Direction.ToString() -ne 'Inbound') -or
$profilesDiffer
if ($needsUpdate) {
Set-NetFirewallRule -Name $rule.Name -Enabled True -Action Allow `
-Direction Inbound -Profile Domain,Private
$Ansible.Changed = $true
}
else {
$Ansible.Changed = $false
}
}
- name: Ensure the OpenSSH server service is enabled and running
ansible.windows.win_service: