There is a probably a better way to do this, but as of now, I am making this work, but having issues. I am attempting to do the following:
- Gather package facts using
package_facts
and register the packages - I debug the registered variable and get the package I want, converting the version to an int
- I would then dynmaically set variable of the later package and also pull the version and convert to an int
- I attempt to upgrade the package with the set variable of the latest version of the package and only apply it if the set_fact variable's int version is greater than the existing package installed
Here is the playbook:
---
- name: Attempt upgrade package
hosts: all
gather_facts: false
tasks:
- name: Get Splunk package
package_facts:
manager: auto
register: package_info
- name: Get SplunkForwarder Package Info
debug:
var: package_info.ansible_facts.packages.splunkforwarder[0].version
register: splunk_current_version
- name: Include variables for new splunk forwarder
set_fact:
splunk_latest_version: "{{ splunkforwarder_latest.split('-')[1] }}"
vars:
splunkforwarder_latest: "splunkforwarder-9.1.1-82c987350fde-linux-2.6-x86_64.rpm"
when: splunk_current_version is defined
- name: Debug latest
debug:
var: splunk_latest_version
- name: Upgrade when latest package is detected
yum:
name: "splunkforwarder-{{ splunk_latest_version }}-linux-2.6-x86_64.rpm"
state: latest
when: splunk_current_version is defined and splunk_current_version is version(splunk_latest_version, '<')
Here is the error message I get, stating I am comparing two dicts, which is probably true. I am still fairly new at this.
fatal: [server1]: FAILED! => {"msg": "The conditional check 'splunk_current_version > splunk_current_version' failed. The error was: Unexpected templating type error occurred on ({% if splunk_current_version > splunk_current_version %} True {% else %} False {% endif %}): '>' not supported between instances of 'dict' and 'dict'\n\nThe error appears to be in '/home/admin/python-env/ansible/playbooks/uuid.yml': line 33, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Upgrade when latest package is detected.\n ^ here\n"}
I can probably simply just past the later version of the package to a variable, by including the variable or set and just yum update with the latest package. I am probably overcomplicating this, but I would appreciate any help or advice.
Debug output below:
TASK [Get Splunk package] ******************************************************************************************************************************************************************************************
ok: [server1]
[WARNING]: Platform linux on host server1 is using the discovered Python interpreter at /usr/bin/python3.6, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [server1]
TASK [Get SplunkForwarder Package Info] ****************************************************************************************************************************************************************************
ok: [server1] => {
"package_info.ansible_facts.packages.splunkforwarder[0].version": "9.0.1"
}
ok: [server1] => {
"package_info.ansible_facts.packages.splunkforwarder[0].version": "9.0.1"
}
TASK [Include variables for new splunk forwarder] ******************************************************************************************************************************************************************
ok: [server1]
ok: [server1]
TASK [Debug latest] ************************************************************************************************************************************************************************************************
ok: [server1] => {
"splunk_latest_version": "9.1.1"
}
ok: [server1] => {
"splunk_latest_version": "9.1.1"
}
TASK [Upgrade when latest package is detected] *********************************************************************************************************************************************************************
fatal: [server1]: FAILED! => {"msg": "The conditional check 'splunk_current_version is defined and splunk_current_version is version(splunk_latest_version, '<')' failed. The error was: Version comparison: '<' not supported between instances of 'str' and 'int'\n\nThe error appears to be in '/home/admin/python-env/ansible/playbooks/uuid.yml': line 36, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Upgrade when latest package is detected\n ^ here\n"}
fatal: [server1]: FAILED! => {"msg": "The conditional check 'splunk_current_version is defined and splunk_current_version is version(splunk_latest_version, '<')' failed. The error was: Version comparison: '<' not supported between instances of 'str' and 'int'\n\nThe error appears to be in '/home/admin/python-env/ansible/playbooks/uuid.yml': line 36, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Upgrade when latest package is detected\n ^ here\n"}
PLAY RECAP *********************************************************************************************************************************************************************************************************