Ansible with shell-local on Packer 1.2.4

248 views
Skip to first unread message

Adam Robinson

unread,
May 29, 2018, 9:22:49 PM5/29/18
to Packer
I have tweaked some code I have for building Windows VMs on AWS, Azure, and GCP to use shell-local instead of ansible remote.  Its working, but I'm wondering if I'm missing a simpler way to do this?

I changed 
{
  "type": "ansible",
  "playbook_file": "poc-base-image-playbook/main.yml",
  "extra_arguments": [
    "--connection", "packer",
    "--extra-vars", "ansible_shell_type=powershell ansible_shell_executable=None"
  ]
}
to
{
  "type": "powershell",
  "inline": [
    "\"[{{build_name}}]\" > C:\\Windows\\Temp\\host",
    "switch -Wildcard ($env:PACKER_BUILDER_TYPE) {",
    "  \"amazon*\" { $publicipv4 = Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/public-ipv4",
    "     $publicipv4.Content >> C:\\Windows\\Temp\\host }",
    "     $publicipv4.Content >> C:\\Windows\\Temp\\host }",
    "  \"googlecompute\" { $publicipv4 = Invoke-WebRequest -Uri http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip -Headers @{\"Metadata-Flavor\" = \"Google\"}",
    "     [system.Text.Encoding]::UTF8.GetString($publicipv4.RawContentStream.ToArray()) >> C:\\Windows\\Temp\\host }",
    "  Default { Throw \"Unknown PACKER_BUILDER_TYPE\" }",
    "}"
  ]
},
{
  "type": "file",
  "direction": "download",
  "source": "C:\\Windows\\Temp\\host",
  "destination": "poc-base-image-playbook/{{build_name}}-host"
},
{
  "type": "shell-local",
  "inline": [
    "export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES",
    "case \"$PACKER_BUILDER_TYPE\" in",
    "  amazon*) ANSIBLE_USER={{user `aws_winrm_user`}};;",
    "  azure*) ANSIBLE_USER={{user `azure_winrm_user`}};;",
    "  googlecompute) ANSIBLE_USER={{user `gcp_winrm_user`}};;",
    "esac",
    "dos2unix poc-base-image-playbook/{{build_name}}-host",
    "ansible-playbook -v -i poc-base-image-playbook/{{build_name}}-host --extra-vars \"ansible_user=$ANSIBLE_USER ansible_password={{.WinRMPassword}} ansible_become_pass={{.WinRMPassword}} ansible_port=5986 ansible_connection=winrm ansible_winrm_server_cert_validation=ignore\" poc-base-image-playbook/main.yml"
  ]
},



Thanks,
Adam Robinson
Virtualization and Cloud Infrastructure Senior
Information and Technology Services
University of Michigan

Benjamin Mitchell

unread,
May 8, 2019, 12:22:00 PM5/8/19
to Packer
Any chance you could publish your code? I'm building out a similar pipeline and this looks like a great starting point for me. 

Thanks
Ben

Adam Robinson

unread,
May 8, 2019, 3:56:14 PM5/8/19
to packe...@googlegroups.com
Hi Ben,
This is the repo I was playing around in - https://github.com/umich-vci/windows-base-poc-image.  I haven't touched this code in a while, and the code we are using for our actual image builds is in an internal git repository.  I just pushed some changes that I had lingering around on my laptop so hopefully I didn't break anything :-).

I do remember that one of my colleagues figured out a way to clean up the previously mentioned code  - no more dos2unix!

Thanks,
Adam Robinson
Virtualization and Cloud Infrastructure Senior
Information and Technology Services
University of Michigan
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/1026dc0e-0a6f-46eb-9f36-383d5807303c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

benmitc...@gmail.com

unread,
May 9, 2019, 12:46:37 PM5/9/19
to packe...@googlegroups.com

Awesome thanks! I’ve decided to go with shell-local provisioner vs ansible to use native WinRM vs the custom connection. I like how you build the logic into the playbook for all the cloud providers – I’m gonna steal that part 😃

 

 

Ben

You received this message because you are subscribed to a topic in the Google Groups "Packer" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/packer-tool/Qr53-eYdVT0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to packer-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/CANh5szof5W_M9mXwDn6D%2Bf54vXKbEX9_1m%2Bk%3DMHxZO2L93tYfA%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages