---
  - name: ensure old versions of Docker are not present
    yum:
      name: "{{ item }}"
      state: absent
    with_items:
      - docker
      - docker-common
      - docker-engine

  - name: install required packages
    yum:
      name: "{{ item }}"
    with_items:
      - yum-utils
      - device-mapper-persistent-data
      - lvm2

  - name: add Docker repository
    get_url:
      url: https://download.docker.com/linux/centos/docker-ce.repo
      dest: /etc/yum.repos.d/docker-ce.repo
      owner: root
      group: root
      mode: 0644

  - name: install yum-versionlock
    yum:
      name: yum-versionlock

  - name: remove existing Docker CE versionlocks
    command: yum versionlock delete {{ docker_ce_pkg_name }}
    args:
      warn: false
    register: versionlock_delete
    failed_when: "versionlock_delete.rc != 0 and 'no matches' not in versionlock_delete.stderr"

  - name: add Docker CE versionlock
    command: yum versionlock add {{ docker_ce_pkg_name }}-{{ centos_docker_ce_version }}
    args:
      warn: false

  - name: install Docker CE
    yum:
      name: "{{ docker_ce_pkg_name }}-{{ centos_docker_ce_version }}"

---
  - name: ensure old versions of Docker are not present
    yum:
      name: "{{ item }}"
      state: absent
    with_items:
      - docker
      - docker-common
      - docker-engine

  - name: install required packages
    yum:
      name: "{{ item }}"
    with_items:
      - yum-utils
      - device-mapper-persistent-data
      - lvm2

  - name: add Docker repository
    get_url:
      url: https://download.docker.com/linux/centos/docker-ce.repo
      dest: /etc/yum.repos.d/docker-ce.repo
      owner: root
      group: root
      mode: 0644

  - name: install yum-versionlock
    yum:
      name: yum-versionlock

  - name: Get Docker CE versions
    yum:
      list: docker-ce
    register: docker_versions

  - name: Get installed Docker CE versions
    set_fact:
      installed_docker_versions: "{{ docker_versions | json_query(query) }}"
    vars:
      query: "results[?yumstate=='installed'].version"

  - name: remove existing Docker CE versionlocks
    command: yum versionlock delete {{ docker_ce_pkg_name }}
    args:
      warn: false
    when: centos_docker_ce_version not in installed_docker_versions
    register: versionlock_delete
    failed_when: "versionlock_delete.rc != 0 and 'no matches' not in versionlock_delete.stderr"

  - name: add Docker CE versionlock
    command: yum versionlock add {{ docker_ce_pkg_name }}-{{ centos_docker_ce_version }}
    args:
      warn: false
    when: centos_docker_ce_version not in installed_docker_versions

  - name: install Docker CE
    yum:
      name: "{{ docker_ce_pkg_name }}-{{ centos_docker_ce_version }}"
      allow_downgrade: yes
    when: centos_docker_ce_version not in installed_docker_versions

Check docker-ce version before changing versionlocks
Check docker-ce version before changing versionlocks

Previously, yum versionlocks for docker-ce would be removed and added
every time the Docker role was run. ansible-lint would raise a warning
since the command module was used without any conditions. To fix this,
the docker-ce version will be checked and versionlocks and installation
will only occur if a different version is currently installed.
