You can get that error if your "kubeadm" cannot connect to the Kubernetes cluster using credentials from the configuration file. You can reproduce it by stopping the docker service on your master node.

There is no difference between running the command using Ansible or shell in your case, so it should work.

So, the only things I can suggest are:

  1. Verify that the Ansible role master is attached to the right host.
  2. Check if the Ansible user has access to the kubeadm configuration, its default path is /etc/kubernetes/admin.conf, and make sure that configuration is right. You might try to run the command as root using the become: true option.
Answer from Anton Kostenko on Stack Overflow
🌐
GitHub
github.com › IBM › ansible-kubernetes-ha-cluster › blob › master › roles › kubeadm-join-node › tasks › main.yaml
ansible-kubernetes-ha-cluster/roles/kubeadm-join-node/tasks/main.yaml at master · IBM/ansible-kubernetes-ha-cluster
July 18, 2024 - This repository provides Ansible Playbooks To setup Kubernetes HA on Redhat Enterprise Linux 7. The playbooks are mainly inspired by Kubeadm documentation and other ansible tentatives on github. The playbooks could be used separately or as one playbook for a fully fledged HA cluster.
Author   IBM
🌐
Medium
medium.com › @fenari.kostem › effortless-kubernetes-deployment-setting-up-a-cluster-with-ansible-and-kubeadm-cc40f9e716f4
Effortless Kubernetes Deployment: Setting Up a Cluster with Ansible and kubeadm | by Bora Köstem | Medium
November 8, 2024 - Finally, add_worker_node.yaml is imported to join the worker nodes to the cluster, allowing them to participate in workload distribution. This structure provides clear, step-by-step automation for creating a Kubernetes cluster. It handles everything from initial setup to cluster-wide integration, ensuring a streamlined, repeatable deployment process. By following this guide, we’ve automated the setup of a Kubernetes cluster using Ansible and kubeadm, streamlining what would otherwise be a complex and time-consuming process.
Discussions

kubernetes - kubeadm token create with Ansible - Stack Overflow
I'm running a Kubernetes cluster on bare metal and I'm writing an Ansible task to get the join command from a master node: - name: Get join command from master shell: kubeadm token create --prin... More on stackoverflow.com
🌐 stackoverflow.com
April 17, 2018
Ansible module for kubeadm
There is a whole collection https://docs.ansible.com/ansible/latest/collections/kubernetes/core/index.html More on reddit.com
🌐 r/ansible
9
4
March 1, 2023
Capturing the kubeadm join command from kubeadm init

you can generate a join command command "on-demand" by running the following on a master

kubeadm token create --print-join-command

Dump it to a file with

kubeadm token create --print-join-command > /tmp/join.sh

However I find it easier to specify the token during init, this way you alreay know the join command for the workers

More on reddit.com
🌐 r/kubernetes
4
0
March 24, 2020
Ansible playbook for kubeadm based installation of latest version of kubernetes
Ansible playbook for a kubeadm-based Kubernetes installation on Linux (Red Hat, Debian, and SUSE-based distributions) with a single control plane… More on reddit.com
🌐 r/kubernetes
8
11
October 6, 2024
🌐
Medium
medium.com › @a.j.longchamps › home-lab-kubernetes-part-3-initializing-the-kubernetes-cluster-with-ansible-d979dc1aeea0
Home Lab Kubernetes Part 3: Initializing The Kubernetes Cluster With Ansible | by Aaron Longchamps | Medium
June 19, 2024 - In 5 steps, I do the following: ... (more on this below) ... Generate the join command with kubeadm token create --print-join-command and save it in the variable {{ join_command_output }}...
🌐
GitHub
github.com › npflan › k8s-ansible
GitHub - npflan/k8s-ansible: Ansible for deploying Kubernetes with KubeAdm · GitHub
Ansible for deploying Kubernetes with KubeAdm. Contribute to npflan/k8s-ansible development by creating an account on GitHub.
Starred by 8 users
Forked by 3 users
🌐
Jnidzwetzki
jnidzwetzki.github.io › 2022 › 02 › 02 › install-kubernetes.html
Install Kubernetes using Ansible / Kubeadm | Jan’s website and blog
February 2, 2022 - To start using your cluster, you ... you can join any number of worker nodes by running the following on each as root: kubeadm join 192.168.178.180:6443 --token 7k2z9o.atvgwvywu5pkd5w3 \ --discovery-token-ca-cert-hash ...
🌐
Checkmateq
checkmateq.com › home › ansible script to create kubernetes cluster
Ansible Script to create Kubernetes Cluster
October 19, 2024 - - hosts: workers become: yes gather_facts: yes tasks: - name: Copy join command from Ansiblehost to the worker nodes. become: yes copy: src: /tmp/kubernetes_join_command dest: /tmp/kubernetes_join_command mode: 0777 - name: Join the Worker nodes to the cluster.
Find elsewhere
🌐
GitHub
github.com › kairen › kubeadm-ansible
GitHub - kairen/kubeadm-ansible: Build a Kubernetes cluster using kubeadm via Ansible.
Build a Kubernetes cluster using kubeadm via Ansible. - kairen/kubeadm-ansible
Starred by 741 users
Forked by 381 users
Languages   Jinja 99.4% | Shell 0.6% | Jinja 99.4% | Shell 0.6%
🌐
Medium
ugurakgul.medium.com › bootstrapping-a-kubernetes-cluster-with-ansible-2d1a1155fcb9
Bootstrapping a Kubernetes Cluster with Ansible | by Uğur Akgül | Medium
January 6, 2021 - - name: Initializing the kubernetes cluster on the master node… become: yes shell: kubeadm init --pod-network-cidr=10.244.0.0/16- name: Creating .kube directory file: path: $HOME/.kube state: directory mode: 0755- name: Configuring users cluster access become: yes copy: src: /etc/kubernetes/admin.conf dest: /home/ansible/.kube/config remote_src: yes owner: ansible - name: Installing Pod network…. (Flannel) shell: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml · After successfully initializing our cluster and deploying Flannel, we can join some workers to this cluster now. First we need to get the join command from the master. ./roles/get-join-command/tasks/main.yml · - name: Getting join command from the master node… shell: kubeadm token create --print-join-command register: join_command - name: Setting join command…
🌐
GitHub
github.com › IBM › ansible-kubernetes-ha-cluster › tree › master › roles › kubeadm-join-node › tasks
ansible-kubernetes-ha-cluster/roles/kubeadm-join-node/tasks at master · IBM/ansible-kubernetes-ha-cluster
This repository provides Ansible ... The playbooks could be used separately or as one playbook for a fully fledged HA cluster. - ansible-kubernetes-ha-cluster/roles/kubeadm-join-node/tasks at master · IBM/ansible-kubernetes-ha-cluster...
Author   IBM
🌐
GitHub
github.com › geerlingguy › ansible-role-kubernetes
GitHub - geerlingguy/ansible-role-kubernetes: Ansible Role - Kubernetes · GitHub
Whether the particular server will serve as a Kubernetes control_plane (default) or node. The control plane will have kubeadm init run on it to intialize the entire K8s control plane, while nodes will have kubeadm join run on them to join them to the control_plane.
Starred by 621 users
Forked by 284 users
Languages   Jinja
🌐
E2enetworks
docs.e2enetworks.com › kubernetes › ansible_cd.html
DEPLOY A KUBERNETES CLUSTER USING ANSIBLE | E2E Networks documentation
- hosts: workers become: yes gather_facts: yes tasks: - name: Copy join command from Ansiblehost to the worker nodes. become: yes copy: src: /tmp/kubernetes_join_command dest: /tmp/kubernetes_join_command mode: 0777 - name: Join the Worker nodes to the cluster.
🌐
Spacelift
spacelift.io › blog › ansible-kubernetes
How to Manage Kubernetes with Ansible [Tutorial]
October 10, 2025 - Let’s create kube_workers.yml under ~/ansible/playbooks/. Please make sure to replace YOUR_MASTER_IP with the IP address of your master node and also replace YOUR_USERPROFILE_NAME with the name of your user profile that is in your /home/ directory that you are installing Kubernetes under. - name: Configure Join Commands on Master Node hosts: master become: yes tasks: - name: Retrieve Join Command shell: kubeadm token create --print-join-command register: join_command_raw - name: Set Join Command set_fact: join_command: "{{ join_command_raw.stdout_lines[0] }}" - name: Join Worker Nodes hosts:
🌐
Dev-eth0
dev-eth0.de › 2019 › 01 › 04 › kubernetes-cluster-ansible
dev-eth0.de - Setup a Kubernetes Cluster with Ansible
January 4, 2019 - In this post, we will setup a Kubernetes Cluster using Ansible and Kubeadm.
🌐
GitHub
github.com › ContainerSolutions › kubadm-ansible › blob › master › create.yml
kubadm-ansible/create.yml at master · ContainerSolutions/kubadm-ansible
local_action: lineinfile dest=.kube/{{ ansible_hostname }} regexp='^(.*server:\ https://).*:(\d*)$' line='\1{{ ansible_hostname }}:\2' backrefs=yes · become: false · · - hosts: nodes · become: yes · tasks: - name: join cluster · command: kubeadm join --skip-preflight-checks --token={{ hostvars[groups['master'][0]]['kubeadm_token'].stdout }} {{ hostvars[groups['master'][0]]["ansible_eth0"]["ipv4"]["address"] }} args: creates: /etc/kubernetes/kubelet.conf
Author   ContainerSolutions
🌐
Linux sysadmins
linuxsysadmins.com › install-kubernetes-cluster-with-ansible
Install Kubernetes Cluster with Ansible on Ubuntu in 5 minutes
March 7, 2022 - Once the master server setup is completed, start with working on joining the worker nodes with the master by running the below playbook. --- - hosts: workers remote_user: ansible become: yes become_method: sudo become_user: root gather_facts: yes connection: ssh tasks: - name: Copy join command to worker nodes.
🌐
Medium
medium.com › @akshay01gh › automate-k8s-cluster-with-ansible-1e6b8e03d166
Automate K8S Cluster with Ansible | by Akshay | Medium
October 4, 2023 - kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
🌐
Kubernetes
kubernetes.io › docs › setup › production-environment › tools › kubeadm › create-cluster-kubeadm
Creating a cluster with kubeadm | Kubernetes
2 weeks ago - Run "kubectl apply -f ... the following on each node as root: kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>...