Ansible Remote provisioning not working in Docker container build I have a Packer template to start with a Docker container and provision it.  The shell script provisioners seem to be working, but, while the playbook runs and seems to succeed, unlike the shell provisioners, it seems to not be applying to the Docker container.

packer-template.json

{
  "builders": [
        {
            "type": "docker",
            "image": "chenjr0719/ubuntu-unity-novnc",
            "commit": "true"
        }
    ],
   "provisioners": [
        {
            "type": "shell",
            "inline": ["sudo apt-get update"]
        },
        {
            "type": "shell",
            "script": "setup_ansible.sh"
        },
        {
            "type": "shell",
            "script": "add_ansible_roles.sh"
        },
        {
            "type": "ansible",
            "playbook_file": "./kubeadm.yml"
        }
   ]
}


setup_ansible.sh

sudo apt-get install software-properties-common -y
sudo apt-add-repository ppa:ansible/ansible -y
sudo apt-get update
sudo apt-get install ansible -y


add_ansible_roles.sh

sudo ansible-galaxy install djx339.k8s-kubeadm-master


kubeadm.yml

- name: install kubeadm
  hosts: localhost
  roles:
    - { role: djx339.k8s-kubeadm-install }


Output from the playbook

docker: PLAY [install kubeadm] *********************************************************
docker:
docker: TASK [Gathering Facts] *********************************************************
docker: ok: [localhost]
docker:
docker: TASK [djx339.k8s-kubeadm-install : include_tasks] ******************************
docker: included: /home/dw/.ansible/roles/djx339.k8s-kubeadm-install/tasks/setup-Debian.yml for localhost
docker:
docker: TASK [djx339.k8s-kubeadm-install : Sysctl] *************************************
docker: ok: [localhost] =&gt; (item={u'name': u'net.bridge.bridge-nf-call-iptables', u'value': 1})
docker:
docker: TASK [djx339.k8s-kubeadm-install : Add kuebeadm apt key] ***********************
docker: ok: [localhost]
docker:
docker: TASK [djx339.k8s-kubeadm-install : Add kubeadm apt repo] ***********************
docker: ok: [localhost]
docker:
docker: TASK [djx339.k8s-kubeadm-install : Install kubeadm] ****************************
docker: ok: [localhost] =&gt; (item=[u'kubelet', u'kubeadm', u'kubectl', u'kubernetes-cni'])
docker:
docker: PLAY RECAP *********************************************************************
docker: localhost                  : ok=6    changed=0    unreachable=0    failed=0


Running kubeadm in the docker container throws an error.

Why isn't the playbook applying? It works when I run it locally.

The error which I'm getting is:


  kubeadm not found


So basically it's not installing kubeadm.



Note: just noticed the mismatch between the role I'm adding in the add_ansible_roles.sh script and the playbook.
It appears that you are trying to run ansible locally inside the container but instead you are running it locally on the machine you use to run packer on(the provisioning one).

To run ansible in local mode you need to update the ansible part of your packer file to:

    {
        "type": "ansible-local",
        "playbook_file": "./kubeadm.yml"
    }


Also, I would suggest to add connection: local to your ansible playbook, to avoid unnecessary attempts for ssh connections.

- name: install kubeadm
  hosts: localhost
  connection: local
  roles:
    - { role: djx339.k8s-kubeadm-install }