Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install docker and docker-compose using ansible https://www.srijn.net/install-docker-and-docker-compose-using-ansible/
---
- name: Install packages using apt
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg2
- software-properties-common
state: present
update_cache: no
- name: Add Docker GPG apt Key
apt_key:
url: "{{ docker_apt_gpg_key }}"
state: present
- name: Add Docker Repository
apt_repository:
repo: "{{ docker_apt_repository }}"
state: present
update_cache: true
- name: Update apt and install docker-ce
apt: update_cache=no name=docker-ce state=present
- name: Check current docker-compose version.
command: docker-compose --version
register: docker_compose_vsn
changed_when: false
failed_when: false
check_mode: no
- set_fact:
docker_compose_current_version: "{{ docker_compose_vsn.stdout | regex_search('(\\d+(\\.\\d+)+)') }}"
when:
- docker_compose_vsn.stdout is defined
- name: Install or upgrade 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
mode: 'a+x'
force: yes
when: >
docker_compose_current_version is not defined
or docker_compose_current_version is version(docker_compose_version, '<')
- name: Run and enable docker
service:
name: docker
state: started
enabled: true
---
docker_apt_release_channel: stable
docker_apt_arch: amd64
docker_apt_repository: "deb [arch={{ docker_apt_arch }}] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} {{ docker_apt_release_channel }}"
docker_apt_gpg_key: https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg
docker_compose_version: "1.24.0"
@schaerli
Copy link

schaerli commented Aug 5, 2021

Hey many thank you for this. I have two little changes. Name for set_fact and docker_compose_current_version == "".

- name: set docker compose current version
  set_fact:
    docker_compose_current_version: "{{ docker_compose_vsn.stdout | regex_search('(\\d+(\\.\\d+)+)') }}"
  when:
    - docker_compose_vsn.stdout is defined
- name: Install or upgrade 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
    mode: 'a+x'
    force: yes
  when:
    - docker_compose_current_version == "" or docker_compose_current_version is version(docker_compose_version, '<')

@jaydrogers
Copy link

jaydrogers commented Sep 22, 2021

docker_compose_current_version == ""

This helped me out a ton (especially for new server provisions that do not have docker-compose installed yet). Thanks @schaerli!

@NurdinDev
Copy link

NurdinDev commented Jan 15, 2022

Thank you @srijan for your article, I faced an issue during the Install or upgrade docker-compose step
I got this error Destination /usr/local/bin/docker-compose is not writable after some researches about the issue I tried to add become: true to this step and it's worked for me.
so is it working with you without become?

@srijan
Copy link
Author

srijan commented Jan 16, 2022

@NurdinDev - I have set become: true in the main file where this task is imported. It's required for all the steps here, not just the compose step.

@NurdinDev
Copy link

NurdinDev commented Jan 16, 2022

I got it @srijan, Thank you so much 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment