HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Ansible to check version of software on remote hosts before install/upgrade

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
installhostsversionsoftwareremotebeforecheckansibleupgrade

Problem

Goal: Skip the host if version of custom software (non-rpm based) installed at remote host is greater than or equal to software version intended for upgrade.
For all intents and purposes, this playbook (underneath) works well.

Questions :

-
How to formulate the code to get rid of [DEPRECATION WARNING] during runtime?

-
Is there are better way to apply the same logic?

ansible --version
    ansible 2.7.10
    python version = 2.7.5


#Remote software version
cat /path/to/version_file.txt
FullVersion=12.52.105.2112


#Playbook Install/upgrades firmare on remote nodes
#
---

- hosts: "RedHat_7_nodes"
  remote_user: test_user
  connection: ssh
  gather_facts: '{{ gather }}'

# Next Section - Variables

  vars_prompt:

    - name: EXPECTED_REMOTE_SOFTWARE_VERSION
      prompt: What is expected version of Software? (If remote server is already at that level, this playbook will skip it)
      private: no

  tasks:

   - name: Check if the remote Software version is below "{{ EXPECTED_REMOTE_SOFTWARE_VERSION }}"
     shell: grep FullVersion /path/to/version_file.txt  | awk -F = '{print $2}'
     check_mode: no
     register: CURRENT_INSTALLED_VERSION

   - debug:
       msg: "{{ CURRENT_INSTALLED_VERSION }}"
   - meta: end_play
     when: CURRENT_INSTALLED_VERSION.stdout | version(EXPECTED_REMOTE_SOFTWARE_VERSION,'>=')

   - name: Run the binary and upgrade since the installed version is lower than intended software version
     shell: echo "Run the binary and upgrade since the installed version is lower than intended software version "
     check_mode: no


```
# Runtime 1. Expected version is lower than installed version. Skip this host. No upgrade.

ansible-playbook WebAgent_Install.yml -i inventory.txt --extra-vars "gather=yes"

What is expected version of Software? (If remote server is already at that level, this playbook will skip it): 12.52.105.2111

PLAY [RedHat_7_nodes] ****

Solution

How to formulate the code to get rid of [DEPRECATION WARNING] during runtime?

Simply by following the tip in the error message.

when: CURRENT_INSTALLED_VERSION.stdout is version(EXPECTED_REMOTE_SOFTWARE_VERSION,'>=')



Is there are better way to apply the same logic?

I don't see anything wrong if it meets your current needs.

Only remark: The first task is not idempotent. It will report change anytime it runs (because shell is supposed by default to change the remote system). Since your command is a simple grep that does not change anything, your can have the task report always ok by adding the option:

changed_when: false

Code Snippets

when: CURRENT_INSTALLED_VERSION.stdout is version(EXPECTED_REMOTE_SOFTWARE_VERSION,'>=')
changed_when: false

Context

StackExchange DevOps Q#8150, answer score: 2

Revisions (0)

No revisions yet.