<blockquote>
  <p>How to formulate the code to get rid of [DEPRECATION WARNING] during runtime? </p>
</blockquote>

<p>Simply by following the tip in the error message.</p>

<pre><code>when: CURRENT_INSTALLED_VERSION.stdout is version(EXPECTED_REMOTE_SOFTWARE_VERSION,'&gt;=')
</code></pre>

<blockquote>
  <p>Is there are better way to apply the same logic?</p>
</blockquote>

<p>I don't see anything wrong if it meets your current needs.</p>

<p>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:</p>

<pre><code>changed_when: false
</code></pre>

CC BY-SA 4.0
2019-05-17T21:48:37.373Z
Indeed this has worked. +1. 
On version_compare vs version usage - Isn't it other way round - `In ansible 2.5 version_compare was renamed to version` as stated here [link](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html)
Raman Kathpalia
CC BY-SA 4.0
2019-05-17T21:49:58.683Z
I don't have enough reputation here on devops to cast vote. Invisible thumbs up from me!
Raman Kathpalia
CC BY-SA 4.0
2019-05-18T12:58:15.573Z
You are absolutely right for the `version` filter. I described deprecation the other way around looking at a too old personnal script as an example when answering. Edited.
Zeitounator
CC BY-SA 4.0
2019-05-17T16:48:54.870Z
2019-05-18T12:58:52.370Z
2019-05-18T12:58:52.370Z
Zeitounator
<p>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. </p>

<p>Questions :</p>

<ol>
<li><p>How to formulate the code to get rid of [DEPRECATION WARNING] during runtime? </p></li>
<li><p>Is there are better way to apply the same logic?</p></li>
</ol>

<pre><code>   ansible --version
    ansible 2.7.10
    python version = 2.7.5 
</code></pre>

<pre><code>#Remote software version
cat /path/to/version_file.txt
FullVersion=12.52.105.2112
</code></pre>

<pre><code>#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,'&gt;=')

   - 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
</code></pre>

<pre><code># 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] ************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [joker7.cartoon.biz]

TASK [Check if the remote Software version is below "12.52.105.2111"] ************************************************************************************************************************
changed: [joker7.cartoon.biz]

TASK [debug] *********************************************************************************************************************************************************************************
ok: [joker7.cartoon.biz] =&gt; {
    "msg": {
        "changed": true,
        "cmd": "grep FullVersion /path/to/version_file.txt | awk -F = '{print $2}'",
        "delta": "0:00:00.013802",
        "end": "2019-05-16 14:37:00.635365",
        "failed": false,
        "rc": 0,
        "start": "2019-05-16 14:37:00.621563",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "12.52.105.2112",
        "stdout_lines": [
            "12.52.105.2112"
        ]
    }
}
[DEPRECATION WARNING]: Using tests as filters is deprecated. Instead of using `result|version` use `result is version`. This feature will be removed in version 2.9. Deprecation warnings can
 be disabled by setting deprecation_warnings=False in ansible.cfg.

PLAY RECAP ***********************************************************************************************************************************************************************************
joker7.cartoon.biz         : ok=3    changed=1    unreachable=0    failed=0
</code></pre>

<pre><code># Runtime - 2. Expected version is higher than installed version. 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.2112]: 12.52.105.2113

PLAY [RedHat_7_nodes] ************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [joker7.cartoon.biz]

TASK [Check if the remote Software version is below "12.52.105.2113"] ************************************************************************************************************************
changed: [joker7.cartoon.biz]

TASK [debug] *********************************************************************************************************************************************************************************
ok: [joker7.cartoon.biz] =&gt; {
    "msg": {
        "changed": true,
        "cmd": "grep FullVersion /path/to/version_file.txt | awk -F = '{print $2}'",
        "delta": "0:00:00.013155",
        "end": "2019-05-16 14:35:49.364318",
        "failed": false,
        "rc": 0,
        "start": "2019-05-16 14:35:49.351163",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "12.52.105.2112",
        "stdout_lines": [
            "12.52.105.2112"
        ]
    }
}
[DEPRECATION WARNING]: Using tests as filters is deprecated. Instead of using `result|version` use `result is version`. This feature will be removed in version 2.9. Deprecation warnings can
 be disabled by setting deprecation_warnings=False in ansible.cfg.

TASK [Run the binary and upgrade since the installed version is lower than intended software version] ****************************************************************************************
changed: [joker7.cartoon.biz]

PLAY RECAP ***********************************************************************************************************************************************************************************
joker7.cartoon.biz         : ok=4    changed=2    unreachable=0    failed=0

</code></pre>

<ansible>
Ansible to check version of software on remote hosts before install/upgrade
Raman Kathpalia
# Ansible to check version of software on remote hosts before install/upgrade

(Asked by: username_0 on 2019-05-16)

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] ************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [joker7.cartoon.biz]

TASK [Check if the remote Software version is below "12.52.105.2111"] ************************************************************************************************************************
changed: [joker7.cartoon.biz]

TASK [debug] *********************************************************************************************************************************************************************************
ok: [joker7.cartoon.biz] => {
    "msg": {
        "changed": true,
        "cmd": "grep FullVersion /path/to/version_file.txt | awk -F = '{print $2}'",
        "delta": "0:00:00.013802",
        "end": "2019-05-16 14:37:00.635365",
        "failed": false,
        "rc": 0,
        "start": "2019-05-16 14:37:00.621563",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "12.52.105.2112",
        "stdout_lines": [
            "12.52.105.2112"
        ]
    }
}
[DEPRECATION WARNING]: Using tests as filters is deprecated. Instead of using `result|version` use `result is version`. This feature will be removed in version 2.9. Deprecation warnings can
 be disabled by setting deprecation_warnings=False in ansible.cfg.

PLAY RECAP ***********************************************************************************************************************************************************************************
joker7.cartoon.biz         : ok=3    changed=1    unreachable=0    failed=0

# Runtime - 2. Expected version is higher than installed version. 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.2112]: 12.52.105.2113

PLAY [RedHat_7_nodes] ************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [joker7.cartoon.biz]

TASK [Check if the remote Software version is below "12.52.105.2113"] ************************************************************************************************************************
changed: [joker7.cartoon.biz]

TASK [debug] *********************************************************************************************************************************************************************************
ok: [joker7.cartoon.biz] => {
    "msg": {
        "changed": true,
        "cmd": "grep FullVersion /path/to/version_file.txt | awk -F = '{print $2}'",
        "delta": "0:00:00.013155",
        "end": "2019-05-16 14:35:49.364318",
        "failed": false,
        "rc": 0,
        "start": "2019-05-16 14:35:49.351163",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "12.52.105.2112",
        "stdout_lines": [
            "12.52.105.2112"
        ]
    }
}
[DEPRECATION WARNING]: Using tests as filters is deprecated. Instead of using `result|version` use `result is version`. This feature will be removed in version 2.9. Deprecation warnings can
 be disabled by setting deprecation_warnings=False in ansible.cfg.

TASK [Run the binary and upgrade since the installed version is lower than intended software version] ****************************************************************************************
changed: [joker7.cartoon.biz]

PLAY RECAP ***********************************************************************************************************************************************************************************
joker7.cartoon.biz         : ok=4    changed=2    unreachable=0    failed=0

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2019-05-17. Score: 2 | Accepted answer

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

> **Comments:**
> **username_0**: Indeed this has worked. +1.  On version_compare vs version usage - Isn't it other way round - `In ansible 2.5 version_compare was renamed to version` as stated here [link](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html)  
> **username_0**: I don't have enough reputation here on devops to cast vote. Invisible thumbs up from me!  
> **username_1**: You are absolutely right for the `version` filter. I described deprecation the other way around looking at a too old personnal script as an example when answering. Edited.  

---

