---
- hosts: "{{IM_HOST}}"
  become: yes
  become_method: sudo
  gather_facts: false
  tasks:
    # Some OSs does not have python by default
    - name: Check Python is installed
      raw: which python
      ignore_errors: yes
      register: python_exists
      changed_when: false

    # Ubuntu 16 have an annoying apt-daily.service boot process
#    - name: Delete apt processes
#      raw: ls /etc/debian_version && pgrep apt && (sudo killall apt ; sleep 1 ; sudo rm -f /var/lib/apt/lists/lock && sudo rm -f /var/lib/dpkg/lock && sudo dpkg --configure -a ; sudo apt-get -f -y install)
#      ignore_errors: yes

    - name: Bootstrap with python
      raw: sudo apt update; sudo apt install -y python; sudo yum install -y python; sudo zypper -n install python python-xml
      ignore_errors: yes
      register: python_install
      changed_when: python_install.stdout_lines|length > 1
      when: python_exists is failed

    - name: Get Facts
      setup:

    - name: Install libselinux-python in RH
      action: yum pkg=libselinux-python state=installed
      when: ansible_os_family == "RedHat"

    - name: EPEL
      yum: name=epel-release,yum-priorities
      when: ansible_os_family == "RedHat" and ansible_distribution != "Fedora"

####################### Install Ansible in Ubuntu and RHEL systems with apt and yum ###################################
################### because they have recent versions of ansible in system repositories ###############################
################# Use INDIGO repos from Ubuntu 14 and CentOS 7 to assure a stable version  ############################

    - name: Ubuntu apt update
      apt: update_cache=yes cache_valid_time=3600
      when: ansible_distribution == "Ubuntu"
      ignore_errors: yes
      register: apt_update

    - name: Ubuntu force apt update (avoid apt lock)
      command: apt update
      when: ansible_distribution == "Ubuntu" and apt_update is failed

    - name: Ubuntu install Ansible requisites with apt
      apt: name=python-pip,python-jinja2,sshpass,openssh-client,unzip
      when: ansible_distribution == "Ubuntu"

    - apt_key: url=https://repo.indigo-datacloud.eu/repository/RPM-GPG-KEY-indigodc state=present
      when: ansible_distribution == "Ubuntu" and ansible_distribution_major_version|int >= 14

    - name: Ubuntu 14 install indigo 1 repo
      apt: deb=https://repo.indigo-datacloud.eu/repository/indigo/1/ubuntu/dists/trusty-updates/main/binary-amd64/indigodc-release_1.0.0-2_amd64.deb
      when: ansible_distribution == "Ubuntu" and ansible_distribution_major_version|int >= 14
      ignore_errors: yes

    - name: Install requirements Ubuntu 14
      apt: name=gcc,python-dev,libffi-dev,libssl-dev
      when: ansible_distribution == "Ubuntu" and ansible_distribution_major_version|int <= 14

    - name: Ubuntu install requirements
      apt: name=software-properties-common
      when: ansible_os_family == "Debian" and (ansible_distribution != "Ubuntu" or (ansible_distribution_major_version != "14" and ansible_distribution_major_version != "16"))
        
    - name: Ubuntu install Ansible PPA repo  
      apt_repository: repo='ppa:ansible/ansible'
      when: ansible_os_family == "Debian" and (ansible_distribution != "Ubuntu" or (ansible_distribution_major_version != "14" and ansible_distribution_major_version != "16"))
        
    - name: Apt-get update
      apt: update_cache=yes
      when: ansible_os_family == "Debian"

    - name: Ubuntu install Ansible with apt
      apt: name=ansible force=yes
      when: ansible_distribution == "Ubuntu"

    - rpm_key: state=present key=https://repo.indigo-datacloud.eu/repository/RPM-GPG-KEY-indigodc
      when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 7 and ansible_distribution != "Fedora"

    - name: RH indigo repos
      yum: state=present name=https://repo.indigo-datacloud.eu/repository/indigo/2/centos7/x86_64/base/indigodc-release-2.0.0-1.el7.centos.noarch.rpm
      when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 7 and ansible_distribution != "Fedora"
        
    - name: RH7 install Ansible with yum
      yum: name=ansible,python-pip,python-jinja2,sshpass,openssh-clients,unzip
      when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 7 and ansible_distribution != "Fedora"

############################################ In other systems use pip #################################################

    - name: Apt install requirements
      apt: name=unzip,gcc,python-dev,openssh-client,sshpass,python-pip,libffi-dev,libssl-dev
      when: ansible_os_family == "Debian" and ansible_distribution != "Ubuntu"
      
    - name: Yum install requirements RH or Fedora
      yum: name=python-distribute,gcc,python-devel,wget,openssh-clients,sshpass,python-pip,libffi-devel,openssl-devel
      when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 6

    - name: Zypper install requirements Suse   
      zypper: name=python,python-pip,gcc,python-devel,wget,libffi-devel,openssl-devel state=present
      when: ansible_os_family == "Suse"
        
#    - name: Install Pip (alternative)
#      shell: wget https://bootstrap.pypa.io/get-pip.py && python get-pip.py

    - name: Upgrade pip
      pip: name=pip extra_args="-I" state=latest

    - name: Upgrade setuptools with Pip
      pip: name=setuptools state=latest

    - name: Upgrade pyOpenSSL with Pip
      pip: name=pyOpenSSL state=latest
      ignore_errors: yes

    - name: Install ansible with Pip
      pip: name=ansible extra_args="-I"
      when: ansible_os_family == "Suse" or (ansible_os_family == "Debian" and ansible_distribution != "Ubuntu") or ansible_distribution == "Fedora" or (ansible_os_family == "RedHat" and ansible_distribution_major_version|int < 7)
        
#################################### Now install jmespath and scp and pywinrm with pip  ########################################
    - name: Install jmespath with Pip
      pip: name=jmespath
        
    - name: Install scp with Pip
      pip: name=scp
      when: ansible_os_family != "RedHat" or (ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 6)

    - name: Install pywinrm with Pip
      pip: name=pywinrm
      when: ansible_os_family != "RedHat" or (ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 6)
      ignore_errors: yes

    - name: Disable SELinux
      selinux: state=disabled
      when: ansible_os_family == "RedHat"
      ignore_errors: yes
        
################################################ Configure Ansible  ###################################################

    - name: Create /etc/ansible
      file: path=/etc/ansible state=directory

    - name: Create /etc/ansible/ansible.cfg
      file: path=/etc/ansible/ansible.cfg state=touch owner=root group=root mode=0644

    - name: Set host_key_checking to false in ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=defaults option=host_key_checking value=False

    - name: Set nocolor to 1 in ansible.cfg to avoid strange chars in Ansible outputs
      ini_file: dest=/etc/ansible/ansible.cfg section=defaults option=nocolor value=1

    - name: Set transport to ssh in ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=defaults option=transport value=ssh
      when: ansible_os_family == "Debian" or (ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 6)  or (ansible_os_family == "Suse" and ansible_distribution_major_version|int >= 10)

    - name: Set transport to smart in ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=defaults option=transport value=smart
      when: (ansible_os_family == "RedHat" and ansible_distribution_major_version|int < 6) or (ansible_os_family == "Suse" and ansible_distribution_major_version|int < 10)

    - name: Change ssh_args to set ControlPersist to 15 min in ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=ssh_connection option=ssh_args value="-o ControlMaster=auto -o ControlPersist=900s -o UserKnownHostsFile=/dev/null"
      when: ansible_os_family == "Debian" or (ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 7) or (ansible_os_family == "Suse" and ansible_distribution_major_version|int >= 12)
        
    - name: Change ssh_args to remove ControlPersist in REL 6 and older in ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=ssh_connection option=ssh_args value="-o UserKnownHostsFile=/dev/null"
      when: (ansible_os_family == "RedHat" and ansible_distribution_major_version|int < 7) or (ansible_os_family == "Suse" and ansible_distribution_major_version|int < 12)
        
    - name: Activate SSH pipelining in ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=ssh_connection option=pipelining value=True

    - name: Set jinja2.ext.do to jinja2_extensions in ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=defaults option=jinja2_extensions value=jinja2.ext.do

    - name: Set allow_world_readable_tmpfiles to True ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=defaults option=allow_world_readable_tmpfiles value=True

    - name: Set fact_caching in ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=defaults option=fact_caching value=jsonfile

    - name: Set fact_caching_connection in ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=defaults option=fact_caching_connection value=/var/tmp/facts_cache

    - name: Set fact_caching_timeout in ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=defaults option=fact_caching_timeout value=86400
        
    - name: Set fact_caching_timeout in ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=defaults option=gathering value=smart

    - name: Set roles_path in ansible.cfg
      ini_file: dest=/etc/ansible/ansible.cfg section=defaults option=roles_path value=/etc/ansible/roles
