Skip to content

Instantly share code, notes, and snippets.

@guilhermegazzinelli
Created December 20, 2022 22:40
Show Gist options
  • Save guilhermegazzinelli/20cb9c51a80a6fcc3504d9928fdb0311 to your computer and use it in GitHub Desktop.
Save guilhermegazzinelli/20cb9c51a80a6fcc3504d9928fdb0311 to your computer and use it in GitHub Desktop.
Setup docker and docker compose with ansible in remote server
---
- hosts: all
become: true
tasks:
- name: Install aptitude
apt:
name: aptitude
state: latest
update_cache: true
# Update and install the base software
- name: Update apt package cache
apt:
update_cache: yes
cache_valid_time: 3600
- name: Upgrade installed apt packages
apt:
upgrade: dist
register: upgrade
retries: 15
delay: 5
until: upgrade is success
- name: Install required system packages
apt:
pkg:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
- python3-pip
- virtualenv
- python3-setuptools
state: latest
update_cache: true
# - name: Add signing key
# ansible.builtin.apt_key:
# url: "https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg"
# state: present
- name: Add signing key
ansible.builtin.apt_key:
url: "https://download.docker.com/linux/ubuntu/gpg"
state: present
- name: Add repository into sources list
ansible.builtin.apt_repository:
repo: "deb [arch={{ ansible_architecture }}] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable"
state: present
filename: docker
- name: Install Docker
ansible.builtin.apt:
name:
- docker
- docker.io
- docker-registry
state: latest
update_cache: true
- name: Get version number of latest docker-compose
shell:
cmd: curl --silent 'https://api.github.com/repos/docker/compose/releases/latest' | grep '"tag_name":' | cut -d'"' -f4
register: docker_compose_latest_svn
changed_when: false
args:
warn: no
- set_fact:
docker_compose_latest: "{{ docker_compose_latest_svn.stdout }}"
when:
- docker_compose_latest_svn.stdout is defined
- name: Install docker-compose from official github repo
get_url:
url : https://github.com/docker/compose/releases/download/{{ docker_compose_latest }}/docker-compose-linux-x86_64
dest: /usr/local/bin/docker-compose
mode: 'u+x,g+x'
- name: update "/usr/local/bin/docker-compose" owner group mode
file:
path: /usr/local/bin/docker-compose
group: docker # to give docker-compose to docker group,
mode: "0750" # to allow docker group users to execute it
- name: Get docker version
ansible.builtin.shell: docker -v
register: command_output_docker_version
- debug:
var: command_output_docker_version.stdout_lines
- name: Get docker-compose version
ansible.builtin.shell: docker-compose -v
register: command_output_docker_compose_version
- debug:
var: command_output_docker_compose_version.stdout_lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment