"Packer" Ansible connection for WinRM breaks on Ansible

1,405 views
Skip to first unread message

Lawrence

unread,
Nov 22, 2017, 11:04:54 AM11/22/17
to Packer
Have a similar issue as seen in https://github.com/hashicorp/packer/issues/4904, and was wondering if anyone has any updates, or thoughts regarding this? 

Connecting from Mac OS X (host PC) to Windows 7, via winrm.

Thanks! 

Jordan Borean

unread,
Nov 22, 2017, 4:36:07 PM11/22/17
to Packer
It should be working on the latest version but personally I think the way Packer has gone about this is wrong. I recommend using the shell-local provision to call ansible-playbook like you would on the commandline so it runs it as Ansible intended. You do require the pywinrm package to be installed on the default python version but at least you are using the supported connection transport that comes with Ansible itself.

Alvaro Miranda Aguilera

unread,
Nov 23, 2017, 3:52:46 AM11/23/17
to packe...@googlegroups.com
Hello Jordan

Can you share your opinion here on what do you think is wrong, and what could be a different way to look into this?

all feedback is always welcome

alvaro.


On Wed, Nov 22, 2017 at 10:36 PM, Jordan Borean <jbor...@gmail.com> wrote:
It should be working on the latest version but personally I think the way Packer has gone about this is wrong. I recommend using the shell-local provision to call ansible-playbook like you would on the commandline so it runs it as Ansible intended. You do require the pywinrm package to be installed on the default python version but at least you are using the supported connection transport that comes with Ansible itself.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/84168986-0a56-43e2-ac9b-e25d31ca3210%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Alvaro

SurferL

unread,
Nov 23, 2017, 4:03:12 AM11/23/17
to Packer
Hi jordan,

Do you have an example of what you're suggesting?

Mine is currently something like:
"provisioners": [
{
"type": "ansible",
"user": "{{ user `username` }}",
"playbook_file": "./ansible/site.yml",
"groups": [
"{{ user `ansible_group` }}"
],
"ansible_env_vars": [
"ANSIBLE_CONFIG=../../../ansible/ansible.cfg",
"ANSIBLE_HOST_KEY_CHECKING=False",
"ANSIBLE_SSH_ARGS='-o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=30m'"
],
"extra_arguments": [
"-vvv",
"--extra-vars", "ansible_shell_executable=None ansible_user={{ user `username` }} ansible_password={{ user `password` }} ansible_become_pass={{ user `password` }}",
"--connection", "packer"
]
}

SurferL

unread,
Nov 23, 2017, 4:38:51 AM11/23/17
to Packer
Although, now that I think about it, running ansible-local wouldn't be extremely feasible due to the fact that it would be running on a Windows machine which would not be great to setup.

Jordan Borean

unread,
Nov 23, 2017, 5:42:08 PM11/23/17
to Packer
You won't be able to run the ansible command on the Windows host itself as Ansible does not run as a controller on Windows. You can still use shell-local as that runs local to what is running Packer itself and not to the host being built itself, which in your case would be the MacOS host. In the end I've been able to create a wide range of Windows images using the virtualbox-iso provisioner on a Mac OS X and Fedora host using this method.

An example of how I did this using the default winrm connector as well as answer to a lot of the questions I had when it came to Packer and Windows can be at this blog post http://www.bloggingforlogging.com/2017/11/23/using-packer-to-create-windows-images/.

SurferL

unread,
Nov 24, 2017, 5:07:48 AM11/24/17
to Packer
Thanks for the tips!

Just to clarify: you're building the windows machine on the MacOS host? (as it seems that your hosts.ini file points to 127.0.0.1)

I would try use the shell-local command, but my machine is being built on VSphere (setup by Packer), and so I won't have the IP address needed to run the ansible command locally I think...

Jordan Borean

unread,
Nov 24, 2017, 6:46:28 AM11/24/17
to Packer
Yep I'm building on the host itself, when Packer creates the VM it sets up a forwarded port so I access it through 127.0.0.1. Sorry I can't be too much help on the VSphere side as I don't know too much about that process.

Thanks

Jordan

SurferL

unread,
Nov 24, 2017, 6:50:52 AM11/24/17
to Packer
Ah right - thanks for the clarification!

Not sure if I can do it this way as I won't know the IP address until the builder stage of Packer has finished. Unless there is some way of passing the IP address from the builder stage to the provisioner stage?

Rickard von Essen

unread,
Nov 24, 2017, 9:14:17 AM11/24/17
to packe...@googlegroups.com
You can use a shell and file provisioner to pass the ip to the host. Similar to this example (but you need to change hostname to ip etc.)

"provisioners": [
  {
    "type": "shell",
    "inline": [ "hostname > /tmp/hostname" ]
  },
  {
    "type": "file",
    "direction": "download",
    "source": "/tmp/hostname",
    "destination": "hostname"
  }
]
 

--
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+unsubscribe@googlegroups.com.

SurferL

unread,
Nov 24, 2017, 9:52:42 AM11/24/17
to Packer
Oo, so I can do something like this?

"provisioners": [
  {
    "type": "shell",
    "inline": [ "hostname > /tmp/hostname" ]
  },
  {
    "type": "file",
    "direction": "download",
    "source": "/tmp/hostname",
    "destination": "hostname"
  },
  {
    "type": "shell-local",
    "command": "ansible-playbook main.yml -i hostname -vv"
  }
]

(where hostname is in fact the ip address in a file of some description)

SurferL

unread,
Nov 24, 2017, 11:31:40 AM11/24/17
to Packer
Cool - I've managed to get a simple test working with something like this (going to a Windows host):

"provisioners": [
{
"type": "file",
"source": "ip.cmd",
"destination": "C:/tmp/ip.cmd"
},
{
"type": "windows-shell",
"inline": [
"echo [{{ user `ansible_group` }}] > C:/tmp/hosts",
"C:/tmp/ip.cmd"
]
},
{
"type": "file",
"direction": "download",
"source": "C:/tmp/hosts",
"destination": "./ansible/hosts"
},
{
"type": "shell-local",
"command": "ANSIBLE_CONFIG=./ansible.cfg ansible-playbook -v -i ./ansible/hosts -l \"{{ user `ansible_group` }}\" -e \"ansible_user={{ user `username` }} ansible_password={{ user `password` }} ansible_become_pass={{ user `password` }} ansible_port=5986 ansible_connection=winrm ansible_winrm_server_cert_validation=ignore \" ./ansible/site.yml"
}

where ip.cmd gets the IP address of the Windows host:

@echo off
FOR /F "tokens=2,3" %%A IN ('ping %computername% -n 1 -4') DO IF "from"== "%%A" set "IP=%%~B"
echo %IP:~0,-1% >> C:/tmp/hosts

Thanks Jordan and Rickard for your solutions/advice! Will test this out more thoroughly to hopefully have all my connection troubles go away! :)  

Vivek Vara

unread,
Jun 12, 2019, 7:37:41 AM6/12/19
to Packer
Dear Jordan,

This looks helping for me where I've Windows host and building Windows Image. Could you please help me to how to use Ansible in this case?
Reply all
Reply to author
Forward
0 new messages