---

- block:
    - name: check '{{ python_command }}' command
      command: "'{{ python_command }}' --version"
      changed_when: false

    - name: discover '{{ python_command }}' executable path
      command: "which '{{ python_command }}'"
      register: discover_python_executable
      changed_when: false

    - name: register '{{ python_command }}' executable as '{{ discover_python_executable.stdout }}'
      set_fact:
        python_executable: '{{ discover_python_executable.stdout }}'

  rescue:
    - name: install '{{ python_name }}' build requeirements
      become: yes
      become_user: root
      yum:
        state: present
        name: '{{ yum_install_packages }}'
      when: "(yum_install_packages | length) > 0"

    - name: download '{{ python_name }}' from '{{ python_url }}'
      get_url:
        url: "{{ python_url }}"
        dest: "{{ python_tar }}"

    - name: ensure '{{ python_src_dir | dirname }}' directory exists
      file:
        path: '{{ python_src_dir }}'
        state: directory

    - name: extract '{{ python_tar | basename }}' into '{{ python_src_dir }}'
      unarchive:
        src: '{{ python_tar }}'
        dest: '{{ python_src_dir | dirname }}'
        remote_src: yes

    - name: configure '{{ python_name }}'
      command:
        chdir: '{{ python_src_dir }}'
        cmd: |
          ./configure '--prefix={{ python_prefix }}' {{ python_configure_flags }}

    - name: compile '{{ python_name }}'
      command:
        cmd: "make -j '{{ make_jobs }}'"
        chdir: '{{ python_src_dir }}'

    - name: install '{{ python_name }}'
      become: yes
      become_user: root
      command:
        cmd: "make '{{ make_install_goal }}'"
        chdir: '{{ python_src_dir }}'

    - name: check '{{ python_executable }}' is installed
      shell: |
        '{{ python_executable }}' --version 2>&1 | \
            grep 'Python {{ python_version}}'
      changed_when: false

    - name: add '{{ python_prefix }}/lib' to '{{ ldconfig_file }}'
      become: yes
      become_user: root
      lineinfile:
        path: '{{ ldconfig_file }}'
        line: '{{ python_prefix }}/lib'
        create: yes
        owner: root
        group: root
        mode: '0644'
      register: add_ldconfig_file

    - name: run ldconfig after changing '{{ ldconfig_file }}'
      become: yes
      become_user: root
      command: ldconfig
      when: add_ldconfig_file.changed

    - name: add '{{ python_executable | dirname }}' to '{{ bash_profile_file }}'
      become: yes
      become_user: root
      lineinfile:
        path: '{{ bash_profile_file }}'
        line: 'PATH={{ python_executable | dirname }}:$PATH'
        create: yes
        owner: root
        group: root
        mode: '0644'
      when: (python_executable | dirname) not in ansible_env.PATH.split(':')
      register: add_python_dir

    - name: reset ssh connection after changing '{{ python_profile_file }}'
      meta: reset_connection

    - name: check '{{ python_command }}' command
      command: "'{{ python_command }}' --version"
      changed_when: false

    - name: discover '{{ python_command }}' executable path
      command: "which '{{ python_command }}'"
      register: discover_python_executable
      changed_when: false
