How to Wait for Server Restart Using Ansible

How to wait for server restart using Ansible?

You should change the wait_for task to run as local_action, and specify the host you're waiting for. For example:

- name: Wait for server to restart
local_action:
module: wait_for
host=192.168.50.4
port=22
delay=1
timeout=300

How can i get Ansible to wait until a service is active?

This is very similar to @zigarn answer above, In my case it was not enough to know that the service was started or running, i needed to know it was active, hence im posting what i went with.
Thank you to all for your help

- name: Ensure myservice  is in a running state
service:
name: myservice
state: started
register: myserviceDetails
until: myserviceDetails.status.ActiveState == "active"
retries: 15
delay: 20

How to wait for tomcat restart before exit the Play in Ansible?

This should work :

Call handlers from your task. Handlers will be for restarting tomcat, wait, checking the status and finally to exit.

- name: Copying Application Configuration files to tomcat nodes
## Task to do, will notify handlers
notify: "Restart tomcat"

Handlers file to have below :

# handlers file for tomcat_service_start_check
- name: Restart service
listen: "Restart tomcat"
shell: <script/command to start tomcat>

- name: Wait for tomcat start for N seconds and continue with play
listen: "Restart tomcat"
local_action:
module: wait_for
host={{ inventory_hostname }}
port=8080
delay=10

- name: Get tomcat status by hitting URL
listen: "Restart tomcat"
uri:
url: http://localhost:8080
register: tomcat_state

- name: End the play if tomcat returns HTTP 200
listen: "Restart tomcat"
meta: end_play
when:
- tomcat_state == '200'

Now, all handlers listen to "Restart tomcat". And once triggered, they will be executed in the order they are defined.

Reference :
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#handlers-running-operations-on-change

Notify handlers are always run in the same order they are defined, not
in the order listed in the notify-statement. This is also the case for
handlers using listen.

Ansible reboot remote host wait for 60 sec and reboot next remote host

As @Peschke said, try the reboot module. But do do it one at a time, you need to set serial: 1 in the play:

- hosts: all
serial: 1
become: yes

tasks:
- name: Rebooting ...
reboot:
reboot_timeout: 60

Make ansible wait for server to start, without logging in

as you correctly stated, this task executes on the "to be provisioned" host, so ansible tries to connect to it (via ssh) first, then would try to wait for the port to be up. this would work for other ports/services, but not for 22 on a given host, since 22 is a "prerequisite" for executing any task on that host.

what you could do is try to delegate_to this task to the ansible host (that you run the PB) and add the host parameter in the wait_for task.

Example:

- name: wait until server is up
wait_for:
port: 22
host: <the IP of the host you are trying to provision>
delegate_to: localhost

hope it helps

How to restart Jenkins using Ansible and wait for it to come back?

I solved it using curl + Ansible's until, which I just learned about. It will request the page until the http status code is 200 OK.

- name: Wait untils Jenkins web API is available
shell: curl --head --silent http://localhost:8080/cli/
register: result
until: result.stdout.find("200 OK") != -1
retries: 12
delay: 5

Ansible wait_for rebooting

It was a connectivity problem. To get rid of this problem I've increased the ssh timeout.

One simple way to do that it's creating a ansible.cfg file in the same directory of the ansible script and put inside this content:

[defaults]
timeout = xx

where xx for me was 30.



Related Topics



Leave a reply



Submit