---
# A simple one-off playbook that gathers basic usage information from OpenedX hosts.
# This is used by the `activity_csv` management command.

- hosts: all
  vars:
    # This should be overridden to be more unique.
    local_output_dir: /tmp/
    remote_output_filename: /tmp/activity_report
  tasks:
    # We have to do a copy+command pair here (instead of script) in order to get
    #   separate channels for stdout and stderr.
    # See also: https://github.com/ansible/ansible-modules-core/pull/4000/files
    - name: Copy stats script
      copy:
        src: stats.py
        dest: /tmp/collect_activity.py
      become_user: edxapp

    - name: Run stats script
      command: >
        /edx/bin/python.edxapp /tmp/collect_activity.py {{ inventory_hostname }} {{ remote_output_filename }}
      register: stats_results
      become_user: edxapp

    # For debugging purposes:
    #   stdout contains the computed stats
    #   stderr contains any logs (including edX logs)
    - debug:
        var: stats_results

    - name: Fetch stats
      fetch:
        src: '{{ remote_output_filename }}'
        dest: '{{ local_output_dir }}/{{ inventory_hostname }}'
        flat: yes
      register: fetch_results

    - debug:
        var: fetch_results

---
# A simple one-off playbook that gathers basic usage information from OpenedX hosts.
# This is used by the `activity_csv` management command.

- hosts: all
  vars:
    # This should be overridden to be more unique.
    local_output_dir: /tmp/
    remote_output_filename: /tmp/activity_report
  tasks:
    # We have to do a copy+command pair here (instead of script) in order to get
    #   separate channels for stdout and stderr.
    # See also: https://github.com/ansible/ansible-modules-core/pull/4000/files
    - name: Copy stats script
      copy:
        src: stats.py
        dest: /tmp/collect_activity.py
      become: yes
      become_user: www-data

    - name: Run stats script
      command: >
        /edx/bin/python.edxapp /tmp/collect_activity.py {{ inventory_hostname }} {{ remote_output_filename }}
      register: stats_results
      become: yes
      become_user: www-data

    # For debugging purposes:
    #   stdout contains the computed stats
    #   stderr contains any logs (including edX logs)
    - debug:
        var: stats_results

    - name: Fetch stats
      fetch:
        src: '{{ remote_output_filename }}'
        dest: '{{ local_output_dir }}/{{ inventory_hostname }}'
        flat: yes
      register: fetch_results

    - debug:
        var: fetch_results

Switch back to www-data and include become: yes
Switch back to www-data and include become: yes
