|
1 | 1 | # =============================================== |
2 | 2 | # 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 |
4 | 4 | # =============================================== |
5 | 5 | # Tier: essential |
6 | 6 | # Dependencies: bootstrap, env |
7 | 7 |
|
8 | 8 | <# |
9 | 9 | .SYNOPSIS |
10 | | - Ansible helper functions and aliases for WSL execution. |
| 10 | + Ansible helper functions and aliases. |
11 | 11 |
|
12 | 12 | .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. |
15 | 16 |
|
16 | 17 | .NOTES |
17 | 18 | Module: PowerShell.Profile.Ansible |
18 | 19 | Author: PowerShell Profile |
19 | 20 | #> |
20 | 21 |
|
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 | + |
22 | 41 | <# |
23 | 42 | .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. |
29 | 44 |
|
30 | 45 | .PARAMETER Arguments |
31 | 46 | Arguments to pass to ansible. |
32 | 47 |
|
33 | | -.EXAMPLE |
34 | | - Invoke-Ansible --version |
35 | | -
|
36 | 48 | .EXAMPLE |
37 | 49 | Invoke-Ansible all -m ping |
38 | 50 | #> |
39 | 51 | function Invoke-Ansible { |
40 | 52 | [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 |
47 | 55 | } |
48 | 56 |
|
49 | | -# Ansible-playbook command wrapper for WSL with UTF-8 locale |
50 | 57 | <# |
51 | 58 | .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. |
57 | 60 |
|
58 | 61 | .PARAMETER Arguments |
59 | 62 | Arguments to pass to ansible-playbook. |
60 | 63 |
|
61 | | -.EXAMPLE |
62 | | - Invoke-AnsiblePlaybook playbook.yml |
63 | | -
|
64 | 64 | .EXAMPLE |
65 | 65 | Invoke-AnsiblePlaybook playbook.yml --check |
66 | 66 | #> |
67 | 67 | function Invoke-AnsiblePlaybook { |
68 | 68 | [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 |
75 | 71 | } |
76 | 72 |
|
77 | | -# Ansible-galaxy command wrapper for WSL with UTF-8 locale |
78 | 73 | <# |
79 | 74 | .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. |
85 | 76 |
|
86 | 77 | .PARAMETER Arguments |
87 | 78 | Arguments to pass to ansible-galaxy. |
88 | 79 |
|
89 | 80 | .EXAMPLE |
90 | 81 | Invoke-AnsibleGalaxy install geerlingguy.docker |
91 | | -
|
92 | | -.EXAMPLE |
93 | | - Invoke-AnsibleGalaxy list |
94 | 82 | #> |
95 | 83 | function Invoke-AnsibleGalaxy { |
96 | 84 | [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 |
103 | 87 | } |
104 | 88 |
|
105 | | -# Ansible-vault command wrapper for WSL with UTF-8 locale |
106 | 89 | <# |
107 | 90 | .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. |
113 | 92 |
|
114 | 93 | .PARAMETER Arguments |
115 | 94 | Arguments to pass to ansible-vault. |
116 | 95 |
|
117 | 96 | .EXAMPLE |
118 | 97 | Invoke-AnsibleVault encrypt secrets.yml |
119 | | -
|
120 | | -.EXAMPLE |
121 | | - Invoke-AnsibleVault decrypt secrets.yml |
122 | 98 | #> |
123 | 99 | function Invoke-AnsibleVault { |
124 | 100 | [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 |
131 | 103 | } |
132 | 104 |
|
133 | | -# Ansible-doc command wrapper for WSL with UTF-8 locale |
134 | 105 | <# |
135 | 106 | .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. |
141 | 108 |
|
142 | 109 | .PARAMETER Arguments |
143 | 110 | Arguments to pass to ansible-doc. |
144 | 111 |
|
145 | 112 | .EXAMPLE |
146 | 113 | Get-AnsibleDoc ping |
147 | | -
|
148 | | -.EXAMPLE |
149 | | - Get-AnsibleDoc -l |
150 | 114 | #> |
151 | 115 | function Get-AnsibleDoc { |
152 | 116 | [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 |
159 | 119 | } |
160 | 120 |
|
161 | | -# Ansible-inventory command wrapper for WSL with UTF-8 locale |
162 | 121 | <# |
163 | 122 | .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. |
169 | 124 |
|
170 | 125 | .PARAMETER Arguments |
171 | 126 | Arguments to pass to ansible-inventory. |
172 | 127 |
|
173 | 128 | .EXAMPLE |
174 | 129 | Get-AnsibleInventory --list |
175 | | -
|
176 | | -.EXAMPLE |
177 | | - Get-AnsibleInventory --host webserver |
178 | 130 | #> |
179 | 131 | function Get-AnsibleInventory { |
180 | 132 | [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 |
187 | 135 | } |
188 | 136 |
|
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' |
198 | 143 | Set-AgentModeAlias -Name 'ansible-inventory' -Target 'Get-AnsibleInventory' |
0 commit comments