Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@JinnaBalu

This comment has been minimized.

Copy link

JinnaBalu commented Apr 24, 2018

Isuue running the playbook

TASK [Add Docker repo] ***********************************************************************************************************************************************
fatal: [app_server]: FAILED! => {"changed": false, "msg": "Failed to validate the SSL certificate for download.docker.com:443. Make sure your managed systems have a valid CA certificate installed. If the website serving the url uses SNI you need python >= 2.7.9 on your managed machine (the python executable used (/usr/bin/python) is version: 2.7.5 (default, Jun 17 2014, 18:11:42) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)]) or you can install the urllib3, pyOpenSSL, ndg-httpsclient, and pyasn1 python modules to perform SNI verification in python >= 2.6. You can use validate_certs=False if you do not need to confirm the servers identity but this is unsafe and not recommended. Paths checked for this platform: /etc/ssl/certs, /etc/pki/ca-trust/extracted/pem, /etc/pki/tls/certs, /usr/share/ca-certificates/cacert.org, /etc/ansible. The exception msg was: [Errno 1] _ssl.c:504: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure."}
to retry, use: --limit @/root/playbooks/install-docker-centos.retry

@empireshades

This comment has been minimized.

Copy link

empireshades commented Jul 6, 2018

Thank you for this !!!

@pahla1

This comment has been minimized.

Copy link

pahla1 commented Aug 20, 2018

you can use with_items for multiple repeated tasks:


  • name: Install docker
    gather_facts: No
    hosts: default

    tasks:

    • name: Install required pkgs
      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/docer-ce.repo
      become: yes

    • name: Enable Docker Edge & Test repo
      ini_file:
      dest: /etc/yum.repos.d/docer-ce.repo
      section: "{{ item }}"
      option: enabled
      value: 0
      become: yes
      with_items: ['docker-ce-test', 'docker-ce-edge']

    • 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

@bollineni36

This comment has been minimized.

Copy link

bollineni36 commented Nov 23, 2018

when running this playbook getting an issue

fatal: FAILED! => {"changed": true, "msg": "Error: Package: 3:docker-ce-18.09.0-3.el7.x86_64 (docker-ce-stable)\n Requires: container-selinux >= 2.9\n", "rc": 1, "results": ["Loaded plugins: amazon-id, rhui-lb, search-disabled-repos\nResolving Dependencies\n--> Running transaction check\n---> Package docker-ce.x86_64 3:18.09.0-3.el7 will be installed\n--> Processing Dependency: container-selinux >= 2.9 for package: 3:docker-ce-18.09.0-3.el7.x86_64\n--> Processing Dependency: containerd.io for package: 3:docker-ce-18.09.0-3.el7.x86_64\n--> Processing Dependency: docker-ce-cli for package: 3:docker-ce-18.09.0-3.el7.x86_64\n--> Running transaction check\n---> Package containerd.io.x86_64 0:1.2.0-3.el7 will be installed\n---> Package docker-ce.x86_64 3:18.09.0-3.el7 will be installed\n--> Processing Dependency: container-selinux >= 2.9 for package: 3:docker-ce-18.09.0-3.el7.x86_64\n---> Package docker-ce-cli.x86_64 1:18.09.0-3.el7 will be installed\n--> Processing Dependency: libltdl.so.7()(64bit) for package: 1:docker-ce-cli-18.09.0-3.el7.x86_64\n--> Running transaction check\n---> Package docker-ce.x86_64 3:18.09.0-3.el7 will be installed\n--> Processing Dependency: container-selinux >= 2.9 for package: 3:docker-ce-18.09.0-3.el7.x86_64\n---> Package libtool-ltdl.x86_64 0:2.4.2-22.el7_3 will be installed\n--> Finished Dependency Resolution\n You could try using --skip-broken to work around the problem\n You could try running: rpm -Va --nofiles --nodigest\n"]}

@map1983

This comment has been minimized.

Copy link

map1983 commented Feb 2, 2019

I am also facing same problem

@technosteve

This comment has been minimized.

Copy link

technosteve commented May 6, 2019

you may need to uninstall any existing docker files from the centos repo first. try making this your first task.

- 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: removed
@SpikePy

This comment has been minimized.

Copy link

SpikePy commented Aug 2, 2019

in your destination a k is missing: dest: /etc/yum.repos.d/docker-ce.repo
I mean it's working but it does not look nice like that ;)

@tobiasehlert

This comment has been minimized.

Copy link

tobiasehlert commented Jan 27, 2020

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

@tongtie

This comment has been minimized.

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

This comment has been minimized.

Copy link

yogeshssawant commented Mar 24, 2020

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

dridi-mohamed commented Jul 2, 2020

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

@a19singh

This comment has been minimized.

Copy link

a19singh commented Jul 12, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.