---
# 
# installation des binaires ORACLE
#

  - name: Création des répertoires
    file: dest={{ item.dir }} mode={{ item.mode }} owner={{ item.owner }} group={{ item.group }} state=directory
    with_items:
      - { dir: "{{ oracle_racine }}", owner: "{{ gi_user }}", group: "{{ oracle_group }}", mode: 775 }
      - { dir: "{{ oracle_base }}", owner: "{{ db_user }}", group: "{{ oracle_group }}", mode: 755 }
      - { dir: "{{ oracle_racine }}/{{ gi_user }}", owner: "{{ gi_user }}", group: "{{ oracle_group }}", mode: 755 }
      - { dir: "{{ gi_home }}", owner: "{{ gi_user }}", group: "{{ oracle_group }}", mode: 775 }
      - { dir: "{{ oracle_stage }}", owner: "{{ gi_user }}", group: "{{ oracle_group }}", mode: 775 }
    tags: createdir

  - name: vérification de l espace disque disponible sur oracle_racine
    action: shell df -P {{ oracle_racine }} | awk 'END { print $4 }'
    register: u01size
    failed_when: u01size.stdout|int < {{ u01_free_space_gb }} * 1024 * 1024
    tags: diskfreespace

  - name: vérification de l espace disque disponible sur /tmp
    action: shell df -P /tmp | awk 'END { print $4 }'
    register: tmpsize
    failed_when: tmpsize.stdout|int < {{ tmp_free_space_gb }} * 1024 * 1024
    tags: diskfreespace

  - name: Vérifier si une installation existe ...
    shell: grep "{{ gi_home  }}" "{{ oracle_inventory_loc }}/ContentsXML/inventory.xml" | wc -l
    register: checkdbswinstall
    failed_when: checkdbswinstall.stdout != "0"
    tags: checkifexists

  - name: Extraction des binaires Oracle
    unarchive: src={{ item[0].filename }}  dest={{ oracle_stage }}
    with_nested: 
      - "{{ gi_software }}"
      - "{{ gi_version }}"
    when: item[0].version == gi_version  
    sudo: yes
    sudo_user: "{{ gi_user }}"
    tags: transfert

  - name: Linux 7 - application du pacth 19404309 pour installer 11.2 sur Linux 7
    # https://updates.oracle.com/Orion/Services/download?type=readme&aru=17984752
    # cela consiste à copier le fichier cvu_prereq.xml dans le répertoire stage
    copy: src=cvu_prereq.xml dest={{ oracle_stage }}/grid/stage/cvu owner={{ gi_user }} group={{ oracle_group }} mode=644
    sudo: yes
    sudo_user: "{{ gi_user }}"
    tags: patch_p19404309
    when: ansible_distribution_major_version == '7' and db_version == "11.2.0.4"

  - name: Copie du fichier de réponse pour installation silencieuse
    template: src=gi_install_{{ gi_version }}.j2 dest={{ oracle_stage }}/{{ gi_response_file }}
    sudo: yes
    sudo_user: "{{ gi_user }}"
    tags: responsefile

  - name: Création du script d installation silencieuse
    template: src=run_gi_install.sh.j2 dest={{ oracle_stage }}/run_gi_install.sh mode=755
    sudo: yes
    sudo_user: "{{ gi_user }}"
    tags: responsefile

  - name: Installation des binaires Oracle
    shell: "{{ oracle_stage }}/run_gi_install.sh"
    sudo: yes
    sudo_user: "{{ gi_user }}"
    register: oradbinstall
    tags: orainstall

  - debug: var=oradbinstall.stdout_lines
    tags: orainstall

  - name: Vérification de l existance du fichier orainstRoot.sh
    stat: path="{{ oracle_inventory_loc }}/orainstRoot.sh"
    register: orainstRoot

  - name: Exécution du script orainstRoot.sh
    shell: "{{ oracle_inventory_loc }}/orainstRoot.sh"
    sudo: yes
    sudo_user: root
    when: orainstRoot.stat.exists
    tags: runroot

  - name: Exécution du script root.sh
    shell: "{{ gi_home }}/root.sh"
    sudo: yes
    sudo_user: root
    register: rootsh
    tags: runroot

  - debug: var=rootsh.stdout_lines
    tags: runroot

  - name: Résultat de l installation via OPatch
    shell: "{{ gi_home }}/OPatch/opatch lsinventory"
    sudo: yes
    sudo_user: "{{ gi_user }}"
    register: opatchls
    tags: opatchgi

  - debug: var=opatchls.stdout_lines
    tags: opatchgi

  - name: Configuration GI standalone
    shell: "{{ gi_home }}/perl/bin/perl {{ gi_home }}/crs/install/roothas.pl"
    sudo: yes
    sudo_user: root
    register: roothas
    tags: roothas

  - debug: var=roothas.stdout_lines
    tags: roothas

  - name: Ajout de export ORACLE_HOME dans le bash_profile de grid
    lineinfile: dest=/home/grid/.bash_profile line="export ORACLE_HOME={{ gi_home }}" state=present create=yes 
    tags: gi_bash_profile

  - name: Ajout de export ORACLE_SID dans le bash_profile de grid
    lineinfile: dest=/home/grid/.bash_profile line="export ORACLE_SID={{ gi_user }}" state=present create=yes 
    tags: gi_bash_profile

  - name: Ajout de export PATH dans le bash_profile de grid
    lineinfile: dest=/home/grid/.bash_profile line="export PATH=$ORACLE_HOME/bin:$PATH" state=present create=yes 
    tags: gi_bash_profile

  # suppression des binaires, du fichier de réponse et du script d'install
  - name: suppression du repertoire des fichiers decompressés
    file: path={{ oracle_stage }}/grid state=absent

  - name: suppression du fichier de réponse
    file: path={{ oracle_stage }}/{{ gi_response_file }} state=absent

  - name: suppression du script d'installation
    file: path={{ oracle_stage }}/run_gi_install.sh state=absent
