---
- import_playbook: aws-credentials.yml hostlist=http:bree
- hosts: http:bree
  become: true
  become_user: root
  vars:
    env_path: "{{ inventory_dir }}/.env.production"
  tasks:
    # check env file
    - name: check if env exists
      local_action: stat path={{ env_path }}
      become: false
      register: env_file
    - name: fail when env file does not exist
      fail:
        msg: .env.production does not exist
      when: not env_file.stat.exists

    # remote dir
    - name: check if remote dir exists
      stat:
        path: '/var/www/production'
      register: remote_dir

    - name: fail when remote dir does not exist
      fail:
        msg: pm2 dir not yet created
      when: not remote_dir.stat.exists or not remote_dir.stat.isdir

    # copy env file to server
    - name: copy env file to server
      copy:
        src: '{{ env_path }}'
        dest: /var/www/production/current/.env
        owner: www-data
        group: www-data
        # https://chmodcommand.com/chmod-660/
        mode: 0660
