Edit a prop file using chef


#1

I saw a file resource which can be used to create and delete a file,but i want
to edit a properites file with values provided by the user.
Does chef provides such facility??Can someone provide and example of the usage


#2

Chef does not have this functionality directly or in the form of a
resource. One option could be to read in the file, parse the contents and
then output the ‘new’ file (with your changes) using a template
resource[1]. There are also things like this[2] in chef that could help.

Though a Chef purist would probably say that you shouldn’t be relying on
configuration already set on the system itself and that you should let chef
control the whole file (chef is the source/messenger of truth). So you
would either let your cookbook control the file or (if the property file is
outside the scope of your application/cookbook) let the correct cookbook
control the file and provide a means to inject/edit the properties through
that cookbook.

[1] - http://wiki.opscode.com/display/chef/Resources#Resources-Template
[2] - http://rubydoc.info/gems/chef/0.10.10/Chef/Util/FileEdit

On Thu, Jun 14, 2012 at 7:01 AM, minmini17@gmail.com wrote:

I saw a file resource which can be used to create and delete a file,but i
want
to edit a properites file with values provided by the user.
Does chef provides such facility??Can someone provide and example of the
usage


-Bryan


#3

There is no “append these lines if they are not present” function?

On Thu, Jun 14, 2012 at 7:13 AM, Bryan Baugher bjbq4d@gmail.com wrote:

Chef does not have this functionality directly or in the form of a resource.
One option could be to read in the file, parse the contents and then output
the ‘new’ file (with your changes) using a template resource[1]. There are
also things like this[2] in chef that could help.

Though a Chef purist would probably say that you shouldn’t be relying on
configuration already set on the system itself and that you should let chef
control the whole file (chef is the source/messenger of truth). So you would
either let your cookbook control the file or (if the property file is
outside the scope of your application/cookbook) let the correct cookbook
control the file and provide a means to inject/edit the properties through
that cookbook.

[1] - http://wiki.opscode.com/display/chef/Resources#Resources-Template
[2] - http://rubydoc.info/gems/chef/0.10.10/Chef/Util/FileEdit


#4

On Thu, Jun 14, 2012 at 1:18 PM, Eric Jain eric.jain@gmail.com wrote:

There is no “append these lines if they are not present” function?

I do this with a grep inside a not_if guard when I have to resort to
stuff like that.

On Thu, Jun 14, 2012 at 7:13 AM, Bryan Baugher bjbq4d@gmail.com wrote:

Chef does not have this functionality directly or in the form of a resource.
One option could be to read in the file, parse the contents and then output
the ‘new’ file (with your changes) using a template resource[1]. There are
also things like this[2] in chef that could help.

Though a Chef purist would probably say that you shouldn’t be relying on
configuration already set on the system itself and that you should let chef
control the whole file (chef is the source/messenger of truth). So you would
either let your cookbook control the file or (if the property file is
outside the scope of your application/cookbook) let the correct cookbook
control the file and provide a means to inject/edit the properties through
that cookbook.

[1] - http://wiki.opscode.com/display/chef/Resources#Resources-Template
[2] - http://rubydoc.info/gems/chef/0.10.10/Chef/Util/FileEdit


#5

On Thu, Jun 14, 2012 at 6:18 PM, Eric Jain eric.jain@gmail.com wrote:

There is no “append these lines if they are not present” function?

Yes:

http://rubydoc.info/gems/chef/0.10.10/Chef/Util/FileEdit#insert_line_if_no_match-instance_method

insert_line_if_no_match(regex, newline)

Well, it checks line by line, not a few lines, though.

On Thu, Jun 14, 2012 at 7:13 AM, Bryan Baugher bjbq4d@gmail.com wrote:

Chef does not have this functionality directly or in the form of a resource.
One option could be to read in the file, parse the contents and then output
the ‘new’ file (with your changes) using a template resource[1]. There are
also things like this[2] in chef that could help.

Though a Chef purist would probably say that you shouldn’t be relying on
configuration already set on the system itself and that you should let chef
control the whole file (chef is the source/messenger of truth). So you would
either let your cookbook control the file or (if the property file is
outside the scope of your application/cookbook) let the correct cookbook
control the file and provide a means to inject/edit the properties through
that cookbook.

[1] - http://wiki.opscode.com/display/chef/Resources#Resources-Template
[2] - http://rubydoc.info/gems/chef/0.10.10/Chef/Util/FileEdit


Juanje


#6

On Thu, Jun 14, 2012 at 11:16 AM, Juanje Ojeda Croissier
juanje.ojeda@gmail.com wrote:

http://rubydoc.info/gems/chef/0.10.10/Chef/Util/FileEdit#insert_line_if_no_match-instance_method

insert_line_if_no_match(regex, newline)

Well, it checks line by line, not a few lines, though.

Neat, somehow I had overlooked this method…


#7

Folks often have a strong desire for this and several methods have
been mentioned. I’ll just say that keeping this type of thing
idempotent is deceivingly difficult. You really don’t want to go down
this road if you can possibly avoid it.

KC

On Thu, Jun 14, 2012 at 11:16 AM, Juanje Ojeda Croissier
juanje.ojeda@gmail.com wrote:

On Thu, Jun 14, 2012 at 6:18 PM, Eric Jain eric.jain@gmail.com wrote:

There is no “append these lines if they are not present” function?

Yes:

http://rubydoc.info/gems/chef/0.10.10/Chef/Util/FileEdit#insert_line_if_no_match-instance_method

insert_line_if_no_match(regex, newline)

Well, it checks line by line, not a few lines, though.

On Thu, Jun 14, 2012 at 7:13 AM, Bryan Baugher bjbq4d@gmail.com wrote:

Chef does not have this functionality directly or in the form of a resource.
One option could be to read in the file, parse the contents and then output
the ‘new’ file (with your changes) using a template resource[1]. There are
also things like this[2] in chef that could help.

Though a Chef purist would probably say that you shouldn’t be relying on
configuration already set on the system itself and that you should let chef
control the whole file (chef is the source/messenger of truth). So you would
either let your cookbook control the file or (if the property file is
outside the scope of your application/cookbook) let the correct cookbook
control the file and provide a means to inject/edit the properties through
that cookbook.

[1] - http://wiki.opscode.com/display/chef/Resources#Resources-Template
[2] - http://rubydoc.info/gems/chef/0.10.10/Chef/Util/FileEdit


Juanje

http://about.me/juanje