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
🌐
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%
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 › @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.
🌐
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 }}...
🌐
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 need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config [...] Then 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 sha256:9f00a315991af8873e050862a8884d8300424e481721d96afe19cdf0d236270f
🌐
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 › 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
🌐
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…
🌐
Kubernetes
kubernetes.io › blog › 2019 › 03 › 15 › kubernetes-setup-using-ansible-and-vagrant
Kubernetes Setup Using Ansible and Vagrant | Kubernetes
January 3, 2026 - - name: Initialize the Kubernetes cluster using kubeadm command: kubeadm init --apiserver-advertise-address="192.168.50.10" --apiserver-cert-extra-sans="192.168.50.10" --node-name k8s-master --pod-network-cidr=192.168.0.0/16
🌐
Kubernetes
kubernetes.io › docs › setup › production-environment › tools › kubeadm › create-cluster-kubeadm
Creating a cluster with kubeadm | Kubernetes
2 weeks ago - You can install and use kubeadm on various machines: your laptop, a set of cloud servers, a Raspberry Pi, and more. Whether you're deploying into the cloud or on-premises, you can integrate kubeadm into provisioning systems such as Ansible or Terraform.
🌐
Researchiteng
researchiteng.github.io › kubeadm-playbook
kubeadm-playboook ansible project's code is on Github
modular, clean code, supporting multiple activies by using ansible tags (e.g. add/reset a subgroup of nodes). optionally help configuring container engine (e.g. docker) This project targets to get a fully working environment in matter of minutes on any hw: baremetal, vms (vsphere, virtualbox), etc. k8s version upgrades: while many of its roles can be used for an upgrade, upgrade should be done using kubeadm tool.
🌐
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:
🌐
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.
🌐
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
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-create-a-kubernetes-cluster-using-kubeadm-on-ubuntu-18-04
How To Create a Kubernetes Cluster Using Kubeadm on Ubuntu 18.04 | DigitalOcean
April 25, 2019 - Kubeadm automates the installation and configuration of Kubernetes components such as the API server, Controller Manager, and Kube DNS. It does not, however, create users or handle the installation of operating-system-level dependencies and their configuration. For these preliminary tasks, it is possible to use a configuration management tool like Ansible or SaltStack.