# How to do math in Ansible?

**URL:** <https://forum.ansible.com/t/how-to-do-math-in-ansible/26531>\
**Category:** Ansible Project\
**Created:** [January 22, 2018, 5:18pm UTC](https://forum.ansible.com/t/how-to-do-math-in-ansible/26531 "2018-01-22T17:18:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [January 22, 2018, 5:18pm UTC](https://forum.ansible.com/t/how-to-do-math-in-ansible/26531/1 "2018-01-22T17:18:34Z")

</div>

I have an Ansible task that aims to upgrade our Gitlab application. I want the user to ONLY be able to upgrade 1 major release. For example, I want to enable a 8.x to 9.x upgrade, but not an 8.x to 10.x upgrade. I’m aware of the version\_compare module, which I use, but need more than what it does.

I did write a filter that extracts the major, minor, patch versions of a Semver-format version so I can get the major part. But how do I then compare them to see if their difference is \> 1?

I do…

`

# NOTE: server\_major is a filter I wrote to extract the major version of an M.m.p Semver-format version.

- name: Get major version of installed version  
set\_fact: M\_installed=installed\_version|sermver\_major
- name: Get the major version of the desired upgrade version  
set\_fact: M\_upgrade=upgrade\_version|semver\_major
- name: Ensure M\_upgrade - M\_installed = 1  
“How do I do this?”  
`

Maybe there is a more straight-forward way to do this?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 22, 2018, 5:30pm UTC](https://forum.ansible.com/t/how-to-do-math-in-ansible/26531/2 "2018-01-22T17:30:08Z")

</div>

Probably something like:

- name: Ensure M\_upgrade - M\_installed = 1

assert:  
that:

- M\_upgrade|int - M\_installed|int == 1

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 22, 2018, 5:32pm UTC](https://forum.ansible.com/t/how-to-do-math-in-ansible/26531/3 "2018-01-22T17:32:47Z")

</div>

If you are using apt I would suggest another way of solving this and that is pinning in apt

Just create a file /etc/apt/preferences.d/gitlab with the following content

Package: gitlab-ce  
Pin: version /9.[0-9]+\.[0-9]+-/  
Pin-Priority: 1000

You can read more about pinning here  
[https://wiki.debian.org/AptPreferences](https://wiki.debian.org/AptPreferences)

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [January 22, 2018, 6:23pm UTC](https://forum.ansible.com/t/how-to-do-math-in-ansible/26531/4 "2018-01-22T18:23:29Z")

</div>

I’m using yum (RHEL). However with this approach, don’t you have to update the file when the major version changes?

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [January 22, 2018, 6:33pm UTC](https://forum.ansible.com/t/how-to-do-math-in-ansible/26531/5 "2018-01-22T18:33:12Z")

</div>

Thanks, I’l try this

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 23, 2018, 2:54pm UTC](https://forum.ansible.com/t/how-to-do-math-in-ansible/26531/6 "2018-01-23T14:54:50Z")

</div>

With that regex yes, it's a nice way to control upgrade to major versions.  
And it also preventing upgrade of a package if someone should run apt dist-upgrade (equivalent to yum upgrade)

My file for Gitlab looks like this

$ more /etc/apt/preferences.d/gitlab  
Package: gitlab-ce  
Pin: version /[0-9]+\.[0-9]+\.([5-9]-|[0-9]{2,}-)/  
Pin-Priority: 1000

It only upgrade if the package is x.y.5 or higher, the reason for this is Gitlab tends to be very buggy until around x.y.5 version is released.
