HiveBrain v1.2.0
Get Started
← Back to all entries
debugdockerMinor

Ansible Remote provisioning not working in Docker container build

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
provisioningcontainerdockerbuildworkingremotenotansible

Problem

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] => (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

Solution

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 }

Code Snippets

{
        "type": "ansible-local",
        "playbook_file": "./kubeadm.yml"
    }
- name: install kubeadm
  hosts: localhost
  connection: local
  roles:
    - { role: djx339.k8s-kubeadm-install }

Context

StackExchange DevOps Q#3418, answer score: 3

Revisions (0)

No revisions yet.