# Asserting the absence of a previous software version

**URL:** <https://forum.ansible.com/t/asserting-the-absence-of-a-previous-software-version/13880>\
**Category:** Ansible Project\
**Created:** [November 10, 2013, 8:59pm UTC](https://forum.ansible.com/t/asserting-the-absence-of-a-previous-software-version/13880 "2013-11-10T20:59:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [November 10, 2013, 8:59pm UTC](https://forum.ansible.com/t/asserting-the-absence-of-a-previous-software-version/13880/1 "2013-11-10T20:59:44Z")

</div>

Hi,

If have the version number of a custom piece of software (configured  
by ansible) that is either part of the name of the executable or part  
of the it's path, is there any way to tell ansible to perform an  
upgrade and remove any previous versions?

So for example, if I have version 114 of my foo app that I install thusly:

/opt/foo-v114/bin/foo

If I want to roll out v115, I guess the idempotentcy will make v115  
available, is there any way to assert the absence of any prior version  
to v115?

Or is this something that a distro package should handle?

Cheers,

Ben

---

<div class="post-metadata">

**Author:** ![Timothy\_Gerla](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@Timothy\_Gerla](https://forum.ansible.com/u/Timothy_Gerla)\
**Post date:** [November 10, 2013, 9:17pm UTC](https://forum.ansible.com/t/asserting-the-absence-of-a-previous-software-version/13880/2 "2013-11-10T21:17:31Z")

</div>

I don’t have a full solution for your particular problem, but I would definitely look into the “stat” module to determine existence of previous versions, and then register the results of that task into a variable that you can then perform tests upon, and alter playbook behavior accordingly.

Distro packages would be ideal, though, it’s much easier just to rely on their well-tested behavior than to write your own, I would suppose!

-Tim

---

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [November 11, 2013, 8:34am UTC](https://forum.ansible.com/t/asserting-the-absence-of-a-previous-software-version/13880/3 "2013-11-11T08:34:43Z")

</div>

Hey Tim,

Thanks for the tip. I guess inadvertently you're making the point that  
what I'm looking for may be beyond the scope of ansible and might be  
better suited to a distro package.

Thanks for the feedback,

Cheers,

Ben

---

<div class="post-metadata">

**Author:** ![tannerjc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tannerjc/32/985_2.png) [@tannerjc](https://forum.ansible.com/u/tannerjc)\
**Post date:** [November 11, 2013, 2:31pm UTC](https://forum.ansible.com/t/asserting-the-absence-of-a-previous-software-version/13880/4 "2013-11-11T14:31:37Z")

</div>

You didn't specify which package manager you were using, but in the case of yum, you can handle this with "--obsoletes". As long as your v115 has the "obsoletes v114" metadata, you can use yum to perform the upgrade.

[http://www.centos.org/docs/5/html/5.1/Deployment\_Guide/s1-yum-useful-commands.html](http://www.centos.org/docs/5/html/5.1/Deployment_Guide/s1-yum-useful-commands.html)

"If the |--obsoletes| option is used (i.e. |yum --obsoletes /|\<package name/s\>|/|, |yum| will process obsolete packages. As such, packages that are obsoleted accross updates will be removed and replaced accordingly. "

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [November 12, 2013, 12:26am UTC](https://forum.ansible.com/t/asserting-the-absence-of-a-previous-software-version/13880/5 "2013-11-12T00:26:07Z")

</div>

So you want something like:

if software is not installed  
&nbsp;&nbsp;&nbsp;install new software  
else installed version is older than new version  
&nbsp;&nbsp;&nbsp;remove old software  
&nbsp;&nbsp;&nbsp;install new software  
&nbsp;&nbsp;&nbsp;(perhaps reconfigure and restart services)  
fi

As Tim suggests, this is better handled by distribution packaging, but  
if you have some custom setup, this may not be possible.

On the surface it looks like something that could be achieved with one  
tasks to determine the current state and register the result, follow  
by 2 tasks to conditionally add/remove the software.

The tricky bit is determining which package is newer. If your version  
numbers are simple integers you may be able to get away with something  
like

- name: determine newest installed software version  
&nbsp;&nbsp;shell: find /opt/ -n 'foo-v\*' | sed 's/\/opt\/foo-v//' | sort -rn | head -1  
&nbsp;&nbsp;register: installed

- name: remove old software versions  
&nbsp;&nbsp;when: installed.stdout != '' and installed.stdout|int \< new\_version  
&nbsp;&nbsp;command: to remove old software

- name: install new software version  
&nbsp;&nbsp;when: installed.stdout == '' or installed.stdout|int \< new\_version  
&nbsp;&nbsp;command: to install new software

where 'new\_version' is a variable you have set somewhere.

The above code is untested but might form the basis for something that  
works for you.

K

Kahlil (Kal) Hodgson GPG: C9A02289  
Head of Technology (m) +61 (0) 4 2573 0382  
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415  
401 Docklands Drive  
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that  
the parts you are reassembling were disassembled by you. Therefore,  
if you can't get them together again, there must be a reason. By all  
means, do not use a hammer." -- IBM maintenance manual, 1925

---

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [November 12, 2013, 9:26am UTC](https://forum.ansible.com/t/asserting-the-absence-of-a-previous-software-version/13880/6 "2013-11-12T09:26:40Z")

</div>

In this case, I'm not actually using a package manager, since this is  
just a custom daemon process we have built. But we're running on arch,  
so if we did package this up, we'd be using pacman.

But thanks for the tip about yum --obsoletes, since I'd never heard of  
that before.

---

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [November 12, 2013, 9:28am UTC](https://forum.ansible.com/t/asserting-the-absence-of-a-previous-software-version/13880/7 "2013-11-12T09:28:44Z")

</div>

Hey Kal,

That was pretty much the semantics I was looking for, so I'll give you  
suggestion a whirl and weigh it up against the cost of producing a  
distro package.

Thanks for your help,

Ben
