I have an Ansible playbook to perform the following task:
Access a remote host via ssh and create a text file that lists all the backups on the remote host filesystem that can be inserted into an email body. I am looking for a more efficient way to write this or to find a best practice to implement this.
Any and all feedback is greatly appreciated!
Below is the current iteration in use to locate backups on the remote host:
- name: Create backup log from Remote Host
  shell: |
    cd /mnt/bntestmnt; echo -e "Backup server: [ipv4 address goes here] \nBackup directory: /export
    /Backups/ESG \n\nConfluence Backups:" > BackupStatus.txt
    ls -td Confluence/daily/* | xargs du -sh >> BackupStatus.txt; echo " " >> BackupStatus.txt; 
    ls -td Confluence/weekly/* | xargs du -sh >> BackupStatus.txt; echo -e "\nGitlab Backups:" >> 
    BackupStatus.txt
    ls -td GitLab/daily/* | xargs du -sh >> BackupStatus.txt; echo " " >> BackupStatus.txt
    ls -td GitLab/weekly/* | xargs du -sh >> BackupStatus.txt; echo -e "\nJira Backups:" >> 
    BackupStatus.txt
    ls -td Jira/daily/* | xargs du -sh >> BackupStatus.txt; echo " " >> BackupStatus.txt
    ls -td Jira/weekly/* | xargs du -sh >> BackupStatus.txt; cat BackupStatus.txt
  register: Backup_Status
  delegate_to: [remote host name goes here]
Because '>>' redirects the command output to a text file, Ansible cannot see the result, so 'cat' is used to display the final product so it can be logged as a variable, to be inserted into the email body.
This is the expected output:
    Backup server: [ipv4 address goes here] 
    Backup directory: /export/Backups/ESG 
    
    Confluence Backups:
    4.1G    Confluence/daily/Thu-23Mar23
    4.0G    Confluence/daily/Wed-22Mar23
    4.0G    Confluence/daily/Tue-21Mar23
    4.0G    Confluence/daily/Mon-20Mar23
    4.0G    Confluence/daily/Sun-19Mar23
    3.9G    Confluence/daily/Sat-18Mar23
    3.9G    Confluence/daily/Fri-17Mar23
     
    3.7G    Confluence/weekly/Fri-10Mar23
    3.5G    Confluence/weekly/Fri-03Mar23
    3.4G    Confluence/weekly/Fri-24Feb23
    3.3G    Confluence/weekly/Sat-18Feb23
    
    Gitlab Backups:
    2.5G    GitLab/daily/Thu-23Mar23
    2.5G    GitLab/daily/Wed-22Mar23
    2.4G    GitLab/daily/Tue-21Mar23
    2.4G    GitLab/daily/Mon-20Mar23
    2.4G    GitLab/daily/Sun-19Mar23
    2.3G    GitLab/daily/Sat-18Mar23
    2.3G    GitLab/daily/Fri-17Mar23
     
    2.2G    GitLab/weekly/Fri-10Mar23
    2.1G    GitLab/weekly/Fri-03Mar23
    2.2G    GitLab/weekly/Fri-24Feb23
    
    Jira Backups:
    2.6G    Jira/daily/Thu-23Mar23
    2.6G    Jira/daily/Wed-22Mar23
    2.6G    Jira/daily/Tue-21Mar23
    2.6G    Jira/daily/Mon-20Mar23
    2.6G    Jira/daily/Sun-19Mar23
    2.6G    Jira/daily/Sat-18Mar23
    2.6G    Jira/daily/Fri-17Mar23
     
    2.6G    Jira/weekly/Fri-10Mar23
    2.6G    Jira/weekly/Fri-03Mar23
    2.6G    Jira/weekly/Fri-24Feb23
    2.6G    Jira/weekly/Fri-17Feb23
The list within each application has a newline between the last instance of 'daily' and the first instance of 'weekly'
Some alternate formats I have tried to use are below. I ran into issues in trying to format the output to achieve the desired result.
`ls -td Confluence/{daily,weekly}/* | xargs du -sh`
or
`find Confluence -mindepth 2 -type d -exec du -sh {} \;`
(This version seems to sort the list incorrectly, as the size/dates are out of order)
I've also looked into modifying the task to utilize Jinja2 to create a template, but feel that I may be out of my depth with that method. See below for the attempted format:
- name: Create backup log from Remote Host
  template:
    src: backup_report.j2
    dest: /mnt/bntestmnt/BackupStatus.txt
  vars:
    confluence_backups: [variable goes here]
    gitlab_backups: [variable goes here]
    jira_backups: [variable goes here]
I'm still trying to figure out how I would write the contents of "backup_report.j2", but I have the following inside:
Backup server: [ipv4 address]
Backup directory: /export/Backups/ESG
Confluence Backups: 
{% for backup in confluence_backups %}
{{ backup }}
{% endfor %}
Gitlab Backups: 
{% for backup in gitlab_backups %}
{{ backup }}
{% endfor %}
Jira Backups: 
{% for backup in jira_backups %}
{{ backup }}
{% endfor %}
 
     
    