#0 Common install by OS 
- debug:
    msg: "Will install components on ansible_os_family:{{ansible_os_family}},ansible_distribution:{{ ansible_distribution }}, ansible_distribution_major_version: {{ ansible_distribution_major_version }}"
  
- name: Check OS support, if not support, exit ansible
  fail: msg="OS not supported,exit!"
  when: ansible_distribution not in common_os_support
  
- include: "{{ansible_os_family}}.yml"
  
- name: Updatedb for mlocate
  shell: updatedb

#2 Pip install
- block:
  - name: pip upgrade(python2 support latest pip version is 20.3.4)
    shell: python{{ansible_python.version.major}} -m pip install -U "pip < 21.0"
    failed_when: False
    
  - name: Install PyMySQL on pip2
    pip:
      name: PyMySQL
      version: 0.10.1
      extra_args: "-U"
      executable: pip2
    when: ansible_os_family == "RedHat" and (ansible_distribution_major_version == '7' or ansible_distribution_major_version == '2')
    
  - name: Install pex requirements module
    pip:
      name: pexpect
      extra_args: "-U"
    register: pex_install_result
    failed_when: False

  - name: Install pymysql requirements module
    pip:
      name: PyMySQL
      extra_args: "-U"
    register: mysql_install_result
    failed_when: False
  when: common_install_python_modules

- debug: 
    msg: "{% if pex_install_result.msg is defined %} {{pex_install_result.msg}} {% endif %}"

- debug: 
    msg: "{% if mysql_install_result.msg is defined %} {{mysql_install_result.msg}} {% endif %}"

#3 Configure
- name: Create common folder
  file:
    path: '/data/{{item}}'
    state: directory
    recurse: yes
    mode: '0755'
  with_items:
    - logs
    - config
    - cert
    - apps
  failed_when: False
      
- block:
  - name: set a regular hostname, remove "."
    shell: get_hostname=$(hostname);echo ${get_hostname%%.*}
    register: common_get_hostname
  - hostname:
      name: "{{common_get_hostname.stdout}}"

#4 Tool install
- name: Install tools
  include_tasks: tools_{{item}}.yml
  with_items: "{{common_install_tools}}"
  when: common_install_tools is defined and common_install_tools is not none and common_install_tools != "" and common_install_tools[0]!= ""



# Softlink
- name: Create a symbolic link
  file:
    src: '{{item.src}}'
    dest: '{{item.dest}}'
    state: link
    force: yes
  with_items:
  - {src: /data/apps/, dest: /data/wwwroot }
  - {src: /var/lib/docker/,dest: /data/docker}

- name: Create credentials Folder
  file:
    path: /credentials
    state: directory
