Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can user variables be used in external files i.e. autounattend.xml #2867

Closed
michaelosmith opened this issue Oct 26, 2015 · 6 comments
Closed
Labels

Comments

@michaelosmith
Copy link

I am building windows servers and the flexibility of using user variables is great.
I was wondering if it is possible to apply a user variable into the Autounattend.xml file that is used to automate the setup of windows machines?

@kikitux
Copy link
Collaborator

kikitux commented Oct 26, 2015

Hello,

At the moment files in http, floppy or uploaded with packer file provisioning are copied as is.

At somepoint you may call an powershell script and modify tokens:

For examples, have a look here:

http://stackoverflow.com/questions/17144355/string-replace-file-content-with-powershell

Use (V3 version):

(gc c:\temp\test.txt).replace('[MYID]', 'MyValue') | sc c:\temp\test.txt
Or for V2:

(gc c:\temp\test.txt) -replace '\[MYID\]', 'MyValue'

@michaelosmith
Copy link
Author

thanks @kikitux, that makes sense.
I'll see if I can put something together.

@deluxebrain
Copy link

Hi @michaelosmith - just wondering if you got anywhere with using variables inside autounattend ? I'm using a hacky solution of pre-transforming a templated autounattend file into a temp file that is reference from the packer build command. Works fine - but not very elegant and can lead to the temp file lying around.

@ryanwischkaemper
Copy link

@deluxebrain I'm about to do the same thing. I agree that it is slightly hacky, but it beats having to update the same value across several files every time a change comes along.

@mwhooker
Copy link
Contributor

going to close as a dupe of #3961 etc. Will track in a meta-issue I'm creating

@michaelosmith
Copy link
Author

@deluxebrain I managed to solve this by using the following basic script.

param(
[string]$hostname
)

$ScriptDir = Split-Path -parent $MyInvocation.MyCommand.Path

(Get-Content $ScriptDir\answer_files\Autounattend_template.xml).replace('[HOSTNAME]', $hostname) | Set-Content $ScriptDir\answer_files\Autounattend.xml
$exe = "C:\Windows\System32\packer.exe"
&$exe build -var-file="variables.json" -var "vm_name=$hostname" template.json

It is just powershell that takes in a hostname parameter (that's all I wanted to change) and then replaces my template value and creates a new Autounattend.xml.

I am sure this could be repeated for many different variables.
A bit hacky but it works for me :-)

@ghost ghost locked and limited conversation to collaborators Apr 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants