Accessing top-level playbook vars from a custom callback plugin

Hello everyone,

I’m creating a custom stdout callback plugin that allow me to run infrastructure tests and create a report accordingly. Here’s how it looks:

+-----------+---------------------------+------------------+--------------------------------------+----------+
| Host      | Task                      | Expected Value   | Obtained Value                       | Status   |
+===========+===========================+==================+======================================+==========+
| localhost | Test disk space           | 100GB            | 100GB                                | OK       |
+-----------+---------------------------+------------------+--------------------------------------+----------+
| localhost | Test network connectivity |                  | PING google.com (74.125.206.102) ... | FAILED   |
+-----------+---------------------------+------------------+--------------------------------------+----------+

+-----------+---------------+----------------+----------------+-------------------+
| Host      |   Total Tests |   Tests Passed |   Tests Failed | Pass Percentage   |
+===========+===============+================+================+===================+
| localhost |             2 |              1 |              1 | 50.00%            |
+-----------+---------------+----------------+----------------+-------------------+

However I am unable to access top-level playbook variables from the stdout callback, and thus I am only able to pass literals as expected_output to each task. Here’s the playbook that generated the above output so you can have a more clear understanding on what’s going on:

---
- name: Linux test
  hosts: localhost
  gather_facts: True
  vars:
    disk_space_threshold: "100GB"
    network_status: "True"
    generate_html_report: true

  tasks:
    - name: Set facts from playbook vars
      set_fact:
        expected_disk_space: "{{ disk_space_threshold }}"
        expected_connectivity: "{{ network_status }}"

    - name: Test disk space
      ansible.builtin.command: "echo 100GB"
      register: disk_output
      vars:
        # This works fine
        expected_output: "100GB"
        # This doesn't work either via top-level pb var or set_fact
        # expected_output: "{{ expected_disk_space }}"

    - name: Test network connectivity
      ansible.builtin.shell: "ping -c 1 google.com"
      register: network_output
      vars:
        expected_output: "{{ expected_connectivity }}"

Any thoughts or ideas?

Thanks,

Jorge Rua

It would really help to see how you are accessing variables in your custom callback.