~gjabell/ansible-docker-install

f7df9c5bb17740d838016494a8ac6aeea10821e4 — Galen Abell 2 years ago
Initial commit
A  => README.md +14 -0
@@ 1,14 @@
# ansible docker install

## Use

- Add your hosts to the `hosts` file (see file for examples + more info)
- Optionally add a list of users that should be added to the `docker` group to the `users` var in the `playbooks/docker.yml` playbook
- Optionally enable the `docker-compose` role to also install `docker-compose` by uncommenting that section of the `playbooks/docker.yml` playbook
- Run the `playbooks/docker.yml` playbook with: `ansible-playbook playbooks/docker.yml`

## Notes

- Adding users to the `docker` group essentially gives them root access, so be careful who you add to this group
- You might need to tweak some settings such as a) `become_pass` for the root user, b) login credentials (ie ssh / password / etc.) c) path to ansible python interpreter


A  => ansible.cfg +4 -0
@@ 1,4 @@
[defaults]
inventory=./hosts
retry_files_enalbed=False
roles_path=./roles

A  => hosts +12 -0
@@ 1,12 @@
[all]
# add your hosts here, in the form:
# [hostname] ansible_host=[ip]
#
# for example:
# server1 ansible_host=192.168.1.1
#
# note: you can also specify the python path, since this may be different
# across different operating systems.
#
# example:
# server1 ansible_host=192.168.1.1 ansible_python_interpreter=/usr/bin/python

A  => playbooks/docker.yml +11 -0
@@ 1,11 @@
---
- hosts: all
  # optional: specify a list of users that should be in the docker group
  vars:
    users: []
  roles:
    - role: docker
      become: yes
    # optional: also install docker compose
    #- role: docker-compose
    #  become: yes

A  => roles/docker-compose/tasks/centos.yml +14 -0
@@ 1,14 @@
---
- name: Enable EPEL
  yum:
    name: epel-release
    state: latest

- name: Install pip
  yum:
    name: python-pip
    state: latest

- name: Install docker-compose
  pip:
    name: docker-compose

A  => roles/docker-compose/tasks/debian.yml +9 -0
@@ 1,9 @@
---
- name: Install pip
  apt:
    name: python3-pip
    state: latest

- name: Install docker-compose
  pip:
    name: docker-compose

A  => roles/docker-compose/tasks/main.yml +8 -0
@@ 1,8 @@
---
- name: Run Debian install
  import_tasks: debian.yml
  when: ansible_facts['os_family'] == 'Debian'

- name: Run CentOS install
  import_tasks: centos.yml
  when: ansible_facts['os_family'] == 'RedHat'

A  => roles/docker/handlers/main.yml +6 -0
@@ 1,6 @@
---
- name: Start Docker
  service:
    name: docker
    state: started
    enabled: yes

A  => roles/docker/tasks/centos.yml +47 -0
@@ 1,47 @@
---
- name: Remove existing Docker installations
  yum:
    name: '{{ packages }}'
    state: absent
  vars:
    packages:
      - docker
      - docker-client
      - docker-client-latest
      - docker-common
      - docker-latest
      - docker-latest-logrotate
      - docker-logrotate
      - docker-engine

- name: Install dependencies
  yum:
    name: '{{ dependencies }}'
    state: latest
  vars:
    dependencies:
      - yum-utils
      - device-mapper-persistent-data
      - lvm2

- name: Add Docker repo
  command: yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

- name: Install Docker
  yum:
    name: '{{ docker }}'
    state: latest
  notify:
    - Start Docker
  vars:
    docker:
      - docker-ce
      - docker-ce-cli
      - containerd.io

- name: Add Users to Docker group
  user:
    name: '{{ item }}'
    append: yes
    groups: docker
  loop: '{{ users }}'

A  => roles/docker/tasks/debian.yml +52 -0
@@ 1,52 @@
---
- name: Remove existing Docker installations
  apt:
    name: '{{ packages }}'
    state: absent
  vars:
    packages:
      - docker
      - docker-engine
      - docker.io
      - containerd
      - runc

- name: Install dependencies
  apt:
    name: '{{ dependencies }}'
    state: latest
    update_cache: yes
  vars:
    dependencies:
      - apt-transport-https
      - ca-certificates
      - curl
      - '{{ "gnupg-agent" if ansible_distribution == "Ubuntu" else "gnupg2" }}'
      - software-properties-common

- name: Add Docker GPG key
  apt_key:
    id: 0EBFCD88
    url: 'https://download.docker.com/linux/{{ ansible_distribution|lower }}/gpg'
    state: present

- name: Add Docker stable repository
  apt_repository:
    repo: >
      deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution|lower }} {{ ansible_distribution_release }} stable
    state: present

- name: Install Docker
  apt:
    name: docker-ce
    state: latest
    update_cache: yes
  notify:
    - Start Docker

- name: Add Users to Docker Group
  user:
    name: '{{ item }}'
    append: yes
    groups: docker
  loop: '{{ users }}'

A  => roles/docker/tasks/main.yml +8 -0
@@ 1,8 @@
---
- name: Run Debian install
  import_tasks: debian.yml
  when: ansible_facts['os_family'] == 'Debian'

- name: Run CentOS install
  import_tasks: centos.yml
  when: ansible_facts['os_family'] == 'RedHat'