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?
There are two things,
Let’s look at each scenario.
You can check for a specific port using the following code.
1 2 3 4 5 |
- 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.
You can make sure the application is up using the following code which loops until a particular response code is retrieved.
1 2 3 4 5 6 7 8 |
- 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.
You can use the following content to check the application status based on a string in the web page.
1 2 3 4 |
- 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.
The Best Tutorials & Tips to Speedup Your DevOps Workflow.
Created by Bibin Wilson.