How to increament a value in file on every run

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 ?

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

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

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

Great !! would try that.

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