How to sort version numbers in Ansible
I'm building an Ansible playbook in which I want to make a backup of a database in case I need to upgrade the software. For this I want to compare the highest version number that is available to the version number that is installed. In case the latest version is hight than the installed version I'll back up the database.
The problem however is that I cannot find a good way to sort version numbers in Ansible. The standard sort filter sorts on strings instead of numbers/versions.
This is what I'm doing right now:
- name: Get package version
yum:
list: package
register: software_version
- name: Read which version is installed and available
set_fact:
software_version_installed: "{{ software_version | json_query("results[?yumstate=='installed'].version") | sort | last }}"
software_version_available: "{{ software_version | json_query("results[?yumstate=='available'].version") | sort | last }}"
- name: Backup old database file on remote host
copy:
src: "{{ software.database_path }}"
dest: "{{ software.database_path }}_{{ ansible_date_time.date }}_v{{ software_version_installed }}"
remote_src: yes
when: software_version_installed is version(software_version_available, "<")
The playbook above works, as long as version numbers stay underneath the number 10 (e.g. 1.2.3, but not 1.10.1) since sorting is performed like a string. When the version number has to sort e.g. 1.2.3 and 1.10.1, it will take 1.2.3 as latest version.
To show the issue:
- name: Read which version is installed and available
set_fact:
software_versions: [ "2.5.0", "2.9.0", "2.10.0", "2.11.0" ]
- name: Debug
debug:
var: software_versions | sort
TASK [grafana : Debug]**********************************
ok: [localhost] => {
"software_versions | sort": [
"2.10.0",
"2.11.0",
"2.5.0",
"2.9.0"
]
}
Does anyone know a good way to sort version numbers in Ansible?
sorting ansible jinja2
add a comment |
I'm building an Ansible playbook in which I want to make a backup of a database in case I need to upgrade the software. For this I want to compare the highest version number that is available to the version number that is installed. In case the latest version is hight than the installed version I'll back up the database.
The problem however is that I cannot find a good way to sort version numbers in Ansible. The standard sort filter sorts on strings instead of numbers/versions.
This is what I'm doing right now:
- name: Get package version
yum:
list: package
register: software_version
- name: Read which version is installed and available
set_fact:
software_version_installed: "{{ software_version | json_query("results[?yumstate=='installed'].version") | sort | last }}"
software_version_available: "{{ software_version | json_query("results[?yumstate=='available'].version") | sort | last }}"
- name: Backup old database file on remote host
copy:
src: "{{ software.database_path }}"
dest: "{{ software.database_path }}_{{ ansible_date_time.date }}_v{{ software_version_installed }}"
remote_src: yes
when: software_version_installed is version(software_version_available, "<")
The playbook above works, as long as version numbers stay underneath the number 10 (e.g. 1.2.3, but not 1.10.1) since sorting is performed like a string. When the version number has to sort e.g. 1.2.3 and 1.10.1, it will take 1.2.3 as latest version.
To show the issue:
- name: Read which version is installed and available
set_fact:
software_versions: [ "2.5.0", "2.9.0", "2.10.0", "2.11.0" ]
- name: Debug
debug:
var: software_versions | sort
TASK [grafana : Debug]**********************************
ok: [localhost] => {
"software_versions | sort": [
"2.10.0",
"2.11.0",
"2.5.0",
"2.9.0"
]
}
Does anyone know a good way to sort version numbers in Ansible?
sorting ansible jinja2
add a comment |
I'm building an Ansible playbook in which I want to make a backup of a database in case I need to upgrade the software. For this I want to compare the highest version number that is available to the version number that is installed. In case the latest version is hight than the installed version I'll back up the database.
The problem however is that I cannot find a good way to sort version numbers in Ansible. The standard sort filter sorts on strings instead of numbers/versions.
This is what I'm doing right now:
- name: Get package version
yum:
list: package
register: software_version
- name: Read which version is installed and available
set_fact:
software_version_installed: "{{ software_version | json_query("results[?yumstate=='installed'].version") | sort | last }}"
software_version_available: "{{ software_version | json_query("results[?yumstate=='available'].version") | sort | last }}"
- name: Backup old database file on remote host
copy:
src: "{{ software.database_path }}"
dest: "{{ software.database_path }}_{{ ansible_date_time.date }}_v{{ software_version_installed }}"
remote_src: yes
when: software_version_installed is version(software_version_available, "<")
The playbook above works, as long as version numbers stay underneath the number 10 (e.g. 1.2.3, but not 1.10.1) since sorting is performed like a string. When the version number has to sort e.g. 1.2.3 and 1.10.1, it will take 1.2.3 as latest version.
To show the issue:
- name: Read which version is installed and available
set_fact:
software_versions: [ "2.5.0", "2.9.0", "2.10.0", "2.11.0" ]
- name: Debug
debug:
var: software_versions | sort
TASK [grafana : Debug]**********************************
ok: [localhost] => {
"software_versions | sort": [
"2.10.0",
"2.11.0",
"2.5.0",
"2.9.0"
]
}
Does anyone know a good way to sort version numbers in Ansible?
sorting ansible jinja2
I'm building an Ansible playbook in which I want to make a backup of a database in case I need to upgrade the software. For this I want to compare the highest version number that is available to the version number that is installed. In case the latest version is hight than the installed version I'll back up the database.
The problem however is that I cannot find a good way to sort version numbers in Ansible. The standard sort filter sorts on strings instead of numbers/versions.
This is what I'm doing right now:
- name: Get package version
yum:
list: package
register: software_version
- name: Read which version is installed and available
set_fact:
software_version_installed: "{{ software_version | json_query("results[?yumstate=='installed'].version") | sort | last }}"
software_version_available: "{{ software_version | json_query("results[?yumstate=='available'].version") | sort | last }}"
- name: Backup old database file on remote host
copy:
src: "{{ software.database_path }}"
dest: "{{ software.database_path }}_{{ ansible_date_time.date }}_v{{ software_version_installed }}"
remote_src: yes
when: software_version_installed is version(software_version_available, "<")
The playbook above works, as long as version numbers stay underneath the number 10 (e.g. 1.2.3, but not 1.10.1) since sorting is performed like a string. When the version number has to sort e.g. 1.2.3 and 1.10.1, it will take 1.2.3 as latest version.
To show the issue:
- name: Read which version is installed and available
set_fact:
software_versions: [ "2.5.0", "2.9.0", "2.10.0", "2.11.0" ]
- name: Debug
debug:
var: software_versions | sort
TASK [grafana : Debug]**********************************
ok: [localhost] => {
"software_versions | sort": [
"2.10.0",
"2.11.0",
"2.5.0",
"2.9.0"
]
}
Does anyone know a good way to sort version numbers in Ansible?
sorting ansible jinja2
sorting ansible jinja2
edited Jan 4 at 8:35
ReDNaX
asked Jan 3 at 16:06
ReDNaXReDNaX
11
11
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I can't confirm your experience that:
sorting is performed like a string.
See the example below
vars:
version_installed: "1.10.1"
versions:
- "1.1.3"
- "1.2.3"
- "1.7.5"
- "1.10.7"
tasks:
- debug: msg="{{ item }} is newer than {{ version_installed }}"
loop: "{{ versions }}"
when: item is version(version_installed, '>')
> ansible-playbook test-versions.yml | grep msg
"msg": "1.10.7 is newer than 1.10.1"
1
This is something else than I was looking for. The versions filter works perfectly, but sorting does not work that way. Sorting takes the variable as string.
– ReDNaX
Jan 4 at 8:30
@ReDNaX. How should Ansible recognize a list of "versions" versus list of "strings"?
– Vladimir Botka
Jan 4 at 9:01
add a comment |
It's now solved in another way. Instead of sorting the versions I compared the current version to all available versions.
- I've started by setting an update variable to false
- Next I compared the installed version to every available version
- If installed version > current version, set the update variable to true
The task performing the backup will only be performed when the update variable is true.
- name: Get package version
yum:
list: package
register: software_version
- name: Read which version is installed and available
set_fact:
software_update: false
software_version_installed: "{{ software_version | json_query("results[?yumstate=='installed'].version") | last }}"
software_version_available: "{{ software_version | json_query("results[?yumstate=='available'].version") }}"
- name: Check if upgrade is needed
set_fact:
software_update: true
when: software_version_installed is version(item, "<")
with_items: "{{ software_version_available }}"
- name: Backup old database file on remote host
copy:
src: "{{ software.database_path }}"
dest: "{{ software.database_path }}_{{ ansible_date_time.date }}_v{{ software_version_installed }}"
remote_src: yes
when: software_update
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025894%2fhow-to-sort-version-numbers-in-ansible%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I can't confirm your experience that:
sorting is performed like a string.
See the example below
vars:
version_installed: "1.10.1"
versions:
- "1.1.3"
- "1.2.3"
- "1.7.5"
- "1.10.7"
tasks:
- debug: msg="{{ item }} is newer than {{ version_installed }}"
loop: "{{ versions }}"
when: item is version(version_installed, '>')
> ansible-playbook test-versions.yml | grep msg
"msg": "1.10.7 is newer than 1.10.1"
1
This is something else than I was looking for. The versions filter works perfectly, but sorting does not work that way. Sorting takes the variable as string.
– ReDNaX
Jan 4 at 8:30
@ReDNaX. How should Ansible recognize a list of "versions" versus list of "strings"?
– Vladimir Botka
Jan 4 at 9:01
add a comment |
I can't confirm your experience that:
sorting is performed like a string.
See the example below
vars:
version_installed: "1.10.1"
versions:
- "1.1.3"
- "1.2.3"
- "1.7.5"
- "1.10.7"
tasks:
- debug: msg="{{ item }} is newer than {{ version_installed }}"
loop: "{{ versions }}"
when: item is version(version_installed, '>')
> ansible-playbook test-versions.yml | grep msg
"msg": "1.10.7 is newer than 1.10.1"
1
This is something else than I was looking for. The versions filter works perfectly, but sorting does not work that way. Sorting takes the variable as string.
– ReDNaX
Jan 4 at 8:30
@ReDNaX. How should Ansible recognize a list of "versions" versus list of "strings"?
– Vladimir Botka
Jan 4 at 9:01
add a comment |
I can't confirm your experience that:
sorting is performed like a string.
See the example below
vars:
version_installed: "1.10.1"
versions:
- "1.1.3"
- "1.2.3"
- "1.7.5"
- "1.10.7"
tasks:
- debug: msg="{{ item }} is newer than {{ version_installed }}"
loop: "{{ versions }}"
when: item is version(version_installed, '>')
> ansible-playbook test-versions.yml | grep msg
"msg": "1.10.7 is newer than 1.10.1"
I can't confirm your experience that:
sorting is performed like a string.
See the example below
vars:
version_installed: "1.10.1"
versions:
- "1.1.3"
- "1.2.3"
- "1.7.5"
- "1.10.7"
tasks:
- debug: msg="{{ item }} is newer than {{ version_installed }}"
loop: "{{ versions }}"
when: item is version(version_installed, '>')
> ansible-playbook test-versions.yml | grep msg
"msg": "1.10.7 is newer than 1.10.1"
edited Jan 3 at 18:21
answered Jan 3 at 17:13
Vladimir BotkaVladimir Botka
2,3682515
2,3682515
1
This is something else than I was looking for. The versions filter works perfectly, but sorting does not work that way. Sorting takes the variable as string.
– ReDNaX
Jan 4 at 8:30
@ReDNaX. How should Ansible recognize a list of "versions" versus list of "strings"?
– Vladimir Botka
Jan 4 at 9:01
add a comment |
1
This is something else than I was looking for. The versions filter works perfectly, but sorting does not work that way. Sorting takes the variable as string.
– ReDNaX
Jan 4 at 8:30
@ReDNaX. How should Ansible recognize a list of "versions" versus list of "strings"?
– Vladimir Botka
Jan 4 at 9:01
1
1
This is something else than I was looking for. The versions filter works perfectly, but sorting does not work that way. Sorting takes the variable as string.
– ReDNaX
Jan 4 at 8:30
This is something else than I was looking for. The versions filter works perfectly, but sorting does not work that way. Sorting takes the variable as string.
– ReDNaX
Jan 4 at 8:30
@ReDNaX. How should Ansible recognize a list of "versions" versus list of "strings"?
– Vladimir Botka
Jan 4 at 9:01
@ReDNaX. How should Ansible recognize a list of "versions" versus list of "strings"?
– Vladimir Botka
Jan 4 at 9:01
add a comment |
It's now solved in another way. Instead of sorting the versions I compared the current version to all available versions.
- I've started by setting an update variable to false
- Next I compared the installed version to every available version
- If installed version > current version, set the update variable to true
The task performing the backup will only be performed when the update variable is true.
- name: Get package version
yum:
list: package
register: software_version
- name: Read which version is installed and available
set_fact:
software_update: false
software_version_installed: "{{ software_version | json_query("results[?yumstate=='installed'].version") | last }}"
software_version_available: "{{ software_version | json_query("results[?yumstate=='available'].version") }}"
- name: Check if upgrade is needed
set_fact:
software_update: true
when: software_version_installed is version(item, "<")
with_items: "{{ software_version_available }}"
- name: Backup old database file on remote host
copy:
src: "{{ software.database_path }}"
dest: "{{ software.database_path }}_{{ ansible_date_time.date }}_v{{ software_version_installed }}"
remote_src: yes
when: software_update
add a comment |
It's now solved in another way. Instead of sorting the versions I compared the current version to all available versions.
- I've started by setting an update variable to false
- Next I compared the installed version to every available version
- If installed version > current version, set the update variable to true
The task performing the backup will only be performed when the update variable is true.
- name: Get package version
yum:
list: package
register: software_version
- name: Read which version is installed and available
set_fact:
software_update: false
software_version_installed: "{{ software_version | json_query("results[?yumstate=='installed'].version") | last }}"
software_version_available: "{{ software_version | json_query("results[?yumstate=='available'].version") }}"
- name: Check if upgrade is needed
set_fact:
software_update: true
when: software_version_installed is version(item, "<")
with_items: "{{ software_version_available }}"
- name: Backup old database file on remote host
copy:
src: "{{ software.database_path }}"
dest: "{{ software.database_path }}_{{ ansible_date_time.date }}_v{{ software_version_installed }}"
remote_src: yes
when: software_update
add a comment |
It's now solved in another way. Instead of sorting the versions I compared the current version to all available versions.
- I've started by setting an update variable to false
- Next I compared the installed version to every available version
- If installed version > current version, set the update variable to true
The task performing the backup will only be performed when the update variable is true.
- name: Get package version
yum:
list: package
register: software_version
- name: Read which version is installed and available
set_fact:
software_update: false
software_version_installed: "{{ software_version | json_query("results[?yumstate=='installed'].version") | last }}"
software_version_available: "{{ software_version | json_query("results[?yumstate=='available'].version") }}"
- name: Check if upgrade is needed
set_fact:
software_update: true
when: software_version_installed is version(item, "<")
with_items: "{{ software_version_available }}"
- name: Backup old database file on remote host
copy:
src: "{{ software.database_path }}"
dest: "{{ software.database_path }}_{{ ansible_date_time.date }}_v{{ software_version_installed }}"
remote_src: yes
when: software_update
It's now solved in another way. Instead of sorting the versions I compared the current version to all available versions.
- I've started by setting an update variable to false
- Next I compared the installed version to every available version
- If installed version > current version, set the update variable to true
The task performing the backup will only be performed when the update variable is true.
- name: Get package version
yum:
list: package
register: software_version
- name: Read which version is installed and available
set_fact:
software_update: false
software_version_installed: "{{ software_version | json_query("results[?yumstate=='installed'].version") | last }}"
software_version_available: "{{ software_version | json_query("results[?yumstate=='available'].version") }}"
- name: Check if upgrade is needed
set_fact:
software_update: true
when: software_version_installed is version(item, "<")
with_items: "{{ software_version_available }}"
- name: Backup old database file on remote host
copy:
src: "{{ software.database_path }}"
dest: "{{ software.database_path }}_{{ ansible_date_time.date }}_v{{ software_version_installed }}"
remote_src: yes
when: software_update
answered Jan 4 at 15:32
ReDNaXReDNaX
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025894%2fhow-to-sort-version-numbers-in-ansible%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown