如何解决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","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.'}\""
},"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 task将管道值传递给Json文件。
以下是步骤:
1。在Json文件中设置值。
{
"variables": {
....
"VagrantUserpassword": "#{VagrantUserPassword}#"
},
- 在脚本任务之前使用“替换令牌”任务。
- 在管道变量中设置值。
然后可以成功设置该值。
另一方面,我还在您的示例文件中发现了一些问题。
- “ environment_vars”:[“ VagrantUserPassword = {{用户
VagrantUserPassword
}}”]],VagrantUserPassword
需要替换为VagrantUserpassword
([“ VagrantUserPassword = {{用户{{ 1}}}}“])。
注意:这是区分大小写的。
- 您需要使用
VagrantUserpassword
来替换$Env:VagrantUserPassword
例如:
$(VagrantUserPassword)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。