added another ansible for test run

This commit is contained in:
2021-06-01 00:39:35 +04:00
parent 1324e0394f
commit 15b3344238
47 changed files with 1231 additions and 13 deletions

5
.gitignore vendored
View File

@@ -9,4 +9,7 @@ p2prc
config.json
*.tgz
.vscode/
hosts
hosts
*.retry
**/*.swp
**/*.retry

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 Dmitry Kozlov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,27 @@
ansible-nvidia
====================
ansible roles to install docker-ce, graphics drivers, cuda, cudnn and nvidia-docker
How-to run
----------------
- Install ansible
```
sudo pip3 install ansible
```
- Create hosts file in current directory
```
$ cat hosts
[gpus:vars]
ansible_python_interpreter=/usr/bin/python3
[gpus]
#192.168.1.2
```
- Use locally available ssh keys to authorise logins on a remote machine
```
ssh-copy-id root@192.168.1.2
```
- Run playbook
```
ansible-playbook gpus.yml
```

View File

@@ -0,0 +1,22 @@
[defaults]
host_key_checking=False
ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}
inventory = hosts
remote_user = ubuntu
sudo = yes
force_color = True
#private_key_file = ~/.ssh/my_aws_ec2_private_key.pem #chmod 400 ~/.ssh/my_aws_ec2_private_key.pem
hash_behaviour=merge
library = ./library
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp
stdout_callback = skippy
callback_whitelist = profile_tasks
forks = 100
timeout = 600
[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=30m -o ConnectionAttempts=100 -o UserKnownHostsFile=/dev/null
pipelining = True
retries = 3

View File

@@ -0,0 +1,9 @@
- hosts: test
become: true
become_user: root
roles:
# - { role: base }
# - { role: docker-ce, docker_version: "5:18.09.3~3-0~ubuntu-bionic", docker_compose_version: 1.23.2 }
- { role: cuda, cuda_version_major: 10, cuda_version_minor: 0 }
- { role: cudnn, cuda_version_major: 10, cuda_version_minor: 0, cudnn_version: 7.3 }
#- { role: nvidia-docker, nvidia_docker_version: 2.0.3+docker18.09.3-1 }

View File

@@ -0,0 +1,17 @@
---
locales_present:
- en_US.UTF-8
locales_language_packs_present:
- language-pack-en
- language-pack-en-base
locales_absent: []
locales_language_packs_absent: []
locales_default:
lang: en_US.UTF-8
lc_all: en_US.UTF-8
lc_ctype: en_US.UTF-8
language: en_US.UTF-8

View File

@@ -0,0 +1,3 @@
---
- name: update locales
command: dpkg-reconfigure --frontend noninteractive locales

View File

@@ -0,0 +1,4 @@
---
- shell: "sudo apt-get clean"
- shell: "sudo rm -r /var/lib/apt/lists/*"

View File

@@ -0,0 +1,12 @@
---
- name: Disable APT:Periodic::Update-Package-Lists
become: yes
shell: 'sudo find /etc/apt/apt.conf.d -type f -exec sed -i -e ''s/APT::Periodic::Update-Package-Lists "1"/APT::Periodic::Update-Package-Lists "0"/g'' {} \;'
- name: Disable APT::Periodic::Unattended-Upgrade
become: yes
shell: 'sudo find /etc/apt/apt.conf.d -type f -exec sed -i -e ''s/APT::Periodic::Unattended-Upgrade "1"/APT::Periodic::Unattended-Upgrade "0"/g'' {} \;'
- name: Disable APT::Periodic::Update-Package-Lists
become: yes
shell: 'sudo find /etc/apt/apt.conf.d -type f -exec sed -i -e ''s/APT::Periodic::Update-Package-Lists "1"/APT::Periodic::Update-Package-Lists "0"/g'' {} \;'

View File

@@ -0,0 +1,7 @@
---
# Setup task.
# eg: tasks/setup-Ubuntu.yml
- include_tasks: "disable-unattended-update-{{ ansible_distribution }}.yml"
- include_tasks: "clean-apt-cache.yml"
- include_tasks: "setup-locale-{{ ansible_distribution }}.yml"
- include_tasks: "setup-{{ ansible_distribution }}.yml"

View File

@@ -0,0 +1,121 @@
---
- name: Install software-properties-common
apt:
name: software-properties-common
state: present
- name:
shell: "sudo add-apt-repository universe"
- name:
shell: "sudo apt update"
- name: Update and upgrade apt packages
apt:
upgrade: yes
update_cache: yes
- name: Install python3-pip
apt:
name: python3-pip
state: present
- name: Upgrade pip
pip:
name: pip
state: latest
- name: Install docker
pip:
name: docker
state: present
- name: Install pyOpenSSL
pip:
name: pyOpenSSL
state: present
- name: Install Ansible
pip:
name: ansible
state: present
- name: Install jmespath
pip:
name: jmespath
state: present
- name: Install apache-libcloud
pip:
name: apache-libcloud
state: present
- name: Install jq
apt:
name: jq
state: present
- name: Install git
apt:
name: git
state: present
- name: Install htop
apt:
name: htop
state: present
- name: Install tmux
apt:
name: tmux
state: present
- name: Install zip
apt:
name: zip
state: present
- name: Install unzip
apt:
name: unzip
state: present
- name: Install rsync
apt:
name: rsync
state: present
- name: Install Silver Searcher
apt:
name: silversearcher-ag
state: present
- name: Install sysdig
apt:
name: sysdig
state: present
- name: Install ncdu
apt:
name: ncdu
state: present
- name: Install nfs-common
apt:
name: nfs-common
state: present
- name: Install httpie
apt:
name: httpie
state: present
- name: Install docker-compose
pip:
name: docker-compose
state: present
- name: Install openjdk-8-jre
apt:
name: openjdk-8-jre
state: present

View File

@@ -0,0 +1,72 @@
# tasks file for locales
---
- name: Update and upgrade apt packages
shell: sudo apt update
- name: Update and upgrade apt packages
apt:
upgrade: yes
update_cache: yes
- name: install
apt:
name: "{{ item }}"
state: "{{ apt_install_state | default('latest') }}"
update_cache: true
cache_valid_time: "{{ apt_update_cache_valid_time | default(3600) }}"
with_items: "{{ locales_dependencies }}"
tags: [configuration, locales, locales-install]
- name: install (specified) language packs
apt:
name: "{{ item }}"
state: "{{ apt_install_state | default('latest') }}"
with_items: "{{ locales_language_packs_present }}"
when: ansible_distribution == 'Ubuntu'
notify: update locales
tags: [configuration, locales, locales-language-packs-present]
- name: remove (specified) language packs
apt:
name: "{{ item }}"
state: absent
with_items: "{{ locales_language_packs_absent }}"
when: ansible_distribution == 'Ubuntu'
notify: update locales
tags: [configuration, locales, locales-language-packs-absent]
- name: detect locale gen file
stat:
path: "{{ locales_gen_file }}"
register: _locales_gen_file_stats
tags: [configuration, locales, locales-gen-file]
- name: create locale gen file
file:
path: "{{ locales_gen_file }}"
state: touch
when: _locales_gen_file_stats.stat.exists == false
tags: [configuration, locales, locales-gen-file]
- name: make sure (specified) locales are present
locale_gen:
name: "{{ item }}"
state: present
with_items: "{{ locales_present }}"
tags: [configuration, locales, locales-present]
- name: make sure (specified) locales are absent
locale_gen:
name: "{{ item }}"
state: absent
with_items: "{{ locales_absent }}"
tags: [configuration, locales, locales-absent]
- name: set default locale
template:
src: etc/default/locale.j2
dest: /etc/default/locale
owner: root
group: root
mode: 0644
tags: [configuration, locales, locales-default]

View File

@@ -0,0 +1,48 @@
# {{ ansible_managed }}
LANG="{{ locales_default.lang }}"
{% if locales_default.language is defined %}
LANGUAGE="{{ locales_default.language }}"
{% endif %}
{% if locales_default.lc_address is defined %}
LC_ADDRESS="{{ locales_default.lc_address }}"
{% endif %}
{% if locales_default.lc_all is defined %}
LC_ALL="{{ locales_default.lc_all }}"
{% endif %}
{% if locales_default.lc_collate is defined %}
LC_COLLATE="{{ locales_default.lc_collate }}"
{% endif %}
{% if locales_default.lc_ctype is defined %}
LC_CTYPE="{{ locales_default.lc_ctype }}"
{% endif %}
{% if locales_default.lc_identification is defined %}
LC_IDENTIFICATION="{{ locales_default.lc_identification }}"
{% endif %}
{% if locales_default.lc_measurement is defined %}
LC_MEASUREMENT="{{ locales_default.lc_measurement }}"
{% endif %}
{% if locales_default.lc_messages is defined %}
LC_MESSAGES="{{ locales_default.lc_messages }}"
{% endif %}
{% if locales_default.lc_monetary is defined %}
LC_MONETARY="{{ locales_default.lc_monetary }}"
{% endif %}
{% if locales_default.lc_name is defined %}
LC_NAME="{{ locales_default.lc_name }}"
{% endif %}
{% if locales_default.lc_numeric is defined %}
LC_NUMERIC="{{ locales_default.lc_numeric }}"
{% endif %}
{% if locales_default.lc_paper is defined %}
LC_PAPER="{{ locales_default.lc_paper }}"
{% endif %}
{% if locales_default.lc_response is defined %}
LC_RESPONSE="{{ locales_default.lc_response }}"
{% endif %}
{% if locales_default.lc_telephone is defined %}
LC_TELEPHONE="{{ locales_default.lc_telephone }}"
{% endif %}
{% if locales_default.lc_time is defined %}
LC_TIME="{{ locales_default.lc_time }}"
{% endif %}

View File

@@ -0,0 +1,5 @@
---
locales_dependencies:
- tzdata
- locales
locales_gen_file: "{{ '/var/lib/locales/supported.d/local' if ansible_distribution == 'Ubuntu' else '/etc/locale.gen' }}"

View File

@@ -0,0 +1,29 @@
---
language: python
python: "2.7"
# Use the new container infrastructure
sudo: false
# Install ansible
addons:
apt:
packages:
- python-pip
install:
# Install ansible
- pip install ansible
# Check ansible version
- ansible --version
# Create ansible.cfg with correct roles_path
- printf '[defaults]\nroles_path=../' >ansible.cfg
script:
# Basic role syntax check
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/

View File

@@ -0,0 +1,6 @@
---
# defaults file for cuda
cuda_version_major: 10
cuda_version_minor: 0
cuda_version_str: "v{{ cuda_version_major }}.{{ cuda_version_minor }}"
cuda_version_linux: "cuda-{{ cuda_version_major}}-{{ cuda_version_minor}}"

View File

@@ -0,0 +1,7 @@
---
- name: Environment configure
lineinfile: >
dest=/etc/profile regexp="^{{ item.regexp }}" line="{{ item.line }}"
with_items:
- { regexp: "export PATH={{ cuda_home }}", line: "export PATH={{ cuda_home }}/bin:$PATH" }
- { regexp: "export LD_LIBRARY_PATH={{ cuda_home }}", line: "export LD_LIBRARY_PATH={{ cuda_home }}/lib64:$LD_LIBRARY_PATH" }

View File

@@ -0,0 +1,13 @@
---
# Include variables and define needed variables.
# eg: vars/Ubuntu.yml
- name: Include OS-specific variables
include_vars: "{{ ansible_distribution }}.yml"
# Setup/install tasks.
# eg: tasks/setup-Ubuntu.yml
- include_tasks: "setup-{{ ansible_distribution }}.yml"
# Configure.
# eg: tasks/configure-Ubuntu.yml
- include_tasks: "configure-{{ ansible_distribution }}.yml"

View File

@@ -0,0 +1,20 @@
---
- name: Add an Apt signing key for CUDA
apt_key:
url: "{{ cuda_installer.cuda_apt_key }}"
state: present
- name: Install CUDA repo
apt: "deb={{ cuda_installer.deb }} state=present update_cache=yes"
register: install_repo_result
ignore_errors: True
- name: Skip if already installed or fail the play if the previous command did not succeed
fail: msg="the command failed {{ install_repo_result }}"
when: "install_repo_result.changed == 'false' and 'A later version is already installed' not in install_repo_result.stderr"
- name: Install CUDA package
apt: "name=cuda state=present update_cache=yes"
- name: Install CUDA {{ cuda_version_linux }}
apt: "name={{ cuda_version_linux }} state=present update_cache=yes"

View File

@@ -0,0 +1 @@
localhost

View File

@@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- cuda

View File

@@ -0,0 +1,33 @@
---
cuda_download_url_map:
bionic:
v10.1:
deb: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.1.243-1_amd64.deb
cuda_apt_key: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
v10.0:
deb: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
cuda_apt_key: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
xenial:
v8.0:
deb: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.61-1_amd64.deb
cuda_apt_key: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
v9.0:
deb: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.0.176-1_amd64.deb
cuda_apt_key: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
v9.1:
deb: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.1.85-1_amd64.deb
cuda_apt_key: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
v9.2:
deb: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.2.148-1_amd64.deb
cuda_apt_key: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
zesty:
v9.0:
deb: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1704/x86_64/cuda-repo-ubuntu1704_9.0.176-1_amd64.deb
cuda_apt_key: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1704/x86_64/7fa2af80.pub
v9.1:
deb: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1704/x86_64/cuda-repo-ubuntu1704_9.1.85-1_amd64.deb
cuda_apt_key: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1704/x86_64/7fa2af80.pub
cuda_installer: "{{ cuda_download_url_map[ansible_distribution_release][cuda_version_str] }}"
cuda_home: /usr/local/cuda

View File

@@ -0,0 +1,29 @@
---
language: python
python: "2.7"
# Use the new container infrastructure
sudo: false
# Install ansible
addons:
apt:
packages:
- python-pip
install:
# Install ansible
- pip install ansible
# Check ansible version
- ansible --version
# Create ansible.cfg with correct roles_path
- printf '[defaults]\nroles_path=../' >ansible.cfg
script:
# Basic role syntax check
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/

View File

@@ -0,0 +1,8 @@
---
cuda_version_major: 10
cuda_version_minor: 0
cuda_version_str: "v{{ cuda_version_major }}.{{ cuda_version_minor }}"
cuda_version_linux: "cuda-{{ cuda_version_major}}-{{ cuda_version_minor}}"
cudnn_version: 7.3
cudnn_version_str: "v{{ cudnn_version }}"

View File

@@ -0,0 +1,9 @@
---
# Include variables and define needed variables.
# eg: vars/Ubuntu.yml
- name: Include OS-specific variables
include_vars: "{{ ansible_distribution }}.yml"
# Setup task.
# eg: tasks/setup-Ubuntu.yml
- include_tasks: "setup-{{ ansible_distribution }}.yml"

View File

@@ -0,0 +1,3 @@
---
- name: Install CUDNN package
apt: "deb={{ cudnn_installer.deb }} state=present update_cache=yes"

View File

@@ -0,0 +1 @@
localhost

View File

@@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- cudnn

View File

@@ -0,0 +1,63 @@
---
# vars file for cudnn
cudnn_installer_map:
bionic:
v7.6:
v10.1:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/libcudnn7_7.6.4.38-1+cuda10.1_amd64.deb
v10.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/libcudnn7_7.6.4.38-1+cuda10.0_amd64.deb
v7.3:
v10.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/libcudnn7_7.3.0.29-1+cuda10.0_amd64.deb
v7:
v9.2:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn7_7.2.1.38-1+cuda9.2_amd64.deb
xenial:
v7:
v9.2:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn7_7.2.1.38-1+cuda9.2_amd64.deb
v9.1:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn7_7.1.3.16-1+cuda9.1_amd64.deb
v9.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn7_7.2.1.38-1+cuda9.0_amd64.deb
v8.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn7_7.2.1.38-1+cuda8.0_amd64.deb
v7-dev:
v9.2:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn7-dev_7.2.1.38-1+cuda9.2_amd64.deb
v9.1:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn7-dev_7.1.3.16-1+cuda9.1_amd64.deb
v9.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn7-dev_7.2.1.38-1+cuda9.0_amd64.deb
v8.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn7-dev_7.2.1.38-1+cuda8.0_amd64.deb
v6:
v8.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn6_6.0.21-1+cuda8.0_amd64.deb
v6-dev:
v8.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/libcudnn6-dev_6.0.21-1+cuda8.0_amd64.deb
zesty:
v7:
v9.1:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1404/x86_64/libcudnn7_7.1.3.16-1+cuda9.1_amd64.deb
v9.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1404/x86_64/libcudnn7_7.1.3.16-1+cuda9.0_amd64.deb
v8.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1404/x86_64/libcudnn7_7.1.3.16-1+cuda8.0_amd64.deb
v7-dev:
v9.1:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1404/x86_64/libcudnn7-dev_7.1.3.16-1+cuda9.1_amd64.deb
v9.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1404/x86_64/libcudnn7-dev_7.1.3.16-1+cuda9.0_amd64.deb
v8.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1404/x86_64/libcudnn7-dev_7.1.3.16-1+cuda8.0_amd64.deb
v6:
v8.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1404/x86_64/libcudnn6_6.0.21-1+cuda8.0_amd64.deb
v6-dev:
v8.0:
deb: http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1404/x86_64/libcudnn6-dev_6.0.21-1+cuda8.0_amd64.deb
cudnn_installer: "{{ cudnn_installer_map[ansible_distribution_release][cudnn_version_str][cuda_version_str]}}"

View File

@@ -0,0 +1,8 @@
.DS_Store
*/**.DS_Store
._*
.*.sw*
*~
.idea/
.vscode/
*.retry

View File

@@ -0,0 +1,178 @@
## What is ansible-docker?
It is an [Ansible](http://www.ansible.com/home) role to:
- Install Docker (editions, channels and version pinning are all supported)
- Install Docker Compose
- Manage login credentials for 1 or more public or private Docker registries
- Set up 1 or more users to run Docker without needing root access
- Configure a cron job to run Docker clean up commands
- Configure the Docker daemon's options and environment variables
## Why would you want to use this role?
If you're like me, you probably love Docker. This role provides everything you
need to get going with a production ready Docker host.
## Supported platforms
- Ubuntu 16.04 LTS (Xenial)
- Ubuntu 18.04 LTS (Bionic)
- Debian 8 (Jessie)
- Debian 9 (Stretch)
## Role variables
```
# When set to "True", the Docker package and supporting files will be removed.
docker_remove_package: False
# Do you want to use "ce" (community edition) or "ee" (enterprise edition)?
docker_edition: "ce"
# Do you want to use the "stable", "edge", "testing" or "nightly" channels?
# Add more than 1 channel by separating each one by a space.
docker_channel: "stable"
# When set to "latest" this role will always attempt to install the latest
# version based on the channel you selected. This could lead to something like
# Docker 18.06 being installed today but then a year from now, re-running the
# role will result in 19.06 or whatever Docker happens to use a year from now.
#
# If you want to pin a version simply put "18.06", "18.06.1" or whatever version
# you want. Even if you update your package list and newer Docker versions are
# available this role will stick to the pinned version on all future runs.
docker_version: "latest"
# Do you want to also install Docker Compose?
docker_install_docker_compose: True
# If Docker Compose is being installed, which version do you want to use?
docker_compose_version: "1.22.0"
# A list of users to be added to the docker group. For example if you have a
# user of "admin", then you'll want to set docker_users: ["admin"] here.
#
# Keep in mind this user needs to already exist, this role will not create it.
#
# This role does not configure User Namespaces or any other security features
# by default. If the user you add here has SSH access to your server then you're
# effectively giving them root access to the system since they can run docker
# without sudo and volume mount in any file on your file system.
#
# In a controlled environment this is safe, but like anything security related
# it's worth knowing this up front. You can enable User Namespaces and any
# other options with the docker_daemon_options variable which is explained later.
docker_users: []
# Manage login credentials for 1 or more Docker registries. Example usage:
# docker_registries:
# # Your registry URL is optional and defaults to the Docker Hub if undefined.
# - registry_url: "https://index.docker.io/v1/"
# # Your username is required.
# username: "your_docker_hub_username"
# # Your password is required.
# password: "your_docker_hub_password"
# # Your email address is optional (not all registries use it).
# email: "your_docker_hub@emailaddress.com"
# # Update your credentials. If undefined, this behavior will be skipped.
# reauthorize: False
# # Enable or disable these credentials by setting "present" or "absent".
# # If undefined, it will default to "present".
# state: "present"
# # The system user that will have access to the registry. If undefined it
# # will default to the root user. You likely want to set this to be a user
# # defined in your docker_users list above.
# system_user: "a_user_defined_in_docker_users"
docker_registries: []
# Docker daemon options as they would appear on the command line, such as:
# docker_daemon_options:
# - "--dns 8.8.8.8"
docker_daemon_options: []
# Can be used to set environment variables for the Docker daemon, such as:
# docker_daemon_environment:
# - "HTTP_PROXY=http://proxy.example.com:3128/"
# - "HTTPS_PROXY=http://proxy.example.com:3128/"
# - "NO_PROXY=localhost,127.0.0.1"
docker_daemon_environment: []
# Manage 1 or more cron jobs to perform Docker related system tasks. By default
# this will safely clean up disk space used by Docker every Sunday at midnight.
docker_cron_tasks:
- job: docker system prune -f &> /dev/null
name: "Docker disk clean up"
schedule: ["0", "0", "*", "*", "0"]
# A list of packages that Docker requires to run. Typically you shouldn't have
# to modify this list, but if Docker's dependencies change it can be updated
# here without having to fork the role.
docker_package_dependencies:
- "apt-transport-https"
- "ca-certificates"
- "software-properties-common"
- "gnupg2"
- "cron"
# The Docker GPG key id used to sign the Docker package.
docker_apt_key_id: "9DC858229FC7DD38854AE2D88D81803C0EBFCD88"
# The Docker GPG key server address.
docker_apt_key_server: "https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg"
# The Docker APT repository.
docker_apt_repository: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} {{ docker_channel }}"
# How long should the apt-cache last in seconds?
docker_apt_cache_time: 86400
```
## Example usage
For the sake of this example let's assume you have a group called **app** and
you have a typical `site.yml` file.
To use this role edit your `site.yml` file to look something like this:
```
---
- name: Configure app server(s)
hosts: "app"
become: True
roles:
- { role: "ansible-docker", tags: ["docker"] }
```
Here's a few examples. You can recreate this example on your end by opening or
creating `group_vars/app.yml` which is located relative to your `inventory`
directory and then making it look like this:
```
---
# Pin Docker version 18.06 from the stable channel.
docker_version: "18.06"
# Allow the admin and zerocool users to access Docker without needing root access.
docker_users: ["admin", "zerocool"]
# A couple of examples of authenticating to a Docker registry.
docker_registries:
# Authenticate to the Docker Hub, and allow the "admin" system user acces to it:
- username: "your_docker_hub_username"
password: "your_docker_hub_password"
email: "your_docker_hub@emailaddress.com"
system_user: "{{ docker_users | first }}"
# Authenticate to some other private registry and allow "zerocool":
- registry_url: "https://your-registry.com"
username: "some_other_username"
password: "some_other_password"
system_user: "zerocool"
# Disable logging in to some old registry you don't use anymore:
- registry_url: "https://old-registry.com"
username: "some_old_username"
password: "some_old_password"
state: "absent"
```
Now you would run `ansible-playbook -i inventory/hosts site.yml -t docker`.

View File

@@ -0,0 +1,34 @@
---
docker_remove_package: False
docker_edition: "ce"
docker_channel: "stable"
docker_version: "18.06.1"
docker_install_docker_compose: True
docker_compose_version: "1.22.0"
docker_users: ["ubuntu"]
docker_registries: []
docker_daemon_options: []
docker_daemon_environment: []
docker_cron_tasks:
- job: docker system prune -f &> /dev/null
name: "Docker clean up"
schedule: ["0", "0", "*", "*", "0"]
docker_package_dependencies:
- "apt-transport-https"
- "ca-certificates"
- "software-properties-common"
- "gnupg2"
- "cron"
docker_apt_key_id: "9DC858229FC7DD38854AE2D88D81803C0EBFCD88"
docker_apt_key_server: "https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg"
docker_apt_repository: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} {{ docker_channel }}"
docker_apt_cache_time: 86400

View File

@@ -0,0 +1,6 @@
---
- name: Restart Docker
service:
name: "docker"
state: "restarted"

View File

@@ -0,0 +1,163 @@
---
- name: Disable pinned docker-ce version
file:
dest: "/etc/apt/preferences.d/docker-ce.pref"
state: "absent"
when: docker_remove_package or docker_version == "" or docker_version == "latest"
- name: Disable pinned docker-ce-cli version
file:
dest: "/etc/apt/preferences.d/docker-ce-cli.pref"
state: "absent"
when: docker_remove_package or docker_version == "" or docker_version == "latest"
- name: Pin docker-ce version
template:
src: "etc/apt/preferences.d/docker-ce.pref.j2"
dest: "/etc/apt/preferences.d/docker-ce.pref"
owner: "root"
group: "root"
mode: "0644"
when: not docker_remove_package and (docker_version and docker_version != "latest")
- name: Pin docker-ce-cli version
template:
src: "etc/apt/preferences.d/docker-ce-cli.pref.j2"
dest: "/etc/apt/preferences.d/docker-ce-cli.pref"
owner: "root"
group: "root"
mode: "0644"
when: not docker_remove_package and (docker_version and docker_version != "latest")
- name: Install Docker's dependencies
apt:
name: "{{ item }}"
update_cache: True
cache_valid_time: "{{ docker_apt_cache_time }}"
loop: "{{ docker_package_dependencies }}"
- name: Add Docker's GPG key
apt_key:
id: "{{ docker_apt_key_id }}"
url: "{{ docker_apt_key_server }}"
- name: Configure upstream APT repository
apt_repository:
repo: "{{ docker_apt_repository }}"
- name: Remove Docker
apt:
name: "docker-{{ docker_edition }}"
state: "absent"
when: docker_remove_package
- name: Install Docker
apt:
name: "docker-{{ docker_edition }}"
update_cache: True
cache_valid_time: "{{ docker_apt_cache_time }}"
state: "latest"
when: not docker_remove_package
- name: Remove Upstart config file
file:
path: "/etc/default/docker"
state: "absent"
- name: Ensure systemd directory exists
file:
path: "/etc/systemd/system"
state: "directory"
owner: "root"
group: "root"
mode: "0755"
- name: Remove systemd unit file
file:
path: "/etc/systemd/system/docker.service"
state: "absent"
when: docker_remove_package
#- name: Generate systemd unit file
# template:
# src: "etc/systemd/system/docker.service.j2"
# dest: "/etc/systemd/system/docker.service"
# owner: "root"
# group: "root"
# mode: "0644"
# register: docker_register_systemd_service
# when: not docker_remove_package
- name: Reload systemd daemon
systemd:
daemon_reload: True
notify: ["Restart Docker"]
when: not docker_remove_package or docker_register_systemd_service is changed
- name: Add user(s) to "docker" group
user:
name: "{{ item }}"
groups: "docker"
append: True
loop: "{{ docker_users }}"
when: not docker_remove_package and docker_users
- name: Install Python for managing Docker login credentials
apt:
name: "python"
update_cache: True
cache_valid_time: "{{ docker_apt_cache_time }}"
when: not docker_remove_package and docker_registries
- name: Install docker-py for managing Docker login credentials
pip:
name: "docker-py"
when: not docker_remove_package and docker_registries
- name: Manage login credentials for 1 or more Docker registries
docker_login:
registry_url: "{{ item.registry_url | default(omit) }}"
username: "{{ item.username }}"
password: "{{ item.password }}"
email: "{{ item.email | default(omit)}}"
reauthorize: "{{ item.reauthorize | default(omit) }}"
state: "{{ item.state | default(omit) }}"
loop: "{{ docker_registries }}"
when: not docker_remove_package and docker_registries
become_user: "{{ item.system_user | default(omit) }}"
- name: Remove Docker Compose
file:
path: "/usr/local/bin/docker-compose"
state: "absent"
when: docker_remove_package
- name: Install Docker Compose
get_url:
url: "https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-Linux-x86_64"
dest: "/usr/local/bin/docker-compose"
force: True
owner: "root"
group: "root"
mode: "0755"
when: not docker_remove_package and docker_install_docker_compose
- name: Remove cron tasks for Docker commands
cron:
name: "{{ item.name }}"
state: "absent"
loop: "{{ docker_cron_tasks }}"
when: docker_remove_package and docker_cron_tasks
- name: Create cron tasks for Docker commands
cron:
name: "{{ item.name }}"
job: "{{ item.job }}"
minute: "{{ item.schedule[0] }}"
hour: "{{ item.schedule[1] }}"
day: "{{ item.schedule[2] }}"
month: "{{ item.schedule[3] }}"
weekday: "{{ item.schedule[4] }}"
loop: "{{ docker_cron_tasks }}"
when: not docker_remove_package and docker_cron_tasks

View File

@@ -0,0 +1,4 @@
Explanation: Pin added by Ansible role "{{ role_name }}"
Package: docker-ce-cli
Pin: version {{ docker_version }}*
Pin-Priority: 600

View File

@@ -0,0 +1,4 @@
Explanation: Pin added by Ansible role "{{ role_name }}"
Package: docker-ce
Pin: version {{ docker_version }}*
Pin-Priority: 600

View File

@@ -0,0 +1,34 @@
# {{ ansible_managed }}
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket
Requires=docker.socket
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
{% if docker_daemon_environment %}
Environment="{{ docker_daemon_environment | join('" "') }}"
{% endif %}
ExecStart=/usr/bin/dockerd {{ docker_daemon_options | join(" ") }}
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
[Install]
WantedBy=multi-user.target

View File

@@ -0,0 +1,59 @@
---
- hosts: "all"
become: True
vars:
docker_version: "18.03"
docker_users: ["test"]
docker_daemon_options:
- "--dns 8.8.8.8"
roles:
- "role_under_test"
pre_tasks:
- name: Add test user
user:
name: "test"
shell: "/bin/bash"
post_tasks:
- name: Ensure Docker gets installed
command: docker --version
register: result_pinned
changed_when: result_pinned.rc != 0
- name: Run the equivalent of "apt-get update"
apt:
update_cache: True
changed_when: False
- name: Ensure pinned Docker version wasn't updated
# This is valid because 18.06+ is currently out. If the pin failed then
# this docker --version would be different from the previous check.
command: docker --version
register: result
changed_when: result.stdout != result_pinned.stdout
- name: Ensure test user can access Docker without root access
command: groups test
register: result
changed_when: result.stdout.split(":")[1] | trim != "test docker"
- name: Ensure Docker Compose gets installed
command: docker-compose --version
register: result
changed_when: result.rc != 0
- name: Ensure /etc/systemd/system/docker.service contains '--dns 8.8.8.8'
# Double dashes: https://unix.stackexchange.com/a/11382
command: grep -- "--dns 8.8.8.8" /etc/systemd/system/docker.service
register: result
changed_when: result.rc != 0
- name: Ensure Docker clean up cron job was added
command: crontab -l
register: result
changed_when: not result.stdout is search("docker system prune -f")

View File

@@ -0,0 +1,5 @@
---
ubuntu_version: ubuntu18.04
nvidia_docker_version: 2.0.3+docker18.09.0-1
nvidia_container_runtime_major_version: 2.0.0

View File

@@ -0,0 +1,4 @@
- name: restart docker service
systemd:
name: docker
state: restarted

View File

@@ -0,0 +1,19 @@
---
- name: nvidia-modprobe is installed
apt:
name: nvidia-modprobe
state: present
update_cache: yes
- name: nvidia-docker (v2) is uninstalled
apt:
name: nvidia-docker2
state: absent
notify: restart docker service
- name: nvidia-docker (v1) is installed
apt:
deb: '{{ nvidia_docker_deb_url }}'
state: present
notify: restart docker service

View File

@@ -0,0 +1,79 @@
---
- name: nvidia-docker (v2) release key is registered
apt_key:
url: https://nvidia.github.io/nvidia-docker/gpgkey
state: present
- name: software-properties-common is installed for prerequisite for apt_repository ansible module
apt:
name: software-properties-common
state: present
- name: nvidia-docker (v2) apt repository is registered
apt_repository:
repo: "{{ item }}"
filename: nvidia-docker
state: present
mode: 0644
update_cache: yes
with_items:
- 'deb https://nvidia.github.io/libnvidia-container/{{ ubuntu_version }}/amd64 /'
- 'deb https://nvidia.github.io/nvidia-container-runtime/{{ ubuntu_version }}/amd64 /'
- 'deb https://nvidia.github.io/nvidia-docker/{{ ubuntu_version }}/amd64 /'
- name: nvidia-docker (v1) is uninstalled
apt:
name: nvidia-docker
state: absent
- name: confirm content of /etc/docker/daemon.json
command: cat /etc/docker/daemon.json
register: nvidia_docker_daemon_json_backup
failed_when: False
changed_when: False
- name: Pin nvidia-docker2 version
template:
src: "etc/apt/preferences.d/nvidia-docker2.pref.j2"
dest: "/etc/apt/preferences.d/nvidia-docker2.pref"
owner: "root"
group: "root"
mode: "0644"
- name: Pin nvidia-container-runtime version
template:
src: "etc/apt/preferences.d/nvidia-container-runtime.pref.j2"
dest: "/etc/apt/preferences.d/nvidia-container-runtime.pref"
owner: "root"
group: "root"
mode: "0644"
- name: nvidia-docker (v2) is installed
apt:
name: "nvidia-docker2={{ nvidia_docker_version }}"
state: present
notify: restart docker service
- name: confirm content of /etc/docker/daemon.json after installation of nvidia-docker2
command: cat /etc/docker/daemon.json
register: nvidia_docker_daemon_json_v2
changed_when: False
- name: content of /etc/docker/daemon.json is merged
copy:
content: "{{ nvidia_docker_daemon_json_backup.stdout | from_json | combine(nvidia_docker_daemon_json_v2.stdout | from_json) }}"
dest: /etc/docker/daemon.json
validate: python3 -m json.tool %s
when: nvidia_docker_daemon_json_backup.stdout != ""
- name: confirm content of /etc/docker/daemon.json after installation of nvidia-docker2
command: cat /etc/docker/daemon.json
register: nvidia_docker_daemon_json_v3
changed_when: False
- name: content of /etc/docker/daemon.json is merged
copy:
content: "{{ nvidia_docker_daemon_json_v3.stdout | from_json | combine({'default-runtime':'nvidia'}) | to_nice_json }}"
dest: /etc/docker/daemon.json
validate: python3 -m json.tool %s

View File

@@ -0,0 +1,19 @@
---
- name: import task for Debian derivatives
include_tasks: debian_v1.yml
when: ansible_os_family == "Debian" and nvidia_docker_version.startswith('1')
- name: import task for Debian derivatives
include_tasks: debian_v2.yml
when: ansible_os_family == "Debian" and nvidia_docker_version.startswith('2')
- name: Reboot machine after nvidia-docker installed
shell: 'sleep 1 && shutdown -r now "Reboot triggered by Ansible" && sleep 1'
async: 1
poll: 0
- name: Wait for system to become reachable
wait_for_connection:
delay: 60
sleep: 30
timeout: 600

View File

@@ -0,0 +1,4 @@
Explanation: Pin added by Ansible role "{{ role_name }}"
Package: nvidia-container-runtime
Pin: version {{ nvidia_container_runtime_major_version }}+{{ nvidia_docker_version .split("+")[1] }}*
Pin-Priority: 600

View File

@@ -0,0 +1,4 @@
Explanation: Pin added by Ansible role "{{ role_name }}"
Package: nvidia-docker2
Pin: version {{ nvidia_docker_version }}*
Pin-Priority: 600

View File

@@ -0,0 +1,3 @@
---
nvidia_docker_deb_url: https://github.com/nvidia/nvidia-docker/releases/download/v{{ nvidia_docker_version }}/nvidia-docker_{{ nvidia_docker_version | regex_replace('-', '.') }}-1_amd64.deb

View File

@@ -1,12 +0,0 @@
---
all:
vars:
ansible_python_interpreter: /usr/bin/python3
test:
hosts:
test1:
ansible_host: 0.0.0.0 # Replace with your remote IP
ansible_port: 38775 # Replace with your remote SSH port
ansible_user: master # Replace wtih your username
ansible_ssh_pass: password
ansible_sudo_pass: password