---
- name: Odoo Install Modules
  hosts: optovia_servers
  become: yes
  gather_facts: no
  
  # Usage: ansible-playbook -i ansible/inventory/production/hosts.yml ansible/playbooks/odoo-install-modules.yml -e "modules=base"
  
  pre_tasks:
    - name: Validate modules parameter
      assert:
        that:
          - modules is defined
          - modules != ""
        fail_msg: "Parameter 'modules' required! Use: -e 'modules=base'"
  
  tasks:
    - name: Get Odoo container ID
      shell: docker ps --filter "name={{ odoo_container }}" --format "{%raw%}{{.ID}}{%endraw%}"
      register: container_id
      
    - name: Install Odoo modules
      shell: >
        docker exec {{ container_id.stdout }}
        infisical run -- sh -c
        'odoo
        --db_host="$ODOO_DB_HOST"
        --db_port="$ODOO_DB_PORT"
        --db_user="$ODOO_DB_USER"
        --db_password="$ODOO_DB_PASSWORD"
        --database="$ODOO_DB_NAME"
        --http-port=8070
        --without-demo=all
        -i {{ modules }} --stop-after-init'
      
    - name: Restart Odoo service
      shell: docker service update --force {{ odoo_container }}
