# How to capture varnishlogs in ansible

**URL:** <https://forum.ansible.com/t/how-to-capture-varnishlogs-in-ansible/3350>\
**Category:** Get Help\
**Created:** [January 17, 2024, 2:05pm UTC](https://forum.ansible.com/t/how-to-capture-varnishlogs-in-ansible/3350 "2024-01-17T14:05:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhkjokjjkj](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@nhkjokjjkj](https://forum.ansible.com/u/nhkjokjjkj)\
**Post date:** [January 17, 2024, 2:05pm UTC](https://forum.ansible.com/t/how-to-capture-varnishlogs-in-ansible/3350/1 "2024-01-17T14:05:24Z")

</div>

how to capture varnishlogs run time after curl req is sent

A

1. i tired this way

- name: varnishlog in T1  
hosts: host1  
become: yes  
tasks:

- name: curl req in T2  
hosts: host2  
become: no  
tasks:

- name: Check if varnishlog is complete  
async\_status:  
jid: “{{ varnishlog\_output.ansible\_job\_id }}”  
register: job\_result  
until: job\_result.finished  
delay: 10  
retries: 3

still unable to get varnishlog run time

---

<div class="post-metadata">

**Author:** ![atul\_mohan](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@atul\_mohan](https://forum.ansible.com/u/atul_mohan)\
**Post date:** [January 5, 2025, 10:25pm UTC](https://forum.ansible.com/t/how-to-capture-varnishlogs-in-ansible/3350/2 "2025-01-05T22:25:47Z")

</div>

```auto
---
- name: Capture Varnish logs after curl request
  hosts: host1 # Assuming host1 is where Varnish is running
  become: yes # Assuming you need root or sudo to run varnishlog
  tasks:
    - name: Start varnishlog in background
      shell: >
        varnishlog > /tmp/varnishlog_{{ ansible_date_time.epoch }}.log &
        echo $! > /tmp/varnishlog_pid_{{ ansible_date_time.epoch }}.pid
      args:
        executable: /bin/bash
      register: varnishlog_start

    - name: Wait for varnishlog to start
      wait_for:
        path: /tmp/varnishlog_{{ ansible_date_time.epoch }}.log
      delegate_to: localhost

    - name: Send curl request
      shell: >
        curl -vso /dev/null "http://54.144.19.76/index.html"
      delegate_to: host2 # Assuming host2 is where you send the request from
      become: no

    - name: Wait for a moment to ensure logs are captured
      pause:
        seconds: 5 # Adjust based on how long it takes for logs to appear

    - name: Stop varnishlog
      shell: >
        kill $(cat /tmp/varnishlog_pid_{{ ansible_date_time.epoch }}.pid) &&
        rm -f /tmp/varnishlog_pid_{{ ansible_date_time.epoch }}.pid

    - name: Fetch varnishlog output
      fetch:
        src: "/tmp/varnishlog_{{ ansible_date_time.epoch }}.log"
        dest: "./varnish_logs/"
        flat: yes
      register: fetch_output

    - name: Clean up temporary log file
      file:
        path: "/tmp/varnishlog_{{ ansible_date_time.epoch }}.log"
        state: absent

    - name: Display fetched log
      debug:
        msg: "{{ lookup('file', './varnish_logs/' + fetch_output.md5) }}"

```
