Ansible Study Notes - Bulk inspection site URL status

This article was last updated on: February 7, 2024 pm

preface

Do not drag mud and water, do not pull things around.

Quick fixes, learn a skill that can be used for the job in five minutes.

Through concrete practical cases, we vividly demonstrate the use of Ansible.

demand

I need to regularly inspect or regularly monitor the availability status of the homepage of all my company’s sites.

Ansible Playbook practical script

check_url_status.yml As follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- hosts: localhost
gather_facts: no
serial: 10
tasks:
- name: check url status
uri:
url: "{{ item }}"
timeout: 10
return_content: no
follow_redirects: safe
validate_certs: yes
with_items: "{{ url_list }}"
vars:
url_list:
- https://www.baidu.com
- https://www.taobao.com
- https://www.qq.com

Pass ansible-playbook ./check_url_status.yml Yes, the return result is as follows:

Detailed description

  1. Used here ansible-playbook to execute, in the form of a choreographed “script”.
  2. hosts: localhost To perform this task on this local machine, you can also specify other machines;
  3. serial: 10: Explicitly define the goals of how Ansible executes the current playbook in batches
  4. uri: URI plugin used, reference link:https://docs.ansible.com/ansible/2.9/modules/uri_module.html
  5. url: Specific URL address of the site to be inspected
  6. item and with_items: Loop batch execution is implemented through these 2 parameters
  7. vars: Defines the variables used in the playbook.

Returns json

If you need to return JSON for further processing, you can use JSON CallBack plugin. As follows:

1
2
3
4
5
#!/bin/bash

export ANSIBLE_CALLBACK_WHITELIST=json
export ANSIBLE_STDOUT_CALLBACK=json
ansible-playbook ./check_url_status.yml > ./url_status_$(date +"%Y-%m-%d").json

Detailed description

  1. ANSIBLE_CALLBACK_WHITELIST=json: Specify Ansible Callback Whitelist as json; Official Callback documentation:https://docs.ansible.com/ansible/2.9/plugins/callback.html
  2. ANSIBLE_STDOUT_CALLBACK: Specifies the standard output take json Callback plugin, the output is in JSON format.
  3. json Callback plugin official documentation:https://docs.ansible.com/ansible/2.9/plugins/callback/json.html
  4. There’s another one syslog_json Plugins:https://docs.ansible.com/ansible/2.9/plugins/callback/syslog_json.html

EOF


Ansible Study Notes - Bulk inspection site URL status
https://e-whisper.com/posts/60396/
Author
east4ming
Posted on
January 6, 2022
Licensed under