Como você mencionou que está executando comandos via ansible, aqui está o que eu uso para reinicializações em um playbook (estou gerenciando máquinas do Ubuntu 14 / 16.04):
---
# execute like:
# ansible-playbook reboot.yaml --inventory hosts --extra-vars "hosts=all user=admin"
# or
# ansible-playbook reboot.yaml -i hosts -e "hosts=all user=admin"
- hosts: "{{ hosts }}"
remote_user: "{{ user }}"
become: yes
tasks:
# add this to to guard you from yourself ;)
#- name: "ask for verification"
# pause:
# prompt: "Are you sure you want to restart all specified hosts?"
# here comes the juicy part
- name: "reboot hosts"
shell: "sleep 2 && shutdown -r now 'Reboot triggered by Ansible'" # sleep 2 is needed, else this task might fail
async: "1" # run asynchronously
poll: "0" # don't ask for the status of the command, just fire and forget
ignore_errors: yes # this command will get cut off by the reboot, so ignore errors
- name: "wait for hosts to come up again"
wait_for:
host: "{{ inventory_hostname }}"
port: "22" # wait for ssh as this is what is needed for ansible
state: "started"
delay: "120" # start checking after this amount of time
timeout: "360" # give up after this amount of time
delegate_to: "localhost" # check from the machine executing the playbook
...
Atualizar
O Ansible 2.7 agora tem um módulo de reinicialização , para que você não precise criar comandos por si só. O manual de cima se traduziria em:
---
# execute like:
# ansible-playbook reboot.yaml --inventory hosts --extra-vars "hosts=all user=admin"
# or
# ansible-playbook reboot.yaml -i hosts -e "hosts=all user=admin"
- hosts: "{{ hosts }}"
remote_user: "{{ user }}"
become: yes
tasks:
# add this to to guard you from yourself ;)
#- name: "ask for verification"
# pause:
# prompt: "Are you sure you want to restart all specified hosts?"
- name: "reboot hosts"
reboot:
msg: "Reboot triggered by Ansible"
reboot_timeout: 360
...