update powershell provisioner docs to include HCL examples (#9759)
This commit is contained in:
parent
e97c56dc72
commit
b5b28c55fb
|
@ -22,6 +22,9 @@ for details.
|
||||||
|
|
||||||
The example below is fully functional.
|
The example below is fully functional.
|
||||||
|
|
||||||
|
<Tabs>
|
||||||
|
<Tab heading="JSON">
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"type": "powershell",
|
"type": "powershell",
|
||||||
|
@ -29,6 +32,18 @@ The example below is fully functional.
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
<Tab heading="HCL2">
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
provisioner "powershell" {
|
||||||
|
inline = ["dir c:\\"]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
## Configuration Reference
|
## Configuration Reference
|
||||||
|
|
||||||
@include 'provisioners/shell-config.mdx'
|
@include 'provisioners/shell-config.mdx'
|
||||||
|
@ -63,17 +78,34 @@ The example below is fully functional.
|
||||||
This is a [template engine](/docs/templates/engine). Therefore, you
|
This is a [template engine](/docs/templates/engine). Therefore, you
|
||||||
may use user variables and template functions in this field. If you are
|
may use user variables and template functions in this field. If you are
|
||||||
running on AWS, Azure, Google Compute, or OpenStack and would like to access
|
running on AWS, Azure, Google Compute, or OpenStack and would like to access
|
||||||
the generated password that Packer uses to connect to the instance via WinRM,
|
the autogenerated password that Packer uses to connect to the instance via
|
||||||
you can use the template variable `{{.WinRMPassword}}` to set this as an
|
WinRM, you can use the `build` template engine to inject it using
|
||||||
environment variable. For example:
|
`{{ build `Password`}}`. In HCL templates, you can do the same thing by
|
||||||
|
accessing the `build` variables For example:
|
||||||
|
|
||||||
```json
|
<Tabs>
|
||||||
{
|
<Tab heading="JSON">
|
||||||
"type": "powershell",
|
|
||||||
"environment_vars": "WINRMPASS={{.WinRMPassword}}",
|
```json
|
||||||
"inline": ["Write-Host \"Automatically generated aws password is: $Env:WINRMPASS\""]
|
{
|
||||||
},
|
"type": "powershell",
|
||||||
```
|
"environment_vars": ["WINRMPASS={{ build `Password`}}"],
|
||||||
|
"inline": ["Write-Host \"Automatically generated aws password is: $Env:WINRMPASS\""]
|
||||||
|
},
|
||||||
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
<Tab heading="HCL2">
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
provisioner "powershell" {
|
||||||
|
environment_vars = ["WINRMPASS=${build.Password}"]
|
||||||
|
inline = ["Write-Host \"Automatically generated aws password is: $Env:WINRMPASS\""]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
- `execute_command` (string) - The command to use to execute the script. By
|
- `execute_command` (string) - The command to use to execute the script. By
|
||||||
default this is as follows:
|
default this is as follows:
|
||||||
|
@ -103,23 +135,64 @@ The example below is fully functional.
|
||||||
|
|
||||||
This is a [template engine](/docs/templates/engine). Therefore, you
|
This is a [template engine](/docs/templates/engine). Therefore, you
|
||||||
may use user variables and template functions in this field. If you are
|
may use user variables and template functions in this field. If you are
|
||||||
running a build on AWS, Azure, Google Compute,
|
running on AWS, Azure, Google Compute, or OpenStack and would like to access
|
||||||
or OpenStack and would like to run using the generated password that Packer
|
the autogenerated password that Packer uses to connect to the instance via
|
||||||
uses to connect to the instance via WinRM, you may do so by using the
|
WinRM, you can use the `build` template engine to inject it using
|
||||||
template variable {{.WinRMPassword}}. For example:
|
`{{ build `Password`}}`. In HCL templates, you can do the same thing by
|
||||||
|
accessing the `build` variables For example:
|
||||||
|
|
||||||
```json
|
<Tabs>
|
||||||
|
<Tab heading="JSON">
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "powershell",
|
||||||
"elevated_user": "Administrator",
|
"elevated_user": "Administrator",
|
||||||
"elevated_password": "{{.WinRMPassword}}",
|
"elevated_password": "{{ build `Password`}}",
|
||||||
```
|
...
|
||||||
|
},
|
||||||
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
<Tab heading="HCL2">
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
provisioner "powershell" {
|
||||||
|
elevated_user = "Administrator"
|
||||||
|
elevated_password = build.Password
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
If you specify an empty `elevated_password` value then the PowerShell
|
If you specify an empty `elevated_password` value then the PowerShell
|
||||||
script is run as a service account. For example:
|
script is run as a service account. For example:
|
||||||
|
|
||||||
```json
|
<Tabs>
|
||||||
|
<Tab heading="JSON">
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "powershell",
|
||||||
"elevated_user": "SYSTEM",
|
"elevated_user": "SYSTEM",
|
||||||
"elevated_password": "",
|
"elevated_password": "",
|
||||||
```
|
...
|
||||||
|
},
|
||||||
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
<Tab heading="HCL2">
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
provisioner "powershell" {
|
||||||
|
elevated_user = "SYSTEM"
|
||||||
|
elevated_password = ""
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
- `execution_policy` - To run ps scripts on windows packer defaults this to
|
- `execution_policy` - To run ps scripts on windows packer defaults this to
|
||||||
"bypass" and wraps the command to run. Setting this to "none" will prevent
|
"bypass" and wraps the command to run. Setting this to "none" will prevent
|
||||||
|
@ -204,18 +277,32 @@ reconfigured to work on a remote system with
|
||||||
dollar sign backslash escaped so that it is not interpreted by the remote Bash
|
dollar sign backslash escaped so that it is not interpreted by the remote Bash
|
||||||
shell - Bash being the default shell for Cygwin environments.
|
shell - Bash being the default shell for Cygwin environments.
|
||||||
|
|
||||||
|
<Tabs>
|
||||||
|
<Tab heading="JSON">
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"provisioners": [
|
"provisioners": [
|
||||||
{
|
{
|
||||||
"type": "powershell",
|
"type": "powershell",
|
||||||
"execute_command": "powershell -executionpolicy bypass \"& { if (Test-Path variable:global:ProgressPreference){\\$ProgressPreference='SilentlyContinue'};. {{.Vars}}; &'{{.Path}}'; exit \\$LastExitCode }\"",
|
"execute_command": "powershell -executionpolicy bypass \"& { if (Test-Path variable:global:ProgressPreference){\\$ProgressPreference='SilentlyContinue'};. {{.Vars}}; &'{{.Path}}'; exit \\$LastExitCode }\"",
|
||||||
"inline": [
|
"inline": ["Write-Host \"Hello from PowerShell\""]
|
||||||
"Write-Host \"Hello from PowerShell\"",
|
}
|
||||||
]
|
]
|
||||||
}
|
|
||||||
]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
<Tab heading="HCL2">
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
provisioner "powershell" {
|
||||||
|
execute_command = "powershell -executionpolicy bypass \"& { if (Test-Path variable:global:ProgressPreference){\\$ProgressPreference='SilentlyContinue'};. {{.Vars}}; &'{{.Path}}'; exit \\$LastExitCode }\""
|
||||||
|
inline = [ "Write-Host \"Hello from PowerShell\""]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
## Packer's Handling of Characters Special to PowerShell
|
## Packer's Handling of Characters Special to PowerShell
|
||||||
|
|
||||||
The escape character in PowerShell is the `backtick`, also sometimes referred
|
The escape character in PowerShell is the `backtick`, also sometimes referred
|
||||||
|
@ -230,6 +317,9 @@ when they appear _directly_ in the users own scripts. Note that where double
|
||||||
quotes appear within double quotes, the addition of a backslash escape is
|
quotes appear within double quotes, the addition of a backslash escape is
|
||||||
required for the JSON template to be parsed correctly.
|
required for the JSON template to be parsed correctly.
|
||||||
|
|
||||||
|
<Tabs>
|
||||||
|
<Tab heading="JSON">
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"provisioners": [
|
"provisioners": [
|
||||||
{
|
{
|
||||||
|
@ -239,11 +329,30 @@ required for the JSON template to be parsed correctly.
|
||||||
"Write-Host \"A literal backtick `` must be escaped\"",
|
"Write-Host \"A literal backtick `` must be escaped\"",
|
||||||
"Write-Host \"Here `\"double quotes`\" must be escaped\"",
|
"Write-Host \"Here `\"double quotes`\" must be escaped\"",
|
||||||
"Write-Host \"Here `'single quotes`' don`'t really need to be\"",
|
"Write-Host \"Here `'single quotes`' don`'t really need to be\"",
|
||||||
"Write-Host \"escaped... but it doesn`'t hurt to do so.\"",
|
"Write-Host \"escaped... but it doesn`'t hurt to do so.\""
|
||||||
]
|
]
|
||||||
},
|
}
|
||||||
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
<Tab heading="HCL2">
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
provisioner "powershell" {
|
||||||
|
inline = [
|
||||||
|
"Write-Host \"A literal dollar `$ must be escaped\"",
|
||||||
|
"Write-Host \"A literal backtick `` must be escaped\"",
|
||||||
|
"Write-Host \"Here `\"double quotes`\" must be escaped\"",
|
||||||
|
"Write-Host \"Here `'single quotes`' don`'t really need to be\"",
|
||||||
|
"Write-Host \"escaped... but it doesn`'t hurt to do so.\"",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
The above snippet should result in the following output on the Packer console:
|
The above snippet should result in the following output on the Packer console:
|
||||||
|
|
||||||
```shell-session
|
```shell-session
|
||||||
|
@ -262,6 +371,9 @@ Special characters appearing in user environment variable values and in the
|
||||||
`elevated_user` and `elevated_password` fields will be automatically dealt with
|
`elevated_user` and `elevated_password` fields will be automatically dealt with
|
||||||
for the user. There is no need to use escapes in these instances.
|
for the user. There is no need to use escapes in these instances.
|
||||||
|
|
||||||
|
<Tabs>
|
||||||
|
<Tab heading="JSON">
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"variables": {
|
"variables": {
|
||||||
|
@ -298,6 +410,46 @@ for the user. There is no need to use escapes in these instances.
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
<Tab heading="HCL2">
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
variable "psvar" {
|
||||||
|
type = string
|
||||||
|
default = "My$tring"
|
||||||
|
}
|
||||||
|
|
||||||
|
build {
|
||||||
|
sources = ["source.amazon-ebs.example"]
|
||||||
|
|
||||||
|
provisioner "powershell" {
|
||||||
|
elevated_user = "Administrator"
|
||||||
|
elevated_password = "Super$3cr3t!"
|
||||||
|
inline = ["Write-Output \"The dollar in the elevated_password is interpreted correctly\""]
|
||||||
|
}
|
||||||
|
provisioner "powershell" {
|
||||||
|
environment_vars = [
|
||||||
|
"VAR1=A$Dollar",
|
||||||
|
"VAR2=A`Backtick",
|
||||||
|
"VAR3=A'SingleQuote",
|
||||||
|
"VAR4=A\"DoubleQuote",
|
||||||
|
"VAR5=${var.psvar}",
|
||||||
|
]
|
||||||
|
inline = [
|
||||||
|
"Write-Output \"In the following examples the special character is interpreted correctly:\"",
|
||||||
|
"Write-Output \"The dollar in VAR1: $Env:VAR1\"",
|
||||||
|
"Write-Output \"The backtick in VAR2: $Env:VAR2\"",
|
||||||
|
"Write-Output \"The single quote in VAR3: $Env:VAR3\"",
|
||||||
|
"Write-Output \"The double quote in VAR4: $Env:VAR4\"",
|
||||||
|
"Write-Output \"The dollar in VAR5 (expanded from a user var): $Env:VAR5\"",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</Tab>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
The above snippet should result in the following output on the Packer console:
|
The above snippet should result in the following output on the Packer console:
|
||||||
|
|
||||||
```shell-session
|
```shell-session
|
||||||
|
|
Loading…
Reference in New Issue