powershell - Packer - Powershell 传递变量
问题描述
目前,我们正在我们的 AWS 域中成功地使用打包程序(在位于 Azure DevOps 中的构建管道中)部署映像。现在我们想更进一步,我们正在尝试为未来的 Ansible 维护配置几个用户。因此,我们编写了一个脚本并尝试将其作为内联 Powershell 脚本,但这两个选项似乎都没有选择在 Azure DevOps 的变量组中设置的变量,所有其他变量都在成功使用。我的代码如下:
{
"variables": {
"build_version": "{{isotime \"2006.01.02.150405\"}}",
"aws_access_key": "$(aws_access_key)",
"aws_secret_key": "$(aws_secret_key)",
"region": "$(region)",
"vpc_id": "$(vpc_id)",
"subnet_id": "$(subnet_id)",
"security_group_id": "$(security_group_id)",
"VagrantUserpassword": "$(VagrantUserPassword)"
},
"builders": [
{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "{{user `region`}}",
"vpc_id": "{{user `vpc_id`}}",
"subnet_id": "{{user `subnet_id`}}",
"security_group_id": "{{user `security_group_id`}}",
"source_ami_filter": {
"filters": {
"name": "Windows_Server-2016-English-Full-Base-*",
"root-device-type": "ebs",
"virtualization-type": "hvm"
},
"most_recent": true,
"owners": [
"801119661308"
]
},
"ami_name": "WIN2016-CUSTOM-{{user `build_version`}}",
"instance_type": "t3.xlarge",
"user_data_file": "userdata.ps1",
"associate_public_ip_address": true,
"communicator": "winrm",
"winrm_username": "Administrator",
"winrm_timeout": "15m",
"winrm_use_ssl": true,
"winrm_insecure": true,
"ssh_interface": "private_ip"
}
],
"provisioners": [
{
"type": "powershell",
"environment_vars": ["VagrantUserPassword={{user `VagrantUserPassword`}}"],
"inline": [
"Install-WindowsFeature web-server,web-webserver,web-http-logging,web-stat-compression,web-dyn-compression,web-asp-net,web-mgmt-console,web-asp-net45",
"New-LocalUser -UserName 'Vagrant' -Description 'User is responsible for Ansible connection.' -Password '$(VagrantUserPassword)'"
]
},
{
"type": "powershell",
"environment_vars": ["VagrantUserPassword={{user `VagrantUserPassword`}}"],
"scripts": [
"scripts/DisableUAC.ps1",
"scripts/iiscompression.ps1",
"scripts/ChocoPackages.ps1",
"scripts/PrepareAnsibleUser.ps1"
]
},
{
"type": "windows-restart",
"restart_check_command": "powershell -command \"& {Write-Output 'Machine restarted.'}\""
},
{
"type": "powershell",
"inline": [
"C:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Scripts\\InitializeInstance.ps1 -Schedule",
"C:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Scripts\\SysprepInstance.ps1 -NoShutdown"
]
}
]
}
"VagrantUserpassword": "$(VagrantUserPassword)" 不起作用,我们尝试了多个选项,但似乎都没有。
有任何想法吗?
亲切的问候,
瑞克。
解决方案
根据我的测试,管道变量确实无法传递给 powershell 环境变量。
解决方法:
您可以尝试使用Replace Token 任务将管道值传递给 Json 文件。
以下是步骤:
1.设置Json文件中的值。
{
"variables": {
....
"VagrantUserpassword": "#{VagrantUserPassword}#"
},
- 在脚本任务之前使用替换令牌任务。
- 在管道变量中设置值。
然后可以成功设置该值。
另一方面,我也在您的示例文件中发现了一些问题。
- "environment_vars": ["VagrantUserPassword={{user
VagrantUserPassword
}}"],VagrantUserPassword
需要替换为VagrantUserpassword
(["VagrantUserPassword={{userVagrantUserpassword
}}"])。
注意:这是区分大小写的。
- 您需要使用
$Env:VagrantUserPassword
来替换$(VagrantUserPassword)
例如:
"inline": [
"Write-Host \"Automatically generated aws password is: $Env:VagrantUserPassword\"",
"Write-Host \"Automatically generated aws password is: $Env:VAR5\""
]
推荐阅读
- ios - 如何创建抽象函数以减少代码重复
- datetime - 日期转换处理 YYYY-MM-DD HH:MM:SS.SSS
- linux - 如何让linux机器在固定的时间内关闭
- java - 如何修复 Mockito 抛出“嵌套异常是 java.lang.NullPointerException”?
- javascript - 我有一个使用 heroku 部署的反应网站。Heroku 在直接导航到子页面时显示反斜杠。我该如何解决?
- firefox - 如何修复 Firefox 扩展弹出窗口中显示的滚动条
- javascript - 使用危险的SetInnerHtml 有什么风险以及是什么阻止了某人通过浏览器删除净化?
- angular - “错误:在更新 Angular cli 时找不到模块'@angular-devkit/schematics/tasks'”
- charts - 当数字低于零时如何更改谷歌表格中的图表颜色
- java - 当池生命周期为 LIFE_CYCLE_FAILED 时,使用 UCP 管理器调用 closeConnections() 时 UCP 连接是否关闭?