Skip to content

Commit 4c69874

Browse files
committed
fix: ansible cross-platform, CRLF newlines in asn1/edifact
- ansible.ps1: detect platform at load time; on Linux/macOS call ansible binaries directly, on Windows route through WSL. Previous code always called 'wsl ...' which fails on Linux. Guard moved to top so functions are only registered when ansible is actually usable (native OR via wsl). Extracted private script:Invoke-AnsibleBin helper to eliminate six copies of the same dispatch logic. - asn1.ps1 / edifact.ps1: replace hardcoded CRLF backtick sequences with [System.Environment]::NewLine so generated files use the correct line ending for the host OS (LF on Linux, CRLF on Windows)
1 parent 94dfab3 commit 4c69874

3 files changed

Lines changed: 56 additions & 109 deletions

File tree

profile.d/ansible.ps1

Lines changed: 48 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,198 +1,143 @@
11
# ===============================================
22
# ansible.ps1
3-
# Ansible wrappers that execute via WSL to ensure Linux toolchain compatibility
3+
# Ansible wrappers — runs natively on Linux/macOS, via WSL on Windows
44
# ===============================================
55
# Tier: essential
66
# Dependencies: bootstrap, env
77

88
<#
99
.SYNOPSIS
10-
Ansible helper functions and aliases for WSL execution.
10+
Ansible helper functions and aliases.
1111
1212
.DESCRIPTION
13-
Provides PowerShell functions and aliases for Ansible commands that execute
14-
via WSL with UTF-8 locale settings for Linux toolchain compatibility.
13+
Provides PowerShell functions and aliases for Ansible commands.
14+
On Linux/macOS ansible is invoked directly. On Windows it is run
15+
through WSL with UTF-8 locale settings.
1516
1617
.NOTES
1718
Module: PowerShell.Profile.Ansible
1819
Author: PowerShell Profile
1920
#>
2021

21-
# Ansible command wrapper for WSL with UTF-8 locale
22+
# Determine invocation strategy once at load time
23+
$script:_ansibleIsLinux = $IsLinux -or $IsMacOS
24+
$script:_ansibleHasWsl = -not $script:_ansibleIsLinux -and (Test-CachedCommand 'wsl')
25+
26+
# On Windows without WSL there is nothing to register — bail out early
27+
if (-not $script:_ansibleIsLinux -and -not $script:_ansibleHasWsl) { return }
28+
29+
# Private helper: invoke an ansible binary with the right strategy
30+
function script:Invoke-AnsibleBin {
31+
param([string]$Bin, [string[]]$Arguments)
32+
if ($script:_ansibleIsLinux) {
33+
& $Bin @Arguments
34+
}
35+
else {
36+
$cmd = "export LC_ALL=C.UTF-8 && export LANG=C.UTF-8 && $Bin $($Arguments -join ' ')"
37+
wsl bash -lc $cmd
38+
}
39+
}
40+
2241
<#
2342
.SYNOPSIS
24-
Runs Ansible commands via WSL with UTF-8 locale.
25-
26-
.DESCRIPTION
27-
Executes ansible commands through WSL bash shell with proper UTF-8 locale
28-
settings for Linux toolchain compatibility.
43+
Runs ansible with the correct invocation strategy for the current platform.
2944
3045
.PARAMETER Arguments
3146
Arguments to pass to ansible.
3247
33-
.EXAMPLE
34-
Invoke-Ansible --version
35-
3648
.EXAMPLE
3749
Invoke-Ansible all -m ping
3850
#>
3951
function Invoke-Ansible {
4052
[CmdletBinding()]
41-
param(
42-
[Parameter(ValueFromRemainingArguments = $true)]
43-
[string[]]$Arguments
44-
)
45-
46-
wsl bash -lc "export LC_ALL=C.UTF-8 && export LANG=C.UTF-8 && ansible $($Arguments -join ' ')"
53+
param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Arguments)
54+
script:Invoke-AnsibleBin 'ansible' $Arguments
4755
}
4856

49-
# Ansible-playbook command wrapper for WSL with UTF-8 locale
5057
<#
5158
.SYNOPSIS
52-
Runs Ansible playbook commands via WSL with UTF-8 locale.
53-
54-
.DESCRIPTION
55-
Executes ansible-playbook commands through WSL bash shell with proper UTF-8
56-
locale settings for Linux toolchain compatibility.
59+
Runs ansible-playbook with the correct invocation strategy for the current platform.
5760
5861
.PARAMETER Arguments
5962
Arguments to pass to ansible-playbook.
6063
61-
.EXAMPLE
62-
Invoke-AnsiblePlaybook playbook.yml
63-
6464
.EXAMPLE
6565
Invoke-AnsiblePlaybook playbook.yml --check
6666
#>
6767
function Invoke-AnsiblePlaybook {
6868
[CmdletBinding()]
69-
param(
70-
[Parameter(ValueFromRemainingArguments = $true)]
71-
[string[]]$Arguments
72-
)
73-
74-
wsl bash -lc "export LC_ALL=C.UTF-8 && export LANG=C.UTF-8 && ansible-playbook $($Arguments -join ' ')"
69+
param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Arguments)
70+
script:Invoke-AnsibleBin 'ansible-playbook' $Arguments
7571
}
7672

77-
# Ansible-galaxy command wrapper for WSL with UTF-8 locale
7873
<#
7974
.SYNOPSIS
80-
Runs Ansible Galaxy commands via WSL with UTF-8 locale.
81-
82-
.DESCRIPTION
83-
Executes ansible-galaxy commands through WSL bash shell with proper UTF-8
84-
locale settings for Linux toolchain compatibility.
75+
Runs ansible-galaxy with the correct invocation strategy for the current platform.
8576
8677
.PARAMETER Arguments
8778
Arguments to pass to ansible-galaxy.
8879
8980
.EXAMPLE
9081
Invoke-AnsibleGalaxy install geerlingguy.docker
91-
92-
.EXAMPLE
93-
Invoke-AnsibleGalaxy list
9482
#>
9583
function Invoke-AnsibleGalaxy {
9684
[CmdletBinding()]
97-
param(
98-
[Parameter(ValueFromRemainingArguments = $true)]
99-
[string[]]$Arguments
100-
)
101-
102-
wsl bash -lc "export LC_ALL=C.UTF-8 && export LANG=C.UTF-8 && ansible-galaxy $($Arguments -join ' ')"
85+
param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Arguments)
86+
script:Invoke-AnsibleBin 'ansible-galaxy' $Arguments
10387
}
10488

105-
# Ansible-vault command wrapper for WSL with UTF-8 locale
10689
<#
10790
.SYNOPSIS
108-
Runs Ansible Vault commands via WSL with UTF-8 locale.
109-
110-
.DESCRIPTION
111-
Executes ansible-vault commands through WSL bash shell with proper UTF-8
112-
locale settings for Linux toolchain compatibility.
91+
Runs ansible-vault with the correct invocation strategy for the current platform.
11392
11493
.PARAMETER Arguments
11594
Arguments to pass to ansible-vault.
11695
11796
.EXAMPLE
11897
Invoke-AnsibleVault encrypt secrets.yml
119-
120-
.EXAMPLE
121-
Invoke-AnsibleVault decrypt secrets.yml
12298
#>
12399
function Invoke-AnsibleVault {
124100
[CmdletBinding()]
125-
param(
126-
[Parameter(ValueFromRemainingArguments = $true)]
127-
[string[]]$Arguments
128-
)
129-
130-
wsl bash -lc "export LC_ALL=C.UTF-8 && export LANG=C.UTF-8 && ansible-vault $($Arguments -join ' ')"
101+
param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Arguments)
102+
script:Invoke-AnsibleBin 'ansible-vault' $Arguments
131103
}
132104

133-
# Ansible-doc command wrapper for WSL with UTF-8 locale
134105
<#
135106
.SYNOPSIS
136-
Runs Ansible documentation commands via WSL with UTF-8 locale.
137-
138-
.DESCRIPTION
139-
Executes ansible-doc commands through WSL bash shell with proper UTF-8
140-
locale settings for Linux toolchain compatibility.
107+
Runs ansible-doc with the correct invocation strategy for the current platform.
141108
142109
.PARAMETER Arguments
143110
Arguments to pass to ansible-doc.
144111
145112
.EXAMPLE
146113
Get-AnsibleDoc ping
147-
148-
.EXAMPLE
149-
Get-AnsibleDoc -l
150114
#>
151115
function Get-AnsibleDoc {
152116
[CmdletBinding()]
153-
param(
154-
[Parameter(ValueFromRemainingArguments = $true)]
155-
[string[]]$Arguments
156-
)
157-
158-
wsl bash -lc "export LC_ALL=C.UTF-8 && export LANG=C.UTF-8 && ansible-doc $($Arguments -join ' ')"
117+
param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Arguments)
118+
script:Invoke-AnsibleBin 'ansible-doc' $Arguments
159119
}
160120

161-
# Ansible-inventory command wrapper for WSL with UTF-8 locale
162121
<#
163122
.SYNOPSIS
164-
Runs Ansible inventory commands via WSL with UTF-8 locale.
165-
166-
.DESCRIPTION
167-
Executes ansible-inventory commands through WSL bash shell with proper UTF-8
168-
locale settings for Linux toolchain compatibility.
123+
Runs ansible-inventory with the correct invocation strategy for the current platform.
169124
170125
.PARAMETER Arguments
171126
Arguments to pass to ansible-inventory.
172127
173128
.EXAMPLE
174129
Get-AnsibleInventory --list
175-
176-
.EXAMPLE
177-
Get-AnsibleInventory --host webserver
178130
#>
179131
function Get-AnsibleInventory {
180132
[CmdletBinding()]
181-
param(
182-
[Parameter(ValueFromRemainingArguments = $true)]
183-
[string[]]$Arguments
184-
)
185-
186-
wsl bash -lc "export LC_ALL=C.UTF-8 && export LANG=C.UTF-8 && ansible-inventory $($Arguments -join ' ')"
133+
param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Arguments)
134+
script:Invoke-AnsibleBin 'ansible-inventory' $Arguments
187135
}
188136

189-
# Only register aliases when WSL is available (ansible runs via WSL)
190-
if (-not (Test-CachedCommand 'wsl')) { return }
191-
192-
# Create aliases for ansible commands
193-
Set-AgentModeAlias -Name 'ansible' -Target 'Invoke-Ansible'
194-
Set-AgentModeAlias -Name 'ansible-playbook' -Target 'Invoke-AnsiblePlaybook'
195-
Set-AgentModeAlias -Name 'ansible-galaxy' -Target 'Invoke-AnsibleGalaxy'
196-
Set-AgentModeAlias -Name 'ansible-vault' -Target 'Invoke-AnsibleVault'
197-
Set-AgentModeAlias -Name 'ansible-doc' -Target 'Get-AnsibleDoc'
137+
# Aliases
138+
Set-AgentModeAlias -Name 'ansible' -Target 'Invoke-Ansible'
139+
Set-AgentModeAlias -Name 'ansible-playbook' -Target 'Invoke-AnsiblePlaybook'
140+
Set-AgentModeAlias -Name 'ansible-galaxy' -Target 'Invoke-AnsibleGalaxy'
141+
Set-AgentModeAlias -Name 'ansible-vault' -Target 'Invoke-AnsibleVault'
142+
Set-AgentModeAlias -Name 'ansible-doc' -Target 'Get-AnsibleDoc'
198143
Set-AgentModeAlias -Name 'ansible-inventory' -Target 'Get-AnsibleInventory'

profile.d/conversion-modules/data/structured/asn1.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ function Initialize-FileConversion-Asn1 {
244244
foreach ($comp in $typeSpec.Components) {
245245
$components += " $($comp.Name) $($comp.Type)"
246246
}
247-
$specStr += "`r`n" + ($components -join ",`r`n")
247+
$nl = [System.Environment]::NewLine
248+
$specStr += $nl + ($components -join (",$nl"))
248249
}
249250
$specStr += '}'
250251
}
@@ -255,7 +256,8 @@ function Initialize-FileConversion-Asn1 {
255256
foreach ($comp in $typeSpec.Components) {
256257
$components += " $($comp.Name) $($comp.Type)"
257258
}
258-
$specStr += "`r`n" + ($components -join ",`r`n")
259+
$nl = [System.Environment]::NewLine
260+
$specStr += $nl + ($components -join (",$nl"))
259261
}
260262
$specStr += '}'
261263
}
@@ -273,7 +275,7 @@ function Initialize-FileConversion-Asn1 {
273275

274276
$asn1Lines += 'END'
275277

276-
$asn1Content = $asn1Lines -join "`r`n"
278+
$asn1Content = $asn1Lines -join $([System.Environment]::NewLine)
277279
Set-Content -LiteralPath $OutputPath -Value $asn1Content -Encoding UTF8
278280
}
279281
catch {
@@ -328,7 +330,7 @@ function Initialize-FileConversion-Asn1 {
328330
$xmlLines += " </Module>"
329331
$xmlLines += '</ASN1>'
330332

331-
$xmlContent = $xmlLines -join "`r`n"
333+
$xmlContent = $xmlLines -join $([System.Environment]::NewLine)
332334
Set-Content -LiteralPath $OutputPath -Value $xmlContent -Encoding UTF8
333335
}
334336
catch {

profile.d/conversion-modules/data/structured/edifact.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ function Initialize-FileConversion-Edifact {
248248
$xmlLines += ' </Interchange>'
249249
$xmlLines += '</EDIFACT>'
250250

251-
$xmlContent = $xmlLines -join "`r`n"
251+
$xmlContent = $xmlLines -join $([System.Environment]::NewLine)
252252
Set-Content -LiteralPath $OutputPath -Value $xmlContent -Encoding UTF8
253253
}
254254
catch {
@@ -295,7 +295,7 @@ function Initialize-FileConversion-Edifact {
295295
$csvLines += ($row -join ',')
296296
}
297297

298-
$csvContent = $csvLines -join "`r`n"
298+
$csvContent = $csvLines -join $([System.Environment]::NewLine)
299299
Set-Content -LiteralPath $OutputPath -Value $csvContent -Encoding UTF8
300300
}
301301
catch {

0 commit comments

Comments
 (0)