<p>I am setting up a Kubernetes setup using Ansible. To set up master I have written a playbook as shown below.</p>
<pre><code>- hosts: master
  become: yes
  tasks:
    - name: initialize the cluster
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16 

    - name: create .kube directory
      become: yes
      become_user: ubuntu
      file:
        path: $HOME/.kube
        state: directory
        mode: 0755

    - name: copy admin.conf to user's kube config
      copy:
        src: /etc/kubernetes/admin.conf
        dest: /home/ubuntu/.kube/config
        remote_src: yes
        owner: ubuntu

    - name: install Pod network
      become: yes
      become_user: ubuntu
      shell: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml &gt;&gt; pod_network_setup.txt
      args:
        chdir: $HOME
        creates: pod_network_setup.txt
</code></pre>
<p>The problem is that when i run the playbook, it doesn't wait for the initialization to complete i.e. for command &quot;kubeadm init &quot; to return and runs each task one after another. Since, the initialization takes time and one it is completed then only the file &quot;/etc/kubernetes/admin.conf&quot; is created. As Ansible doesn't wait for it to complete it exists giving error in task 3 that &quot;/etc/kubernetes/admin.conf&quot; is not found.</p>
<p>If I run following playbook then Ansible waits till the initialization is complete i.e. the control hangs till &quot;kubeadm init &quot; returns.</p>
<pre><code>- hosts: master
  become: yes
  tasks:
    - name: initialize the cluster
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16 
</code></pre>
<p>How can I make Ansible wait for command &quot;kubeadm init &quot; to complete and then only start another command ?</p>

Prakhar
<p>There are three ways:</p>
<ol>
<li><a href="https://docs.ansible.com/ansible/latest/modules/wait_for_module.html" rel="nofollow noreferrer">wait_for</a> some time after executing <code>kubeadm init</code> ansible task.</li>
</ol>
<pre><code>- hosts: master
  become: yes
  tasks:
    - name: initialize the cluster
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16 
    - name: sleep for 20 seconds
      wait_for:
        timeout: 20
</code></pre>
<ol start="2">
<li>Make use of <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#retrying-a-task-until-a-condition-is-met" rel="nofollow noreferrer">until</a> in ansible.</li>
</ol>
<pre><code>- hosts: master
  become: yes
  tasks:
    - name: initialize the cluster
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16
      register: result
      until: result.stdout.find(&quot;Your Kubernetes master has initialized successfully!&quot;) != -1
      retries: 1
      delay: 20
</code></pre>
<p><em><strong>NOTE:</strong> Here we retry <code>kubeadm init</code> until we get the string <code>Your Kubernetes master has initialized successfully!</code> in the output.</em></p>
<ol start="3">
<li>Check if the file <code>/etc/kubernetes/admin.conf</code> exists after executing <code>kubeadm init</code> ansible task.</li>
</ol>
<pre><code>- hosts: master
  become: yes
  tasks:
    - name: initialize the cluster
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16
    - name: create .kube directory
      become: yes
      become_user: ubuntu
      file:
        path: $HOME/.kube
        state: directory
        mode: 0755
    - name: Check admin.conf file exists.
      stat:
        path: /etc/kubernetes/admin.conf
      register: k8s_conf
    - name: copy admin.conf to user's kube config
      copy:
        src: /etc/kubernetes/admin.conf
        dest: /home/ubuntu/.kube/config
        remote_src: yes
        owner: ubuntu
      when: k8s_conf.stat.exists
</code></pre>
<p><em><strong>NOTE:</strong> Here we execute admin.conf copy only when the k8s config file <code>/etc/kubernetes/admin.conf</code> exists.</em></p>
