3

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

2
  • What error does it throw?
    – ydaetskcoR
    Commented Feb 23, 2018 at 8:12
  • kubeadm not found. It is not installing kubeadm
    – David West
    Commented Feb 24, 2018 at 7:27

1 Answer 1

3

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 }
5
  • Tried it. Now I get ERROR! the role 'djx339.k8s-kubeadm-install' was not found
    – David West
    Commented Feb 27, 2018 at 15:52
  • Can you post the contents of setup_ansible.sh as well?
    – man0v
    Commented Feb 27, 2018 at 15:54
  • posted the contents of setup_ansible.sh
    – David West
    Commented Feb 27, 2018 at 15:58
  • 1
    I see the error. in add_ansible_roles.sh I'm installing the wrong role.
    – David West
    Commented Feb 27, 2018 at 16:01
  • Instead of add_ansible_roles.sh use packer.io/docs/provisioners/ansible-local.html#galaxy_file Commented Nov 24, 2018 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.