What is the best way to change a field of a conf file in centos

Hi,

I am trying to change a field (here is “net.ipv4.ip_nonlocal_bind=1”) of
the file /etc/sysctl.conf, currently I am using template to change the
field, however someone complained that it was not good to template the
system file '‘sysctl.conf’, because it may also used by other cookbooks. I
agreed with that guy, it is definitely better to only change a specific
field instead of the whole file with respect to a system file.

I am wondering what is the best way to change a specific field of a config
file in Chef.
It will be much appreciated if someone can give me some hints and share
some opinions.

Thanks,
Sam

Sam,

Check out Chef::Util::FileEdit to match and replace lines in a single file
without replacing the entire file with a template. The chef-server cookbook
has a good example of this here:

Cheers,
MikeD

On Tue, Mar 25, 2014 at 8:23 PM, Sam Su sam.debian@gmail.com wrote:

Hi,

I am trying to change a field (here is "net.ipv4.ip_nonlocal_bind=1") of
the file /etc/sysctl.conf, currently I am using template to change the
field, however someone complained that it was not good to template the
system file ''sysctl.conf', because it may also used by other cookbooks. I
agreed with that guy, it is definitely better to only change a specific
field instead of the whole file with respect to a system file.

I am wondering what is the best way to change a specific field of a config
file in Chef.
It will be much appreciated if someone can give me some hints and share
some opinions.

Thanks,
Sam

There is a cookbook for managing sysctl:
http://community.opscode.com/cookbooks/sysctl

You best bet is to depend on and include that cookbook, and then either use
node.default['sysctl']['params']['net']['ipv4']['ip_nonlocal_bind'] = 1

or the LWRP:

sysctl_param 'net.ipv4.ip_nonlocal_bind' do
value 1
end

On Tue, Mar 25, 2014 at 5:23 PM, Sam Su sam.debian@gmail.com wrote:

Hi,

I am trying to change a field (here is "net.ipv4.ip_nonlocal_bind=1") of
the file /etc/sysctl.conf, currently I am using template to change the
field, however someone complained that it was not good to template the
system file ''sysctl.conf', because it may also used by other cookbooks. I
agreed with that guy, it is definitely better to only change a specific
field instead of the whole file with respect to a system file.

I am wondering what is the best way to change a specific field of a config
file in Chef.
It will be much appreciated if someone can give me some hints and share
some opinions.

Thanks,
Sam

Mike's solution is the least obtrusive, but if other cookbook authors are
good citizens, they'll be using the sysctl cookbook to edit that file as
well.

On Tue, Mar 25, 2014 at 5:39 PM, Christopher Armstrong <
chris@chrisarmstrong.me> wrote:

There is a cookbook for managing sysctl:
http://community.opscode.com/cookbooks/sysctl

You best bet is to depend on and include that cookbook, and then either
use node.default['sysctl']['params']['net']['ipv4']['ip_nonlocal_bind'] =
1

or the LWRP:

sysctl_param 'net.ipv4.ip_nonlocal_bind' do
value 1
end

On Tue, Mar 25, 2014 at 5:23 PM, Sam Su sam.debian@gmail.com wrote:

Hi,

I am trying to change a field (here is "net.ipv4.ip_nonlocal_bind=1") of
the file /etc/sysctl.conf, currently I am using template to change the
field, however someone complained that it was not good to template the
system file ''sysctl.conf', because it may also used by other cookbooks. I
agreed with that guy, it is definitely better to only change a specific
field instead of the whole file with respect to a system file.

I am wondering what is the best way to change a specific field of a
config file in Chef.
It will be much appreciated if someone can give me some hints and share
some opinions.

Thanks,
Sam

Hi Mike and Christopher,

Great, thank you so much for you guys' help.

I totally agree with Christopher, for my situation the Chef::Util::FileEdit
solution may be better.

Thanks,
Sam

On Tue, Mar 25, 2014 at 5:40 PM, Christopher Armstrong <
chris@chrisarmstrong.me> wrote:

Mike's solution is the least obtrusive, but if other cookbook authors are
good citizens, they'll be using the sysctl cookbook to edit that file as
well.

On Tue, Mar 25, 2014 at 5:39 PM, Christopher Armstrong <
chris@chrisarmstrong.me> wrote:

There is a cookbook for managing sysctl:
http://community.opscode.com/cookbooks/sysctl

You best bet is to depend on and include that cookbook, and then either
use node.default['sysctl']['params']['net']['ipv4']['ip_nonlocal_bind']
= 1

or the LWRP:

sysctl_param 'net.ipv4.ip_nonlocal_bind' do
value 1
end

On Tue, Mar 25, 2014 at 5:23 PM, Sam Su sam.debian@gmail.com wrote:

Hi,

I am trying to change a field (here is "net.ipv4.ip_nonlocal_bind=1") of
the file /etc/sysctl.conf, currently I am using template to change the
field, however someone complained that it was not good to template the
system file ''sysctl.conf', because it may also used by other cookbooks. I
agreed with that guy, it is definitely better to only change a specific
field instead of the whole file with respect to a system file.

I am wondering what is the best way to change a specific field of a
config file in Chef.
It will be much appreciated if someone can give me some hints and share
some opinions.

Thanks,
Sam