---
- name: Generate Agent Configuration File
  become: yes
  become_user: jingrow
  command: '/home/jingrow/agent/env/bin/agent setup config --name {{ server }} --workers {{ workers }} {% if proxy_ip is defined %}--proxy-ip {{ proxy_ip }}{% endif %}'
  args:
    chdir: /home/jingrow/agent

- name: Setup Agent Authentication
  become: yes
  become_user: jingrow
  command: '/home/jingrow/agent/env/bin/agent setup authentication --password {{ agent_password }}'
  args:
    chdir: /home/jingrow/agent

- name: Setup Agent Supervisor
  become: yes
  become_user: jingrow
  command: /home/jingrow/agent/env/bin/agent setup supervisor
  args:
    chdir: /home/jingrow/agent

- name: Setup Agent TLS (Private Key)
  become: yes
  become_user: jingrow
  copy:
    content: '{{ certificate_private_key }}'
    dest: /home/jingrow/agent/tls/privkey.pem

- name: Setup Agent TLS (Full Chain)
  become: yes
  become_user: jingrow
  copy:
    content: '{{ certificate_full_chain }}'
    dest: /home/jingrow/agent/tls/fullchain.pem

- name: Setup Agent TLS (Intermediate Chain)
  become: yes
  become_user: jingrow
  copy:
    content: '{{ certificate_intermediate_chain }}'
    dest: /home/jingrow/agent/tls/chain.pem

- name: Setup Agent NGINX
  become: yes
  become_user: jingrow
  command: /home/jingrow/agent/env/bin/agent setup nginx
  args:
    chdir: /home/jingrow/agent

- name: Setup Monitoring Authentication
  become: yes
  become_user: jingrow
  command: 'htpasswd -Bbc /home/jingrow/agent/nginx/monitoring.htpasswd jingrow {{ monitoring_password }}'
