---

- name: Ensure {{ key }} is set to {{ '***' if ['pass', 'secret', 'key']|select('in', key) else value }}
  block:
    - name: Check value of {{ key }}
      community.docker.docker_container_exec:
        container: "{{ nextcloud_container_name }}"
        command: "{{ nextcloud_occ_command }} config:{{ type }}:get {{ scope }} {{ entry }}"
        user: "{{ nextcloud_user_info.uid }}"
        tty: yes
      register: nextcloud_current_config_entry
      check_mode: false
      changed_when: false

    - name: Set {{ key }} to {{ '***' if (['pass', 'secret', 'key']|select('in', key)) else value }}
      community.docker.docker_container_exec:
        container: "{{ nextcloud_container_name }}"
        command: "{{ nextcloud_occ_command }} config:{{ type }}:set {{ scope }} {{ entry }} --type={{ value_type }} --value={{ value }} -n"
        user: "{{ nextcloud_user_info.uid }}"
        tty: yes
      when: nextcloud_current_config_entry.stdout != value
      notify: restart-nextcloud
  vars:
    entry_path: "{{ key.split('.') }}"
    value_type: >-
      {% if value is boolean %}
      boolean
      {% elsif value is integer %}
      integer
      {% elsif value is float %}
      float
      {% else %}
      string
      {% endif %}
    type: "{{ entry_path | first }}"
    scope: "{{ entry_path[1] if entry_path | length > 2 else '' }}"
    entry: >-
      {{
         entry_path[1] if entry_path|length == 2 else 
         (entry_path[2:] | join(" "))
      }}