Any equivalent of "puppet augeas" in Chef?


#1

Hi there,

I need to do in-place file editing of config files using Chef. For now, i could
see that we can append data to the config files using the “bash” resource. But,
i need to change specific variables in config files using Chef.

I could see that there’s a handy tool called augeas in puppet that clearly
performs the above requirement. But, i couldn’t come up with anything in Chef.

So, is there any way this could be done in Chef?

Thanks in advance.


#2

theres a line resource iirc and also fileedit, i dont know if anybody has
written a native augeas binding. I was considering it for editing in place
xml configs (because chef used internal dsl, building augeus like
capabilities are trivial), but extending augeas is another pain (the lenses
needs to be written in its own dsl ), afterwards i fall back to home grown
nokogiri based in place xml_template file resource.

http://community.opscode.com/cookbooks/line
http://rubydoc.info/gems/chef/0.8.16/Chef/Util/FileEdit

On Sun, Sep 15, 2013 at 11:11 PM, sivajimsingh@gmail.com wrote:

Hi there,

I need to do in-place file editing of config files using Chef. For now, i
could
see that we can append data to the config files using the “bash” resource.
But,
i need to change specific variables in config files using Chef.

I could see that there’s a handy tool called augeas in puppet that clearly
performs the above requirement. But, i couldn’t come up with anything in
Chef.

So, is there any way this could be done in Chef?

Thanks in advance.


#3

Here’s some previous discussion http://lists.opscode.com/sympa/arc/chef/2013-02/msg00337.html

I think Noah Kantrowitz answers perfectly http://lists.opscode.com/sympa/arc/chef/2013-02/msg00339.html


Matthew Hooker

On Sunday, September 15, 2013 at 11:11 PM, sivajimsingh@gmail.com wrote:

Hi there,

I need to do in-place file editing of config files using Chef. For now, i could
see that we can append data to the config files using the “bash” resource. But,
i need to change specific variables in config files using Chef.

I could see that there’s a handy tool called augeas in puppet that clearly
performs the above requirement. But, i couldn’t come up with anything in Chef.

So, is there any way this could be done in Chef?

Thanks in advance.


#4

I wrote an LWRP when I had a similar challenge. Its underlying lib had a few classes that modeled the config, had interfaces for changes, and handled the file read/write stuff. Fairly trivial, really.

On Sep 15, 2013, at 23:11, sivajimsingh@gmail.com wrote:

Hi there,

I need to do in-place file editing of config files using Chef. For now, i could
see that we can append data to the config files using the “bash” resource. But,
i need to change specific variables in config files using Chef.

I could see that there’s a handy tool called augeas in puppet that clearly
performs the above requirement. But, i couldn’t come up with anything in Chef.

So, is there any way this could be done in Chef?

Thanks in advance.


#5

On 2013-09-16, at 06:11, sivajimsingh@gmail.com wrote:

I need to do in-place file editing of config files using Chef. For now, i could
see that we can append data to the config files using the “bash” resource. But,
i need to change specific variables in config files using Chef.

I could see that there’s a handy tool called augeas in puppet that clearly
performs the above requirement. But, i couldn’t come up with anything in Chef.

So, is there any way this could be done in Chef?

There isn’t an augeas resource provided by chef, and cursory searching doesn’t reveal a cookbook with one. But you could certainly write one. Here’s some inspiration:

Salt provides an augeas ‘state’ (like resource) with one ‘command’ (like action): setvalue. It also provides a ‘module’ (like library) that supports get, ls, match, remove, setvalue & tree.
http://docs.saltstack.com/ref/modules/all/salt.modules.augeas_cfg.html
http://docs.saltstack.com/ref/states/all/salt.states.augeas.html

Puppet has very thorough complete doc for using the augeas ‘type’ (like resource) in puppet:
http://projects.puppetlabs.com/projects/puppet/wiki/Puppet_Augeas
http://docs.puppetlabs.com/references/latest/type.html#augeas

I’d like to see a chef resource that does things like:

augeas '/files/etc/ssh/sshd_config/AcceptEnv/01' do
  value 'FOO'
  action :set
end

augeas 'accept foo env in sshd' do
  path '/files/etc/ssh/sshd_config/AcceptEnv/01'
  value 'FOO'
  action :set
end

augeas "/files/etc/ssh/sshd_config/AcceptEnv/01" do
  action :rm
end

Looks like there are some decent augeas bindings in ruby-augeas:
https://github.com/hercules-team/ruby-augeas

I think you could get dangerously far with something like

require 'ruby-augeas'

action :set do
  Augeas::open do |aug|
    aug.set(new_resource.path, new_resource.path)
    unless aug.save
      raise IOError, "Failed to save changes"
    end
  end
  new_resource.updated_by_last_action(true)
end

action :rm do
  Augeas::open do |aug|
    aug.rm(new_resource.path)
    unless aug.save
      raise IOError, "Failed to save changes"
    end
  end
  new_resource.updated_by_last_action(true)
end

I’d still want a recipe to install augeas and the ruby-augeas gem, plus some actual error handling, really checking if the resource was updated, and converge_by/why-run support would be awesome.

~j