Ask Your Question

How to resolve "more than one line match patterns" problem using file_line?

asked 2016-08-13 03:23:15 -0500

showx12 gravatar image

updated 2016-08-14 22:09:08 -0500


I'm trying to change configuration file using puppet.

This is my test.txt file that i want to change :



I want to change "#puppet=no" to "puppet=yes" only on [default] tab.

This is my test.pp for two version :

   path => '/root/openstack-puppet/computenode/nova/test.txt',
   match => '[default]\n#puppet',
   line => 'puppet=ok'

This one failed to find match pattern, so it just add "puppet=ok" at the end of file.

   path => '/root/openstack-puppet/computenode/nova/test.txt',
   match => '#puppet',
   line => 'puppet=ok'

This one failed because of multi match pattern problem.

I tried Augeas also, but I can't find how to uncomment using Augeas.

Someone please help me with this problem!!

================================================================= I tried in 'match' attribute like this :

match => '[default]\n#puppet' match => '[default]\n#puppet' match => '[default]\\n#puppet'

All of this cannot find match pattern, so it just add 'puppet=ok' at the end of the file/

Then, I tried 'affter' attribute I run this code :

file_line { 'someline':
  path     => '/root/openstack-puppet/computenode/nova/test.txt',
  after    => '\[default\]',
  multiple => 'false',
  match    => '#puppet',
  line     => 'puppet=ok',

But when I run with "puppet apply" it still makes multi match error :

Error: More than one line in file '/root/openstack-puppet/computenode/nova/test.txt' matches pattern '#puppet'
Error: /Stage[main]/Main/File_line[someline]/ensure: change from absent to present failed: More than one line in file '/root/openstack-puppet/computenode/nova/test.txt' matches pattern '#puppet'

I think that 'after' attribute cannot applied when 'match' attribute is defined.

When I erase 'match' attribute, it works, but it didn't replace original string('#puppet=no').

It just added new line after [default] like this :



So the issues still remain, how can I erase(or replace) the string '#puppet=no' only on [default] tab??

edit retag flag offensive close merge delete


I believe `[` and `]` have a special meaning in regular expressions. I suspect you need to escape them to do a literal match.

kaizenCoder gravatar imagekaizenCoder ( 2016-08-14 00:02:51 -0500 )edit

Thank you for helping me!!. However, I also tried your answer, and 'affter' attribute, it still didn't work well. I added these on my question. Please check it. Thanks.

showx12 gravatar imageshowx12 ( 2016-08-14 22:05:43 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2017-04-11 23:10:48 -0500

natemccurdy gravatar image

updated 2017-04-11 23:12:51 -0500

file_linedoesn't handle multiple lines and augeus can be hard to figure out.

What you need to solve this problem is the ini_settingresource from

ini_setting { 'openstack nova puppet':
  ensure            => present,
  path              => '/root/openstack-puppet/computenode/nova/test.txt',
  section           => 'default',
  setting           => 'puppet',
  value             => 'yes',
  key_val_separator => '='

ini_setting lets you manage individual settings within sections of config files. Read the README of the Forge module for more examples.

Also, just a note, this won't remove the commented line from the .txt file, but it will add the correct line to it.

edit flag offensive delete link more

answered 2016-08-15 06:05:17 -0500

Emerson Prado gravatar image

updated 2017-04-10 15:19:19 -0500

Well, this seems to be a dead end. According to the docs, the "match" attribute will fail if multiple lines match the pattern and "multiple" attribute is false. It doesn't seem the "after" attribute affects this behaviour. Maybe you could open a feature request ticket, asking that "after" limits the results from "match".
By the way, don't use "\n" in patterns which should match a single line. Single lines never have line breaks.

EDIT: some Augeas

Using the standalone Augeas tool (augtool), I could do this change with 2 commands:

mv /files/etc/puppet/puppet.conf/default/#comment[. =~ regexp('^puppet=.*')] /files/etc/puppet/puppet.conf/default/directive
set /files/etc/puppet/puppet.conf/default/directive[. =~ regexp('puppet=.*')] 'puppet=yes'

mv changes the node, which is how Augeas calls each line.
/files should precede any path leading to a file. Yes, Augeas has other kinds of path.
Your section is specified right after the file path: .../puppet.conf/default.
#comment[<match>] finds a matching comment. In your case, starting with "puppet=". The regexp should match the entire line. Note the "#".
.../directive states the matching node should have its type changed from "#comment" to "directive".
set <directive> <new value> does exactly what it states

So, moving on to Puppet, this is how I almost managed to uncomment this line:

augeas { "uncomment":
    context => '/files/home/vagrant/test.txt/default',
    changes => [
        'mv #comment[. =~ regexp('^puppet=.*')] directive',
        'set directive[. =~ regexp('puppet=.*')] "puppet=yes''',

Unfortunately, the first mv didn't work as intended, changing '#puppet=no' to 'directive=puppet=no'. Maybe the resource augeas doesn't understand all labels known by the augeas tool, or something isn't well implemented yet. Or maybe I should have set the lens, something I didn't fully understand so far.

I hope this helps. In any case, go for:

Best regards,

edit flag offensive delete link more


Thanks for answering! So file_line cannot match multi line?? Do I have to use Augeas or sometihing else to match multi line??

showx12 gravatar imageshowx12 ( 2016-08-15 09:56:41 -0500 )edit

It's been long and you probably found a way out, but I remembered I had a similar problem once - having to uncomment a line with Augeas. Since your match is a bit more complex, maybe Augeas is the only way to go. I'll play a bit and add some code in my answer.

Emerson Prado gravatar imageEmerson Prado ( 2017-04-10 14:35:02 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2016-08-13 03:23:15 -0500

Seen: 368 times

Last updated: Apr 11