<p>I want to Cordon, Drain and then reboot each worker node in a kubernetes cluster using ansible</p>
<p>To do this I need to run the Cordon and Drain commands using kubectl on the localhost but I need to connect to each remote host to reboot it and I want to run all three on one node at a time.</p>
<p>Am I able to override the host at the task level but get a reference to the hostname to use in the task?, I am thinking something along the following lines.</p>
<pre><code>- name: Reboot worker nodes
  become: yes
  hosts: workers
  gather_facts: no
  serial: 1
  tasks:
    - name: cordon worker
      hosts: localhost
      shell: kubectl cordon {{inventory_hostname}} ......
      async: 300
      poll: 10
    - name: drain worker
      hosts: localhost
      shell: kubectl drain {{inventory_hostname}} ....
      async: 300
      poll: 10
    - name: reboot
      reboot:
        reboot_timeout: 500

</code></pre>

PrestonDocks
<p>You should use <a href="https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_delegation.html#delegating-tasks" rel="nofollow noreferrer">delegate_to</a> to perform tasks locally, in context of current host in play</p>
<pre><code>- name: Reboot worker nodes
  become: yes
  hosts: workers
  gather_facts: no
  serial: 1
  tasks:
    - name: cordon worker
      shell: kubectl cordon {{inventory_hostname}} ......
      async: 300
      poll: 10
      delegate_to: localhost
    - name: drain worker
      shell: kubectl drain {{inventory_hostname}} ....
      async: 300
      poll: 10
      delegate_to: localhost
    - name: reboot
      reboot:
        reboot_timeout: 500
</code></pre>
