I have an Ansible script that installs a Kubernetes cluster and is supposed to enable the dashboard. One task is giving me issues:
- name: Expose Dashboard UI
shell: kubectl proxy --port=8001 --address={{ hostvars['master'].master_node_ip }} --accept-hosts="^*quot; >> dashboard_started.txt
args:
chdir: $HOME
creates: dashboard_started.txt
The problem is that this works, but the command kubectl proxy
is blocking: you can't type in another command until you ctrl+c out of the command, at which point the dashboard is inaccessible. My Ansible script freezes from performing this command. I can successfully connect to the dashboard in my browser while Ansible is frozen. But I need Ansible to perform other tasks after this one as well. I have tried adding an ampersand &
at the end of my command:
kubectl proxy --port=8001 --address={{ hostvars['master'].master_node_ip }} --accept-hosts="^*quot; >> dashboard_started.txt &
Or
kubectl proxy --port=8001 --address={{ hostvars['master'].master_node_ip }} --accept-hosts="^*quot; & >> dashboard_started.txt
And while both these commands cause Ansible to execute and pass over my task, I can't reach the dashboard. Using the jobs
command on the machine the command is run on shows no background tasks, either for root or the Ansible user.
What am I doing wrong?
EDIT:
To those reading this: DO NOT DO THIS UNLESS YOU CAN ACCESS THE DASHBOARD FROM LOCALHOST. If you are running Kubernetes Dashboard in a VM or on some external server, and are trying to access it from another machine (the VM host for example), you will NOT be able to log in. See here:
https://github.com/kubernetes/dashboard/blob/master/docs/user/accessing-dashboard/1.7.x-and-above.md
NOTE: Dashboard should not be exposed publicly using kubectl proxy command as it only allows HTTP connection. For domains other than localhost and 127.0.0.1 it will not be possible to sign in. Nothing will happen after clicking Sign in button on login page.
You could run the task with the asynchronous option. For example:
- name: Expose Dashboard UI
shell: "(kubectl proxy --port=8001 --address={{ hostvars['master'].master_node_ip }} --accept-hosts="^*quot; >> dashboard_started.txt >/dev/null 2>&1 &)"
args:
chdir: $HOME
creates: dashboard_started.txt
async: 10
poll: 0
When poll is 0, Ansible will start the task and immediately move on to the next one without waiting for a result.
I personally added the subshell parentheses though i suppose that there is no need to use them, async itself does the trick I hope!
Hope it helps!
https://docs.ansible.com/ansible/latest/user_guide/playbooks_async.html