Centos 7 docker
What ?
This is my Ansible playbook to automatically replace docker ( if installed from yum ) update to latest stable docker-ce version and add latest docker-compose.
Why ?
I had to update couple of Centos 7 servers that run old ( very very old ) docker. So this was kind of shortcut ? I spend more time making sure this playbook works that it would take to do it manually. 🙂
But thats mostly because I have tried to use git module to get the latest version number of docker-compose... this of course does not work with python 2.7. Switching the playbook to python 3+ broke the package / yum module and suggested dnf module also did not work on Centos 7. Honestly, this is my biggest love/hate with Ansilbe. On one hand you can do so much quite easily and its maintainable... on the other hand, we known that python 2 will be obsolete for years and Ansible kinda said meh... it will sort it self out eventually... Now we have modules that work under 2.7 up to some version, some now do not support Python 2 at all. Switching to Python 3 breaks stuff. In the end Python 2 will not be even installed anymore so go ahead and create playbooks that will work in every possible situation, going back to shell scripts suddenly does not look as such a bad idea. 🙂
How ?
Copy the yaml file into some file where you have Ansible installed, lets say main.yaml
Change: hosts
, remote_user
to match your target and run it.
vladoportos@Odin:~/ansible/docker-ce$ ansible-playbook main.yml
Playbook
---
- name: Install Docker-ce Centos 7
hosts: 10.0.0.114
remote_user: vladoportos
become: true
become_method: sudo
gather_facts: false
tasks:
- name: Remove docker if installed from CentOS repo
yum:
name:
- docker
- docker-client
- docker-client-latest
- docker-common
- docker-latest
- docker-latest-logrotate
- docker-logrotate
- docker-engine
state: absent
- name: Install required pkgs
yum:
name:
- yum-utils
- device-mapper-persistent-data
- lvm2
state: latest
- name: Add Docker repo
get_url:
url: https://download.docker.com/linux/centos/docker-ce.repo
dest: /etc/yum.repos.d/docker-ce.repo
- name: Install Docker
yum:
name:
- docker-ce
- docker-ce-cli
- containerd.io
state: latest
- name: Convert old images to new runtime
shell:
cmd: " grep -rl 'docker-runc' /var/lib/docker/containers/ | xargs sed -i 's/docker-runc/runc/g' "
changed_when: false
failed_when: false
- name: Start Docker service
service:
name: docker
state: started
enabled: yes
- 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: Check if /usr/local/bin/docker-compose exists.
stat:
path: /usr/local/bin/docker-compose
register: docker_compose_file
- name: Check current docker-compose version
shell:
cmd: /usr/local/bin/docker-compose --version | cut -d ' ' -f3 | sed 's/.$//'
register: docker_compose_vsn
changed_when: false
failed_when: false
args:
warn: no
when:
- docker_compose_file.stat.exists
- set_fact:
docker_compose_current_version: "{{ docker_compose_vsn.stdout }}"
when:
- docker_compose_vsn.stdout is defined and docker_compose_file.stat.exists
- name: Install or upgrade docker-compose
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: 'a+x'
force: yes
when: >
docker_compose_current_version is not defined
or docker_compose_current_version is version(docker_compose_latest, '<')