我想从Ansible中获得站点的响应时间,类似like this,但在Ansible中。我正在使用URI module,但似乎不支持响应时间。
我不喜欢在时间配置文件中使用回调插件,因为我在单个任务中声明了多个url。
我看到Ansible没有返回我需要的值,这是某人已经完成的事情吗?
答案 0 :(得分:0)
因此,我创建了a whole playbook out of this request。该剧本本身包括:
一个人可以设置一个cronjob来让剧本每隔x秒运行一次。
要回答这个特定问题:
- 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 }}"