Skip to content

Instantly share code, notes, and snippets.

@yonglai
Created November 15, 2017 18:04
Show Gist options
  • Save yonglai/d4617d6914d5f4eb22e4e5a15c0e9a03 to your computer and use it in GitHub Desktop.
Save yonglai/d4617d6914d5f4eb22e4e5a15c0e9a03 to your computer and use it in GitHub Desktop.
An Ansible playbook to install docker-ce on Centos
---
- name: Install docker
gather_facts: No
hosts: default
tasks:
- name: Install yum utils
yum:
name: yum-utils
state: latest
- name: Install device-mapper-persistent-data
yum:
name: device-mapper-persistent-data
state: latest
- name: Install lvm2
yum:
name: lvm2
state: latest
- name: Add Docker repo
get_url:
url: https://download.docker.com/linux/centos/docker-ce.repo
dest: /etc/yum.repos.d/docer-ce.repo
become: yes
- name: Enable Docker Edge repo
ini_file:
dest: /etc/yum.repos.d/docer-ce.repo
section: 'docker-ce-edge'
option: enabled
value: 0
become: yes
- name: Enable Docker Test repo
ini_file:
dest: /etc/yum.repos.d/docer-ce.repo
section: 'docker-ce-test'
option: enabled
value: 0
become: yes
- name: Install Docker
package:
name: docker-ce
state: latest
become: yes
- name: Start Docker service
service:
name: docker
state: started
enabled: yes
become: yes
- name: Add user vagrant to docker group
user:
name: vagrant
groups: docker
append: yes
become: yes
@tongtie
Copy link

tongtie commented Feb 17, 2020

@yonglai, why are you adding vagrant to the docker-group?

maybe he used vagrant software do this stuff,so he can docker run xxx with vagrant user

@yogeshssawant
Copy link

Hi Folks,

I am getting error adter building all workaround discussed in this forum:

Error:
ERROR! Syntax Error while loading YAML.
did not find expected '-' indicator

The error appears to be in '/root/ansible-playbooks/docker_centos1/playbook1.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  • name: Remove Docker
    gather_facts: No
    ^ here

Playbook:


  • name: Remove Docker
    gather_facts: No
    hosts: all

tasks:

  • name: Remove docker if installed from CentOS repo
    yum:
    name: "{{ item }}"
    state: removed

    with_items:
    - docker
    - docker-client
    - docker-client-latest
    - docker-common
    - docker-latest
    - docker-latest-logrotate
    - docker-logrotate
    - docker-engine

tasks:

-   name: Install yum utils
  yum:
    name: "{{ item }}"
    state: latest

with_items:

        - yum-utils
          - device-mapper-persistent-data
            - lvm2

-  name: Add Docker repo
  get_url:
    url: https://download.docker.com/linux/centos/docker-ce.repo
    dest: /etc/yum.repos.d/docker-ce.repo
  become: yes

-  name: Enable Docker Edge & Test repo


-  name: Install Docker
  package:
    name: docker-ce
    state: latest
  become: yes

-  name: Start Docker service
  service:
    name: docker
    state: started
    enabled: yes
  become: yes

-  name: Add user yogesh to docker group
  user:
    name: yogesh
    groups: docker
    append: yes
  become: yes

@pc-star
Copy link

pc-star commented Jun 26, 2020

@yogeshssawant : you can try with the following..and see how it goes.

  • No real reasons to declare more than once the task value.
  • Be careful as you might had miss to declare the become: true instruction, in several points in the playbook. Easier to do it once, at the top of it, if you know it will be used for most of it.
  • One of the tasks it is completely missing

I really suggest to specify the host you want to use as a target for this playbook instead of using all .

In the near future try to give a try to roles. I do prefer them as grants you a bit more of flexibility in what you can do in your tasks.

Playbook:

gather_facts: false
hosts: all
become: true

tasks:

- name: Remove docker if installed from CentOS repo
  yum:
       name: "{{ item }}"
       state: removed

   with_items:
    - docker
    - docker-client
    - docker-client-latest
    - docker-common
    - docker-latest
    - docker-latest-logrotate
    - docker-logrotate
    - docker-engine

- name: Install yum utils
  yum:
      name: "{{ item }}"
      state: latest
  with_items:
        - yum-utils
        - device-mapper-persistent-data
        - lvm2

- 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: Enable Docker Edge & Test repo

   #### Something it is missing here ####

- name: Install Docker
  yum:
    name: docker-ce
    state: latest

- name: Start Docker service
  service:
    name: docker
    state: started
    enabled: yes
  
- name: Add user yogesh to docker group
  user:
      name: yogesh
      groups: docker
      append: yes

@dridi-mohamed
Copy link

its work ... thanks bro ... good work

@a19singh
Copy link

TASK [Install Docker]
FAILED! => {"changed": false, "failures": [], "msg": "Depsolve Error occured: \n Problem: cannot install the best candidate for the job\n - nothing provides libcgroup needed by docker-ce-3:19.03.12-3.el7.x86_64\n - nothing provides container-selinux >= 2:2.74 needed by docker-ce-3:19.03.12-3.el7.x86_64", "rc": 1, "result": []}

when installing in RHEL 8

@pc-star
Copy link

pc-star commented Jul 27, 2020

@a19singh : your best option at the moment it is to change the task as for the following

current

  • name: Install Docker
    yum:
    name: docker-ce
    state: latest

RHEL 8 version

  • name: Install Docker
    shell: "dnf install --nobest docker-ce"

A more clean solution It would be to declare both tasks and set a when conditional, in order to evaluate which version of the current OS it is running and, based on that execute the related task.

The error you see it is due to the fact RH it is trying to stop the usage of docker on their systems, making use of their own tools ( buildah and podman ).

At today the merging of the usage of the --nobest option into the dnf module for ansible it is currently ongoing, that's the reason why it still be necessary to declare it as a ( shell ) command.

source : ansible/ansible#70318

@dyasny
Copy link

dyasny commented Aug 25, 2020

Actually, this is because RHEL's container-tools module provides it's own runc binary, which conflicts with the runc docker-ce provides.

@pacjin79
Copy link

this worked very well for me on centos7, thanks for sharing!

@Josemyr1993
Copy link

Worked for me, many thks

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