How to run Ansible with Docker?

Hello all, I’m trying to get ansible to run on docker but can’t get it working. I’ve tried to “translated” JSON to HCL.
So I’m guessing the failure lies there, most likely with the “run_command” Don’t know how to make it HCL, I haven’t really use variables so don’t how it should work with Packer.

variable "ansible_host" {
  type = string
  default = "default"
}

variable "ansible_connection" {
  type = string
  default = "docker"
}

source "docker" "debian" {
  image  = "debian:buster"
  commit = true
  run_command = [ "-d", "-i", "-t", "--name", "{{user `ansible_host`}}", "{{.Image}}", "/bin/bash" ]
}

 provisioner "ansible" {
      playbook_file = "./playbook.yml"
      extra_arguments = ["--extra-vars","ansible_host=${var.ansible_host}", "ansible_connection=${var.ansible_connection}"]
    }

Error outputs

root:1:2: executing “root” at <user ansible_host>: error calling user: test
Error running container: template: root:1:2: executing “root” at <user ansible_host>: error calling user: test

Can anybody help?

Hi!

Is there any way you can share relevant parts of the playbook? It sounds like there’s a user which Ansible is expecting which is not present (test). The hostname and connection seem set correctly. Perhaps try setting the ansible_user variable to a user which is known to be present on the image (root).

Here’s the playbook.

---
- name: 'nvm'
  hosts: default
  become: yes

  tasks:
    - name: install git
      ansible.builtin.apt:
        name:
        - 'git'
        state: latest
    - name: clone git repo nvm
      ansible.builtin.git:
        repo: https://github.com/nvm-sh/nvm.git
        dest: /opt/nvm
    - name: install nvm
      ansible.builtin.command: bash /opt/nvm/install.sh

It’s pretty bare for the monument.
Should have also mention that I’m learning Ansible.

Thanks for that! I didn’t notice before that it seems that the Packer template doesn’t have a build block.

I still can’t tell what is causing the error you show in the output (no reference to test in the output for example), but it could be that you need move the provisioner block into a build block.

I can point you to one of the templates I use often:

This template is specifically for creating test instances for CI/CD of the Ansible role that the repo contains - your use case sounds pretty similar.

Interesting! Thanks I didn’t know about the plugins.

Here’s my build block.

variable "ansible_host" {
  type = string
  default = "default"
}

variable "ansible_connection" {
  type = string
  default = "docker"
}

build {
  name        = "${local.name}"
  description = local.description

  sources = [
    "source.docker.debian"
  ]
  
  provisioner "ansible" {
      playbook_file = "./playbook.yml"
      extra_arguments = ["--extra-vars","ansible_host=${var.ansible_host}", "ansible_connection=${var.ansible_connection}"]

    }
  
  post-processor "docker-tag" {
    repository = "debian-nvm"
    tags = ["latest"]
  }
}

here’s the exact output from when I run sudo packer build .

template.docker.debian: output will be in this color.

==> template.docker.debian: Creating a temporary directory for sharing data...
==> template.docker.debian: Pulling Docker image: debian:buster
    template.docker.debian: buster: Pulling from library/debian
    template.docker.debian: Digest: sha256:6c153b6c1b042fb70140a465e6d7c9c4a6112062afdc843c6a4d9c5bbfe5d0d5
    template.docker.debian: Status: Image is up to date for debian:buster
    template.docker.debian: docker.io/library/debian:buster
==> template.docker.debian: Starting docker container...
==> template.docker.debian: Error running container: template: root:1:2: executing "root" at <user `ansible_host`>: error calling user: test
Build 'template.docker.debian' errored after 1 second 201 milliseconds: Error running container: template: root:1:2: executing "root" at <user `ansible_host`>: error calling user: test