Working ansible command/task for restarting nodes

I didn’t find any working ansible commands to do something on a node, then reboot it, wait for it to come back and do other stuff, so I experimented around and wrote a working
one.
You’ll probably run into trouble when your SSH port is not 22. I guess you can write something with jinja2 syntax to get the SSH port from ansible.
It probably will not work at all, if your SSH port is defined in an ssh_config file (Read the man page for that name, if you don’t know what it is, if you’re interested) and not the ansible hosts list.

I tested this on Ansible 2.2.0.0 and it works here.

So there you go:

 -  name: do something
    
 -  name: restart node
    shell: sleep 2 && shutdown -r now "Ansible update triggered"
    async: 1
    poll: 0
    ignore_errors: true
    become: true

 -  name: wait for node to come back
    local_action: 
        module: wait_for 
            host={{ inventory_hostname }}
            port=22
            state=started
            delay=30
            timeout=300
            connect_timeout=15
    become: false

 -  name: do something

 

Kind regards,
Thermi

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.