###
# This Playbook uploads the logs generate from validations to a configured storage 
# account.
# Input: {{ item }} - name of the host machine. 
# Assumption: All the related logs of a machine are present inside the folder
#             ./validation_results/<hostname>
###
---
- set_fact:
    hostname: "{{ item }}"
    current_time: "{{ ansible_date_time.iso8601 }}"
    current_epoch: "{{ ansible_date_time.epoch }}"
    resource_group: "{{ lookup('env', 'RESOURCE_GROUP') }}"
    storage_account_name: "{{ lookup('env', 'ACCOUNT_NAME') }}"
    container_name: "{{ lookup('env', 'CONTAINER_NAME') }}"
    sa_resource_group: "{{ lookup('env', 'STORAGE_ACCOUNT_RESOURCE_GROUP') }}"

- name: Recursively find all the log files
  find:
    paths: "./validation_results/{{ hostname }}"
    recurse: yes
  register: logfiles

- name: Upload yum repolist errors file to storage account
  azure_rm_storageblob:
    resource_group: "{{ sa_resource_group }}"
    storage_account_name: "{{ storage_account_name }}"
    container: "{{ container_name }}"
    blob: "{{ hostname }}{{ file_name.path.split('tmp')[1] }}"
    src: "{{ file_name.path }}"
    public_access: container
    tags:
      validation_time: "{{ current_time }}"
    force: yes 
  loop: "{{ logfiles.files }}"
  loop_control:
    loop_var: file_name

- name: Create table entry
  shell: |
    ./set_validation_results.sh {{hostname}} {{ current_epoch }} {{current_time}}
    az storage blob upload-batch --account-name {{storage_account_name}} -s ./report -d '$web'