---
# file: roles/serverdensity/tasks/main.yml

- name: 'ServerDensity | Init SD plugin'
  local_action:
    serverdensity
    api_token={{sd_api_token}}
    cleanup={{sd_alert_cleanup}}
    force={{ sd_force_update }}
    cache={{sd_api_cache_file}}

- name: 'ServerDensity | Install Public Repo Key'
  apt_key:
    url="{{ sd_repo_public_key_uri }}"
    state=present

- name: 'ServerDensity | Add ServerDensity Repository To Apt'
  copy:
    content='deb http://www.serverdensity.com/downloads/linux/deb all main'
    dest='/etc/apt/sources.list.d/sd-agent.list'
    mode=644

- name: 'ServerDensity | Install The Agent'
  apt:
    pkg=sd-agent
    state=installed
    update_cache=yes
  notify: 'ServerDensity | Restart Agent'

- name: 'ServerDensity | Create Plugins Directory'
  file:
    dest="{{ sd_agent_plugins_dir }}"
    state=directory
    mode=755

- name: 'ServerDensity | Copy Plugins'
  copy:
    src="{{ sd_plugins_files_dir }}/{{item.1}}"
    dest="{{ sd_agent_plugins_dir }}/{{item.1}}"
  with_items: sd_plugins|dictsort
  when: sd_plugins|length > 0
  notify: 'ServerDensity | Restart Agent'

- name: 'ServerDensity | Configure The Agent'
  template: src="{{ sd_template_config }}"
            dest=/etc/sd-agent/config.cfg
            owner=root
            group=root
            mode=0644
  when: sd_agent_key != ''
  notify: 'ServerDensity | Restart Agent'