Skip to content

Instantly share code, notes, and snippets.

@elreydetoda
Created August 30, 2020 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elreydetoda/065ed953486e3fbf91e2f85f4e821028 to your computer and use it in GitHub Desktop.
Save elreydetoda/065ed953486e3fbf91e2f85f4e821028 to your computer and use it in GitHub Desktop.
an ansible playbook that does semantic version sorting, more here: https://github.com/diodonfrost/ansible-role-vagrant/pull/1
---
- hosts: localhost
tasks:
- name: Linux | Find all versions of Vagrant
uri:
url: https://releases.hashicorp.com/vagrant/index.json
return_content: yes
register: vagrant_index
# this was the original code from here: https://github.com/diodonfrost/ansible-role-vagrant/blob/master/tasks/setup-Linux.yml#L14
# this code doesn't get the most recent version of vagrant
# - name: Linux | Finds the latest version of Vagrant when latest var is define
# set_fact:
# vagrant_version_to_install: "{{ item }}"
# with_items: "{{ (vagrant_index.content | from_json).versions | list | sort | last }}"
#
# - debug:
# var: vagrant_version_to_install
- shell:
cmd: |
# the set command comes from this: https://blog.elreydetoda.site/cool-shell-tricks/#bashscriptingmodifiedscripthardening
set -${-//[sc]/}eu${DEBUG+xv}o pipefail
# this assigns everything from stdin to an array value
# got it from here: https://github.com/koalaman/shellcheck/wiki/SC2207
IFS=" " read -r -a arrayzz
# then this line reads in that assigned array and sorts the values in the array
# with the latest value being at the top. So, then at the end you print out
# the first value in the array. which should be the latest release.
# got this from here: https://stackoverflow.com/questions/7442417/how-to-sort-an-array-in-bash#answer-11789688
IFS=$'\n' sorted=($( sort -t '.' -k1,1nr -k2,2nr -k3,3nr <<<"${arrayzz[*]}" ))
# then you just print out that version so it get put in the register
printf '%s' "${sorted[0]}"
args:
stdin: '{{ (vagrant_index.content | from_json).versions | list | join(" ") }}'
executable: /bin/bash
register: shell_output
changed_when: False
- debug:
var: shell_output
- debug:
msg: '{{ shell_output.stdout }}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment