# How to increament a value in file on every run

**URL:** <https://forum.ansible.com/t/how-to-increament-a-value-in-file-on-every-run/26202>\
**Category:** Ansible Project\
**Created:** [December 5, 2017, 6:35am UTC](https://forum.ansible.com/t/how-to-increament-a-value-in-file-on-every-run/26202 "2017-12-05T06:35:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sudip\_Dutta](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@Sudip\_Dutta](https://forum.ansible.com/u/Sudip_Dutta)\
**Post date:** [December 5, 2017, 6:35am UTC](https://forum.ansible.com/t/how-to-increament-a-value-in-file-on-every-run/26202/1 "2017-12-05T06:35:08Z")

</div>

Hi all,

The scenario is -

I have a file in which I have below content

BUILD\_VERSION = 4

on every run, I want to increase BUILD\_VERSION counter to 1, so that it will be -

BUILD\_VERSION = 5 (on next run)

I am trying different things but stuck. How can this be done ?

---

<div class="post-metadata">

**Author:** ![dch](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dch/32/7132_2.png) [@dch](https://forum.ansible.com/u/dch)\
**Post date:** [December 5, 2017, 8:11am UTC](https://forum.ansible.com/t/how-to-increament-a-value-in-file-on-every-run/26202/2 "2017-12-05T08:11:25Z")

</div>

Hi Sudip,

Please show us what you've tried and gotten stuck with -- there are  
plenty of solutions already if you google for "ansible increment  
integer".

One thing to be aware of, is that you will need to have a central  
"store" of truth for your integer. Ansible is both stateless and able to  
be run from any computer. Your BUILD\_VERSION will need to acquire the  
"latest" last highest integer from somewhere, and then increment and  
update that

There are many alternatives for this, from the simple "I only run it on  
this 1 machine", to using etcd or some other distributed key/value  
store, to track and increment the global counter.

As a very general rule, I highly recommend using UUID/GUID for  
versioning as the concepts of ordering in time and space tend to get  
rather sloppy when dealing with distributed computer systems. UUIDs are  
intended to be unique by design. Is that sufficient?

A+  
Dave

---

<div class="post-metadata">

**Author:** ![Sudip\_Dutta](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@Sudip\_Dutta](https://forum.ansible.com/u/Sudip_Dutta)\
**Post date:** [December 5, 2017, 9:15am UTC](https://forum.ansible.com/t/how-to-increament-a-value-in-file-on-every-run/26202/3 "2017-12-05T09:15:33Z")

</div>

Thanks a lot Dave.

Anyway, Since “I only run it in one machine for now” and need to deploy as quickly as I can , I am planning to do this (in a rather crude way :-)) by using a simple bash script and invoking it in remote target -

oldnum=`cat filename | sed -n '8'p | cut -d '=' -f2`  
newnum=`expr $oldnum + 1`  
sed -i “s/$oldnum$/$newnum/g” filename

Anyway, would really love to hear if we can use any ansible module for above operation (lookup ? , even though lookup works locally, we can fetch the file in control machine do something, just a thought

---

<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:** [December 5, 2017, 12:41pm UTC](https://forum.ansible.com/t/how-to-increament-a-value-in-file-on-every-run/26202/4 "2017-12-05T12:41:13Z")

</div>

If BUILD\_VERSION = 4 is the only content of that file you could use this oneliner  
&nbsp;&nbsp;gawk -i inplace '{ print $1,$2,++$3}' filename  
in the Ansible command module

---

<div class="post-metadata">

**Author:** ![Sudip\_Dutta](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@Sudip\_Dutta](https://forum.ansible.com/u/Sudip_Dutta)\
**Post date:** [December 6, 2017, 6:26am UTC](https://forum.ansible.com/t/how-to-increament-a-value-in-file-on-every-run/26202/5 "2017-12-06T06:26:10Z")

</div>

Great !! would try that.

---

<div class="post-metadata">

**Author:** ![Sudip\_Dutta](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@Sudip\_Dutta](https://forum.ansible.com/u/Sudip_Dutta)\
**Post date:** [December 6, 2017, 6:28am UTC](https://forum.ansible.com/t/how-to-increament-a-value-in-file-on-every-run/26202/6 "2017-12-06T06:28:51Z")

</div>

Okay… missed one thing.

BUILD\_VERSION = 4 is not the only content. In my small script, you can see it is on line 8 -

oldnum=`cat filename | **sed -n '8'p** | cut -d '=' -f2`
