Packer-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","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}#"
},
  1. 在脚本任务之前使用“替换令牌”任务。

enter image description here

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

enter image description here

然后可以成功设置该值。

enter image description here

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

  1. “ environment_vars”:[“ VagrantUserPassword = {{用户VagrantUserPassword}}”]],VagrantUserPassword需要替换为VagrantUserpassword([“ VagrantUserPassword = {{用户{{ 1}}}}“])。

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

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

例如:

$(VagrantUserPassword)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-