letthefireflieslive
letthefireflieslive

Reputation: 12664

Include task when compared version is lower

I'd like to install docker when version is lower than 19.03.2.

---
- include_tasks: get_version.yml

- include_tasks: install.yml
  when: (docker_version | int) is version('19.03.2', '<=')

get_version.yml

---
- name: Get docker version
  shell: docker --version
  register: results

- name: Extract docker version
  set_fact:
    docker_version: "{{ results.stdout | regex_search('^.*version\\s([0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}),.+$','\\1') }}"

- name: Show docker version
  debug: var=docker_version

What's the right condition? the current code always include install.yml. The current docker_version i am getting is 1.13.1

Upvotes: 1

Views: 77

Answers (2)

Vladimir Botka
Vladimir Botka

Reputation: 68004

For example the tasks below

  tasks:
    - command: docker --version
      register: result
    - set_fact:
        docker_version: "{{ result.stdout.split(' ')[2].split('-')[0] }}"
    - debug:
        var: docker_version
    - debug:
        msg: Install docker
      when: docker_version is version('19.03.2', '<=')

give

"docker_version": "18.06.1"

"msg": "Install docker"

Upvotes: 1

Smily
Smily

Reputation: 2568

Can you try without converting to int?

- include_tasks: install.yml
  when: docker_version is version('19.03.2', '<=')

Also in your code the compared version is lower, so I guess the install.yml playbook should be called.

Upvotes: 0

Related Questions