Skip to content

Commit 41a21c3

Browse files
author
Colin Hoglund
committed
initial commit
0 parents  commit 41a21c3

File tree

7 files changed

+180
-0
lines changed

7 files changed

+180
-0
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
In addition to installing Python, this role also installs `pip` and `virtualenv` globally.
2+
3+
Install default OS package:
4+
```
5+
---
6+
7+
- hosts: all
8+
become: true
9+
roles:
10+
- python
11+
```
12+
13+
Install custom package:
14+
```
15+
---
16+
17+
- hosts: all
18+
become: true
19+
vars:
20+
python_apt_repo: deb http://example.repo/ trusty main
21+
python_apt_key_url: https://example.repo/Release.key
22+
python_package_name: python-custom
23+
roles:
24+
- python
25+
```
26+
27+
Build Python 2 and 3 from source with custom prefix and global packages:
28+
```
29+
---
30+
31+
- hosts: all
32+
become: true
33+
vars:
34+
python_build_from_source: true
35+
python_configure_prefix: /opt/local
36+
python_global_packages:
37+
- ansible==2.0.1.0
38+
- uwsgi
39+
roles:
40+
- { role: python, python_version: 2.7.7 }
41+
- { role: python, python_version: 3.4.4 }
42+
```

defaults/main.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
3+
# Package defaults
4+
python_apt_key_url: ''
5+
python_apt_repo: ''
6+
python_global_packages: []
7+
python_package_name: python
8+
9+
# Build defaults
10+
python_build_from_source: false
11+
python_configure_prefix: '/usr/local'
12+
python_version: ''
13+
14+
# Internals
15+
python_major_version: "{{python_version[0]}}"
16+
python_path: "{{python_configure_prefix}}/bin/python{{python_major_version}}"
17+
python_pip_path: "/usr/local/bin/pip{{python_major_version}}"
18+
python_tar_dir: "Python-{{python_version}}"
19+
python_tar_url: "https://www.python.org/ftp/python/{{python_version}}/{{python_tar_dir}}.tgz"
20+
python_dependencies:
21+
- build-essential
22+
- libbz2-dev
23+
- libdb-dev
24+
- libgdbm-dev
25+
- liblzma-dev
26+
- libncurses-dev
27+
- libreadline-dev
28+
- libsqlite3-dev
29+
- libssl-dev
30+
- python-dev
31+
- tk-dev
32+
- zlib1g-dev

meta/main.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
dependencies: []
3+
4+
galaxy_info:
5+
author: Colin Hoglund
6+
description: Python Ansible role that supports package installs as well as building from source.
7+
license: "license (BSD, MIT)"
8+
min_ansible_version: 1.9
9+
platforms:
10+
- name: Debian
11+
versions:
12+
- all
13+
- name: Ubuntu
14+
versions:
15+
- all
16+
categories:
17+
- development

tasks/build_source.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
3+
- name: Install dependencies
4+
apt: name="{{item}}" state=latest update_cache=yes cache_valid_time=86400
5+
with_items: "{{python_dependencies}}"
6+
7+
- name: Download python tarball
8+
get_url: url="{{python_tar_url}}" dest=/tmp/
9+
10+
- name: Unpack python tarball
11+
unarchive: src="/tmp/{{python_tar_dir}}.tgz" dest=/tmp/ copy=no
12+
13+
- name: Ensure python prefix directory exists
14+
file: path="{{python_configure_prefix}}" state=directory
15+
16+
- name: Build python
17+
shell: "./configure --prefix={{python_configure_prefix}}; make; make install"
18+
args:
19+
chdir: "/tmp/{{python_tar_dir}}"

tasks/install_package.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
3+
- name: Add apt key
4+
apt_key: url={{python_apt_key_url}}
5+
when: python_apt_repo != ''
6+
7+
- name: Setup apt repository
8+
apt_repository: repo="{{python_apt_repo}}"
9+
when: python_apt_repo != ''
10+
11+
- name: Install package
12+
apt: name={{python_package_name}} update_cache=yes cache_valid_time=86400
13+
14+
- name: Get python version
15+
shell: "apt-cache show {{python_package_name}} | grep 'Version:' | awk '{print $2}'"
16+
register: python_version_output
17+
18+
- name: Set python_major_version
19+
set_fact:
20+
python_major_version: "{{python_version_output.stdout_lines[0][0]}}"
21+
22+
- name: Get python location
23+
command: "which python{{python_major_version}}"
24+
register: python_location_output
25+
26+
- name: Set python_path
27+
set_fact:
28+
python_path: "{{python_location_output.stdout_lines[0]}}"

tasks/install_pip.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
3+
- name: Download get-pip.py
4+
get_url: url=https://bootstrap.pypa.io/get-pip.py dest=/tmp/
5+
6+
- name: Install pip
7+
command: "{{python_path}} /tmp/get-pip.py"

tasks/main.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
3+
# Install package
4+
- include: install_package.yml
5+
when: not python_build_from_source
6+
7+
# Build from source
8+
- name: Check if python is already installed
9+
stat: path="{{python_path}}"
10+
when: python_build_from_source
11+
register: python_installed
12+
13+
- include: build_source.yml
14+
when: python_build_from_source and python_installed.stat.exists == false
15+
16+
# Install/upgrade pip
17+
- name: Check if pip is already installed
18+
stat: path="{{python_pip_path}}"
19+
register: pip_installed
20+
21+
- name: Install pip
22+
include: install_pip.yml
23+
when: pip_installed.stat.exists == false
24+
25+
- name: Upgrade pip
26+
pip: name=pip state=latest executable="{{python_pip_path}}"
27+
28+
# Install packages
29+
- name: Install/upgrade virtualenv
30+
pip: name=virtualenv state=latest executable="{{python_pip_path}}"
31+
when: python_major_version == '2'
32+
33+
- name: Install global packages
34+
pip: name="{{item}}" state=present executable="{{python_pip_path}}"
35+
with_items: "{{python_global_packages}}"

0 commit comments

Comments
 (0)