---
- name: Config system other config,like disabled firewalld & selinux,install package,update kernel etc.
  hosts: myNewHosts
  become: true

# This is my defined vars,include hostname and other
  vars_files:
    - 00_myvars_config.yml

  tasks:
  - name: Create directory
    file:  
      path: "{{ item }}"
      state: directory
    with_items: "{{ mypydir }}"

  - name: Wget New python version
    get_url:
      url: "https://mirrors.huaweicloud.com/python/{{ pylongversion }}/Python-{{ pylongversion }}.tar.xz"
      dest: /tmp/python  
      validate_certs: false

#  - name: Generate script from template
#    template:
#      src: 00_installpy_script.j2
#      dest: 00_installpy_script.sh

  - name: Update python version 3
    raw: |
      cd {{ mypydir[0] }}
      tar -Jxvf Python-{{ pylongversion }}.tar.xz
      cd Python-{{ pylongversion }}/
      ./configure --prefix=/usr/local/python{{ pyversion }} --with-ssl
      make && make install
      ln -sfn /usr/local/python{{ pyversion }}/bin/pip{{ pyshortversion }} /etc/alternatives/pip-{{ pyversion }}
      ln -sfn /usr/local/python{{ pyversion }}/bin/pip{{ pyshortversion }} /etc/alternatives/pip{{ pyversion }}
      ln -sfn /usr/local/python{{ pyversion }}/bin/pip{{ pyshortversion }} /usr/bin/pip{{ pyshortversion }}
      ln -sfn /usr/local/python{{ pyversion }}/bin/pip{{ pyshortversion }} /usr/bin/pip-{{ pyshortversion }}
      ln -sfn /usr/local/python{{ pyversion }}/bin/pydoc{{ pyshortversion }} /etc/alternatives/pydoc-{{ pyversion }}
      ln -sfn /usr/local/python{{ pyversion }}/bin/pydoc{{ pyshortversion }} /etc/alternatives/pydoc{{ pyversion }}
      ln -sfn /usr/local/python{{ pyversion }}/bin/pydoc{{ pyshortversion }} /usr/bin/pydoc{{ pyshortversion }}
      ln -sfn /usr/local/python{{ pyversion }}/bin/python{{ pyshortversion }} /etc/alternatives/python{{ pyversion }}
      ln -sfn /usr/local/python{{ pyversion }}/bin/python{{ pyshortversion }} /usr/bin/python{{ pyshortversion }}
      ln -sfn /usr/local/python{{ pyversion }}/bin/python{{ pyshortversion }}-config /etc/alternatives/python-{{ pyversion }}-config
      ln -sfn /usr/local/python{{ pyversion }}/bin/python{{ pyshortversion }}-config /etc/alternatives/python{{ pyversion }}-config
      ln -sfn /usr/local/python{{ pyversion }}/bin/python{{ pyshortversion }}-config /usr/bin/python{{ pyshortversion }}-config
      alternatives --install /usr/bin/python{{ pyversion }} python{{ pyversion }} /usr/bin/python{{ pyshortversion }} 2000000 --slave /usr/bin/pip{{ pyversion }} pip{{ pyversion }} /usr/bin/pip{{ pyshortversion }} --slave /usr/bin/pydoc{{ pyversion }} pydoc{{ pyversion }} /usr/bin/pydoc{{ pyshortversion }}  --slave /usr/bin/pydoc-{{ pyversion }} pydoc-{{ pyversion }} /usr/bin/pydoc{{ pyshortversion }} --slave /usr/bin/pip-{{ pyversion }} pip-{{ pyversion }} /usr/bin/pip-{{ pyshortversion }} --slave /usr/bin/python{{ pyversion }}-config python{{ pyversion }}-config /usr/bin/python{{ pyshortversion }}-config --slave /usr/bin/pyvenv-{{ pyversion }} pyvenv-{{ pyversion }} /usr/bin/pyvenv-{{ pyversion }}.6 --slave /usr/bin/easy_install-{{ pyversion }} easy_install-{{ pyversion }} /usr/bin/easy_install-{{ pyversion }}.6
      alternatives --auto python3
    ignore_errors: true

  - name: Update pip repo
    raw: |
      cat > ~/.pip/pip.conf <<EOF
      [global]
      index-url = https://pypi.mirrors.ustc.edu.cn/simple/ 
      [install]
      trusted-host = pypi.mirrors.ustc.edu.cn
      EOF

      pip3 install --upgrade pip
      pip3 install pyreadline
      pip3 completion --bash >> ~/.bashrc
    ignore_errors: true
