Does anyone have a working example of Ubuntu Xenial host, builder=LXD, provisioner=ansible (remote)

329 views
Skip to first unread message

Peter Goodall

unread,
Jan 5, 2018, 3:07:47 AM1/5/18
to Packer
Hello all,

I have struggled for days trying to build and provision an ubuntu:xenial LXD image with provisioning using ansible. I have reported a bug: https://github.com/hashicorp/packer/issues/5740

I'm wondering if anyone has files for a simple working example of Packer building an image for lxd and using ansible as the provisioner to install a package
Not because that's exactly what I want to do, but because its a reasonable example that would show ansible-l working.

N.B. The ansible remote provisioner is named 'ansible' and runs the ansible plays over ssh on the guest being built. It does not install ansible on the guest. 

Cheers,
--Peter G

Peter Goodall

unread,
Jan 5, 2018, 3:09:37 AM1/5/18
to Packer
 also... the lxd builder runs shell commands fine remotely on the guest.

Rickard von Essen

unread,
Jan 5, 2018, 6:07:49 PM1/5/18
to packe...@googlegroups.com
I think I have that somewhere, let me get back with it tomorrow.

What's your extra_arguments to ansible? 

On Jan 5, 2018 09:09, "Peter Goodall" <pjgo...@gmail.com> wrote:
 also... the lxd builder runs shell commands fine remotely on the guest.

--
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/2befac29-9098-4318-a03d-571664e502d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Goodall

unread,
Jan 5, 2018, 10:22:18 PM1/5/18
to Packer
Thanks Rickard,

"extra_arguments": ["-vvvvv"]

--Peter G


On Saturday, January 6, 2018 at 10:07:49 AM UTC+11, Rickard von Essen wrote:
I think I have that somewhere, let me get back with it tomorrow.

What's your extra_arguments to ansible? 
On Jan 5, 2018 09:09, "Peter Goodall" <pjgo...@gmail.com> wrote:
 also... the lxd builder runs shell commands fine remotely on the guest.

--
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.

Rickard von Essen

unread,
Jan 6, 2018, 2:27:36 AM1/6/18
to Packer
Packer doesn't assume the LXD container is running with SSH and thus doesn't know anything about a potential SSH setup. Packer uses the lxc command to upload, download, and exec commands in the container. To get that working with Ansible you have to use a special connection plugin, see https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/connection/lxd.py.


If that doesn't solve please provide your template and the output of running: PACKER_LOG=1 packer build template.json

Peter Goodall

unread,
Jan 14, 2018, 8:45:36 PM1/14/18
to Packer
Thanks Rikard,
I will conduct experiments :-)

Peter Goodall

unread,
Feb 2, 2018, 9:21:43 PM2/2/18
to Packer
Hi Rickard,

The documentation for LXD Ansible provisioner seems to contradict what you are saying about communication to the container being provisioned:
"The ansible Packer provisioner runs Ansible playbooks. It dynamically creates an Ansible inventory file configured to use SSH, runs an SSH server, executes ansible-playbook, and marshals Ansible plays through the SSH server to the machine being provisioned by Packer. "
 
I tried using the LXD communicator, but was unable to work-out what to call the host in the ansible playbook.  

As I say there are no fully worked examples I can find.  My concern is that the LXD gets ignored by developers because there are few reliable tutorials, and the tutorials don't get written because there are no users.

Rickard von Essen

unread,
Feb 3, 2018, 5:53:43 AM2/3/18
to packe...@googlegroups.com
It doesn't really contradict what I say. But maybe I can explain better than the docs.

Packer has an abstraction called communicator which all provisioners uses to upload, download, and execute commands on the guest. This doesn't fit very well with Ansible since it self handles the connection. 

The somewhat complex solution to this is that Packer starts a SSH proxy which Ansible connects to, the proxy in turn uses the connector.

                           Host                                                |        Guest
                     .---------------.        .-----------------------.          .---------.
Ansible -->   | SSH Proxy |===| Connector (LXD) | ----> | Guest |
                     `---------------´        `----------------------´           `--------´ 

Unfortunately this doesn't seems to work very well with the Ansible LXD connector. But this is a work around:

Ensure that you have a recent version of Ansible (I used 2.4.3.0) which have the LXD connector plugin (check with: ansible-doc -t connection lxd).
I used Packer 1.1.3, but I think anything since 1.0 should probably work.

template.json
{
  "builders": [
    {
      "type": "lxd",
      "name": "lxd-xenial",
      "image": "ubuntu-daily:xenial",
      "output_image": "ubuntu-xenial",
      "publish_properties": {
        "description": "Trivial repackage with Packer"
      }
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "inline": [ "apt-get update && apt-get install -y python-minimal" ]
    },
    {
      "type": "ansible",
      "playbook_file": "./playbook.yml",
      "extra_arguments": [ "-c", "lxd", "-i", "packer-lxd-xenial,", "-l", "packer-lxd-xenial" ]
    }
  ]
}

playbook.yml
---
- hosts: all
  tasks:
    - name: Shelling
      shell: echo "Hello world"




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/dfbecf2c-fed6-4885-8e6d-d006e5314bd2%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages