I have an application running on port 8080. I want to check if the port is open and the application is successfully running on the port. How can i do this using ansible wait_for module?
Check Application Status Using waif_for module
There are two things,
- If a port is opened, it doesn’t mean your application has started.
- You need to check the response code for a specific URL to make sure that your application is running.
- Download the page content and check for a specific string in your application to make sure you are getting the right application page.
Let’s look at each scenario.
Check for Port
You can check for a specific port using the following code.
- name: Wait for apache app to kick in wait_for: port: 80 delay: 10 timeout: 900
The above code will check for port 80 connectivity for 900 seconds. If the port doesn’t come up within 900 seconds, then ansible will throw a timeout error. You can change the parameter based on your needs.
Check based on HTTP response code
You can make sure the application is up using the following code which loops until a particular response code is retrieved.
- name: "wait for website to come up" uri: url: "http://localhost:8080" status_code: 200 register: result until: result.status == 200 retries: 90 delay: 10
The status code might change depend on the application. Some applications might respond with 401. Test the response code with a REST client before you implement the ansible code.
Check based on page content
You can use the following content to check the application status based on a string in the web page.
- name: Check if web page contains `user` string uri: url=http://localhost:8080 return_content=true register: response failed_when: "'user' not in response.content"
You can replace “user” with a string that is available in your application web page.