Stephen Walsh
Stephen Walsh

Reputation: 835

Chef: Increment value in a file each time the cookbook is applied

One of my recipes echoes a static value into a file each time the cookbook is applied to a server, e.g.

bash 'example' do
  code <<-EOF
    echo "1" > /path/file.txt
  EOF
end

I would like to increment this value each time the cookbook is applied a server. So the next time, /path/file.txt would contain the value "2", then "3"...

Upvotes: 0

Views: 212

Answers (1)

coderanger
coderanger

Reputation: 54249

This is really weird thing to do since it would be non-convergent but whatever floats your boat:

ruby_block 'weird thing' do
  block do
    path = '/whatever.txt'
    value = IO.read(path).to_i
    IO.write(path, (value + 1).to_s)
  end
end

Upvotes: 2

Related Questions