How do i export the ansible output of a playbook TASK to a excel sheet or table?

I am writing a script for IOS upgrade in switches and script involves multiple TASKS like checking copying image, settings boot statement, etc. The final task would be to check if IOS upgrade was successful or not and display it on screen.

Mgmt wants only the output message for the final TASK (whether IOS upgrade was succesful or not) to be saved in excel sheet or table. They don’t have the patience to go through the entire output shown by ansible for the playbook.

How do i automatically save the output shown by ansible for only the above mentioned final TASK to a excel sheet/table?

Thanks,

Vikram

write a Jinja2 template and render the variable values and get a html table output.

There are multiple ways to do this. Like Pushparaj suggested you can use Jinja2 template and render variables.

Other ways to do this can be -

  1. Use JSON callback plugin and convert it CSV file like - https://stackoverflow.com/questions/54855755/need-to-generate-csv-file-report-out-of-ansible-playbook-output-executed-in-chec
  2. Write your own callback plugin to create a CSV file. You can see https://docs.ansible.com/ansible/latest/dev_guide/developing_plugins.html#callback-plugins for callback development. You may want to explore and modify https://github.com/ansible-collections/ansible.posix/blob/main/plugins/callback/cgroup_perf_recap.py to match your needs.

Hi,

I later came to know about lieinfile module and wrote it using that. Thanks for your suggestion though.

  • name: GATHER CURRENT VERSION IN DEVICE FACTS
    ios_facts:

  • lineinfile:
    path: ./output/output10.txt
    state: present
    line: - “Image upgrade is successful. Current version is {{ ansible_net_version }} for {{ inventory_hostname }}”
    when: ansible_net_version == upgrade_ios_version

  • lineinfile: path: ./output/output10.txt
    state: present
    line: - “Image upgrade not successful. Current version is {{ ansible_net_version }} for {{ inventory_hostname }}”
    when: ansible_net_version != upgrade_ios_version

Regards,
Vikram

Hi Pushparaj,

I am aware of writing some basic scripts using Jinja2 template but yet to master it. I later came to know about lieinfile module and wrote it using that

Thanks,
Vikram