带有响应时间的 uri 模块 [英] uri module with response times

查看:25
本文介绍了带有响应时间的 uri 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Ansible 中的站点获得响应时间,类似于 像这样,但在 Ansible 中.我正在使用 URI 模块,但它似乎不支持响应时间.

I'd like to have the response times from sites in Ansible, something along the lines like this, but in Ansible. I'm using the URI module but it seems it does not support response times.

我不喜欢在时间配置文件中使用回调插件,因为我在一个任务中声明了多个 url.

I do not like to use the callback plugin with the time profile, because I state multiple url's in a single task.

我看到 Ansible 没有返回我需要的值,这是有人已经做过的吗?

I see Ansible does not return the values I require, is this something someone has already done?

推荐答案

所以,我创建了一整套剧本出于这个请求.剧本本身包括:

So, I've created a whole playbook out of this request. The playbook itself includes:

  • 检查 URL 是否提供状态代码 200
  • 包括从主机到服务器的反应时间
  • 失败时发送 Slack 消息
  • 将日志发送到 Elasticsearch 服务器

可以设置一个 cronjob,让剧本每 x 秒运行一次.

One could set a cronjob to let the playbook run each x seconds.

要回答这个特定问题:

 - hosts: localhost
   vars:
     sites:
       - https://google.com

    - name: verify if all urls works properly
      uri:
        url: "{{ item }}"
        timeout: 10
      with_items: "{{ sites }}"
      register: health_check
      ignore_errors: yes

    # From the docs: The time in sec it took from the start until
    # the first byte was just about to be transferred. This includes
    # time_pretransfer and also the time the server needed to calculate the result.
    - name: response times of sites via cURL via the `starttransfer` method
      shell: 'curl {{ item }} -s -o /dev/null -w  "%{time_starttransfer}\n"'
      register: curl_time
      ignore_errors: True
      args:
        warn: false
      with_items:
        - "{{ sites }}"

    - name: write url and status code to file
      lineinfile:
        line: "{{ start_time }}, {{ item.url }}, {{ item.status }}"
        insertafter: EOF
        dest: "{{ file_path }}"
      with_items: "{{ health_check.results }}"

    - name: append response times to the file
      lineinfile:
        path: "{{ file_path }}"
        backrefs: yes
        regexp: "^(.*{{ start_time }}, .*{{ item.item | replace ('https://', '') | replace ('http://', '')   }}.*)"
        line: '\1, {{ item.stdout }}'
      with_items: "{{ curl_time.results }}"

这篇关于带有响应时间的 uri 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆