We can use Ansible register to capture the output of tasks. This article explains the configuration to capture the output of tasks in Ansible.
Example playbook is given below.
- name: Get the files list of /var/log
- name: Print the file list
This playbook is for getting the list of files in the folder "/var/log" with .gz extension. The collected list is saving to the register with name file_list. We can use this variable for all future operations. Here the second task just printing the value saved in the variable file_list and it will print the task output that registered earlier.