I'm trying to restart the server and then wait, using this:
- name: Restart server shell: reboot - name: Wait for server to restart wait_for: port=22 delay=1 timeout=300
But I get this error:
TASK: [iptables | Wait for server to restart] ********************************* fatal: [example.com] => failed to transfer file to /root/.ansible/tmp/ansible-tmp-1401138291.69-222045017562709/wait_for: sftp> put /tmp/tmpApPR8k /root/.ansible/tmp/ansible-tmp-1401138291.69-222045017562709/wait_for Connected to example.com. Connection closed
reboot_timeout. integer. Maximum seconds to wait for machine to reboot and respond to a test command. This timeout is evaluated separately for both reboot verification and test command success so the maximum execution time for the module is twice this amount.
Take this code: - hosts: localhost become: yes tasks: - name: Stop and Start ssh service: name: ssh state: stopped and check the status using "service ssh status" . It will show sshd is running not stopped. Similarly on stage restarted it will not stop-start the service.
In an Ansible playbook, when local_action is used, Ansible will run the module work mentioned under it on the controller node. Mostly modules used with local_action are shell and command. As you can do almost all the tasks using these modules on the controller node.
Use the built-in reboot module:
- name: Wait for server to restart reboot: reboot_timeout: 3600
- name: restart server shell: 'sleep 1 && shutdown -r now "Reboot triggered by Ansible" && sleep 1' async: 1 poll: 0 become: true
This runs the shell command as an asynchronous task, so Ansible will not wait for end of the command. Usually async
param gives maximum time for the task but as poll
is set to 0, Ansible will never poll if the command has finished - it will make this command a "fire and forget". Sleeps before and after shutdown
are to prevent breaking the SSH connection during restart while Ansible is still connected to your remote host.
You could just use:
- name: Wait for server to restart local_action: module: wait_for host={{ inventory_hostname }} port=22 delay=10 become: false
..but you may prefer to use {{ ansible_ssh_host }}
variable as the hostname and/or {{ ansible_ssh_port }}
as the SSH host and port if you use entries like:
hostname ansible_ssh_host=some.other.name.com ansible_ssh_port=2222
..in your inventory (Ansible hosts
file).
This will run the wait_for task on the machine running Ansible. This task will wait for port 22 to become open on your remote host, starting after 10 seconds delay.
But I suggest to use both of these as handlers, not tasks.
There are 2 main reason to do this:
code reuse - you can use a handler for many tasks. Example: trigger server restart after changing the timezone and after changing the kernel,
trigger only once - if you use a handler for a few tasks, and more than 1 of them will make some change => trigger the handler, then the thing that handler does will happen only once. Example: if you have a httpd restart handler attached to httpd config change and SSL certificate update, then in case both config and SSL certificate changes httpd will be restarted only once.
Read more about handlers here.
Restarting and waiting for the restart as handlers:
handlers: - name: Restart server command: 'sleep 1 && shutdown -r now "Reboot triggered by Ansible" && sleep 1' async: 1 poll: 0 ignore_errors: true become: true - name: Wait for server to restart local_action: module: wait_for host={{ inventory_hostname }} port=22 delay=10 become: false
..and use it in your task in a sequence, like this, here paired with rebooting the server handler:
tasks: - name: Set hostname hostname: name=somename notify: - Restart server - Wait for server to restart
Note that handlers are run in the order they are defined, not the order they are listed in notify
!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With