首页 > 解决方案 > 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)" 不起作用,我们尝试了多个选项,但似乎都没有。

有任何想法吗?

亲切的问候,

瑞克。

标签: powershellazure-devopspacker

解决方案


根据我的测试,管道变量确实无法传递给 powershell 环境变量。

解决方法:

您可以尝试使用Replace Token 任务将管道值传递给 Json 文件。

以下是步骤:

1.设置Json文件中的值。

{
  "variables": {
....
  "VagrantUserpassword": "#{VagrantUserPassword}#"
},
  1. 在脚本任务之前使用替换令牌任务。

在此处输入图像描述

  1. 在管道变量中设置值。

在此处输入图像描述

然后可以成功设置该值。

在此处输入图像描述

另一方面,我也在您的示例文件中发现了一些问题。

  1. "environment_vars": ["VagrantUserPassword={{user VagrantUserPassword}}"],VagrantUserPassword需要替换为VagrantUserpassword(["VagrantUserPassword={{user VagrantUserpassword}}"])。

注意:这是区分大小写的。

  1. 您需要使用$Env:VagrantUserPassword来替换$(VagrantUserPassword)

例如:

  "inline": [
    "Write-Host \"Automatically generated aws password is: $Env:VagrantUserPassword\"",
    "Write-Host \"Automatically generated aws password is: $Env:VAR5\""


  ]

推荐阅读