#!/usr/bin/ansible-playbook
# -*- yaml -*-

# Copyright (C) 2023 Progetto Fuss <info@fuss.bz.it>
# Author:           Simone Piccardi <piccardi@truelite.it>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program or from the site that you downloaded it
# from; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307   USA

##
## NOTE: this role use configuration variables (and certificate) from 
## the fuss-server, where the files must be put on the server
## in the /var/www/fuss-data-conf/ directory, the configuration variables
## must be in the ocsinventory.yml file with content like:
##
##   ocs_server: https://ocs.server.addr/ocsinventory
##   ocs_tag: change-me
##   ocs_cert: server.crt
##
## and server.crt has to be in the same directory, see docs in the
## fuss-tech-guide at:  
## https://fuss-tech-guide.readthedocs.io/it/latest/miniguide_software/index.html
##

##
## Usage:
## /usr/share/fuss-server/scripts/ocs-agent-inst -i client,
##

---
- hosts: all
  vars:
    ocs_source: /var/www/fuss-data-conf
    ocs_var_file: "{{ocs_source}}/ocsinventory.yml"
  tasks:

  - name: Check ocsinventory variables file presence
    delegate_to: localhost
    stat:
      path: "{{ocs_var_file}}"
    register: ocs_file

  - debug: msg="configuration variables not found, not configuring ocsinventory"
    when: not ocs_file.stat.exists

  - block:
    - name: get variables from ocsinventory variables file
      include_vars:
        file: "{{ocs_var_file}}"

    - name: Install ocsinventory agent
      apt:
        name: ocsinventory-agent
        state: present

    - name: Get the ocsinventory server certificate from fuss server
      copy:
        src: "{{ocs_source}}/{{ocs_cert}}"
        dest: /etc/ocsinventory/
      when: ocs_cert is defined
    
    - name: Install agent configuration
      copy:
        content: |
          logfile=/var/log/ocsinventory-client/ocsinventory-agent.log
          snmpretry=2
          snmp=1
          ssl=0
          nosoftware=0
          {% if ocs_cert is defined %}ca=/etc/ocsinventory/{{ocs_cert}}
          {% endif %}
          basevardir=/var/lib/ocsinventory-agent
          snmptimeout=3
          tag={{ocs_tag}}
          server={{ocs_server}}
        dest: /etc/ocsinventory/ocsinventory-agent.cfg
        
    # TODO: make this a systemd timer
    - name: Install esecution in cron.d
      cron:
        name: "Run ocsinventory agent"
        cron_file: ocsinventory-agent
        job: ocsinventory-agent --lazy
        user: root
        hour: "{{ 24 | random(seed=inventory_hostname) }}"
        minute: "{{ 60 | random(seed=inventory_hostname) }}"

    when: ocs_file.stat.exists
