Ask Your Question
0

comment out in augeas

asked 2015-01-19 11:20:43 -0500

Chutki gravatar image

updated 2015-01-29 11:06:11 -0500

Hi,

I am testing "comment out" part in augeas. My requirement is, want to comment out the existing lines.

Below is my file

orange
apple
banana
pappaya is good for eyes

I want to comment out apple (row 2). I tried below manifests, but it is creating new line

class commentfile::commnettest {
augeas { "testfilecomment":
    lens    => 'Simplelines.lns',
    incl    => '/tmp/comment_test',
    changes => ["insert #comment  after orange",
                "set #comment  sweet"]
 }
}

below is the o/p i got when i ran manifests

orange
apple
banana
pappaya is good for eyes
#apple

I tried the below option as well

changes => ["insert #comment  after orange",
                "set #comment  sweet"]

but I am getting error "

Error: /Stage[main]/Commentfile::Commnettest/Augeas[testfilecomment]: Could not evaluate: Error sending command 'insert' with params ["#comment", "after", "/files/tmp/comment_test/orange"]/Error sending command 'insert' with params ["#comment", "after", "/files/tmp/comment_test/orange"]
"

I want the output like below

orange
#apple
banana
pappaya is good for eyes

=============================================================================================

Hi,

I have tried below two manifests,but unfortunately none of the manifests are not working

class fileline_test::file_line {
  shellvar { "fileline_test":
  ensure  => present,
  target  => "/tmp/testfileline",
  comment => "aaa  eee  ccc   ddd"
}
}
Error: Could not retrieve catalog from remote server: Error 400 on SERVER:
Puppet::Parser::AST::Resource failed with error ArgumentError:
Invalid resource type shellvar at /etc/puppet/modules/test/fileline_test/manifests/file_line.pp:7 on

second manifests

class fileline_test::file_line {
  file_line { 'testfileline':
    ensure => present,
    path => '/tmp/testfileline',
    line => "#aaa   eee   ccc   ddd",
    match => '^aaa'
  }
}

Error: Failed to apply catalog: Validation of File_line[testfileline] failed: When providing a 'match' parameter, the value must be a regex that matches against the value of your 'line' parameter at /etc/puppet/modules/test/fileline_test/manifests/file_line.pp:8

below is my test file

888   999   000   3245
aaa   eee   ccc   ddd
vvv   333   dec   bcd
cdf   sw3   fff   dss

want the o/p like below

888   999   000   3245
#aaa   eee   ccc   ddd
vvv   333   dec   bcd
cdf   sw3   fff   dss

==================================================================

Hi,

I tried below manifests as you update in the commnts, below is manifests

class commentfile::service_comment {

  augeas { "augfile_testabc":
    lens    => 'services.lns',
    incl    => '/etc/services',
    #lens    => 'Simplelines.lns',
    #incl    => '/tmp/comment_test',
    changes => "mv service-name[.='testcomment'] #comment"
 }
}

But I got the below error message

Warning: Augeas[augfile_testabc](provider=augeas): Loading failed for one or more files, see debug for /augeas//error output
Error: /Stage[main]/Commentfile::Service_comment/Augeas[augfile_testabc]:
Could not evaluate: Error sending command 'mv' with params ["/files/etc/services/*[.=~regexp(\"testfileaugeas.*\")]",
"/files/etc/services/#comment"]/
Error sending command 'mv' with params ["/files/etc/services/*[.=~regexp(\"testfileaugeas.*\")]", "/files/etc/services/#comment"]

I have tried all the below options, noluck

changes => "mv service-name*[.=~regexp('testcomment.*')] #comment"
changes => "mv service-name[.=~regexp('testcomment.*')] #comment"
changes => mv *[.="testcomment"] #comment'
changes => mv *[.=~regexp('testcomment.*')] #comment

But when I try with Simplelines.lns, it is working perfectly.

The problem is only when I use services.lns

please help me I am going mad of this one

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2015-01-20 02:50:37 -0500

domcleal gravatar image

While cbarbour's answer is entirely correct, with Simplelines you can nearly get this restriction by moving a node to a new name.

changes => 'mv *[.="apple"] #comment'

This renames a node with the value (that's the ".") of "apple" to the name #comment, though it has the side effect of shuffling it to the end of the file.

Augeas itself has a rename method which would preserve positioning, but the Puppet provider doesn't expose this today - you could file a ticket against Puppet to add it.

edit flag offensive delete link more

Comments

Unless I'm missing something (E.g. augeas has a way to flatten trees on mv), this won't work in most cases. See my updated answer.

cbarbour gravatar imagecbarbour ( 2015-01-20 19:24:06 -0500 )edit

cbarbour - sure, but I qualified my answer by saying "with Simplelines", which was used in the question - not crontab.

domcleal gravatar imagedomcleal ( 2015-01-21 02:05:08 -0500 )edit

Good clarification. I haven't played with smplelines, but that does make complete sense.

cbarbour gravatar imagecbarbour ( 2015-01-21 05:02:46 -0500 )edit

Using the default lens, no (maybe file_line or something can). You could set lens (and incl) to Simplelines.lns and then something like: mv *[.=~regexp('myservice.*')] #comment, but it's not great.

domcleal gravatar imagedomcleal ( 2015-01-28 02:12:34 -0500 )edit

Domcleal, I edited my question. Is there any update on this

Chutki gravatar imageChutki ( 2015-02-01 11:28:10 -0500 )edit
1

answered 2015-01-19 13:51:41 -0500

cbarbour gravatar image

updated 2015-01-20 19:22:58 -0500

With Augeas, each element of the configuration file is a node on a tree structure. Augeas doesn't think in terms of character editing of the files, it thinks of the files as a tree of objects, each with it's own structure.

To comment out a line, you don't insert a comment object; doing so creates a new line in the file (and a new branch on the tree), leaving the original node alone. Instead, you need to transform the original object from a configuration entry, into a comment. Doing so isn't actually very easy with Augeas, because a comment has a different structure than the original node.

While cbarbour's answer is entirely correct, with Simplelines you can nearly get this restriction by moving a node to a new name.

This renames a node with the value (that's the ".") of "apple" to the name #comment, though it has the side effect of shuffling it to the end of the file.

Unfortunately, this won't work in many cases. The reason why is that the #comment node often won't match the structure of the tree you're trying to comment out.

Here's a real-world example:

# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
1 1 1 1 1 root echo foo



# augtool

augtool> print /files/etc/crontab/entry[.="echo foo"]
/files/etc/crontab/entry = "echo foo"
/files/etc/crontab/entry/time
/files/etc/crontab/entry/time/minute = "1"
/files/etc/crontab/entry/time/hour = "1"
/files/etc/crontab/entry/time/dayofmonth = "1"
/files/etc/crontab/entry/time/month = "1"
/files/etc/crontab/entry/time/dayofweek = "1"
/files/etc/crontab/entry/user = "root"


augtool> mv /files/etc/crontab/entry[.="echo foo"] /files/etc/crontab/#comment[last()+1]

augtool> print /files/etc/crontab/
/files/etc/crontab
/files/etc/crontab/SHELL = "/bin/bash"
/files/etc/crontab/PATH = "/sbin:/bin:/usr/sbin:/usr/bin"
/files/etc/crontab/MAILTO = "root"
/files/etc/crontab/HOME = "/"
/files/etc/crontab/#comment[1] = "For details see man 4 crontabs"
/files/etc/crontab/#comment[2] = "Example of job definition:"
/files/etc/crontab/#comment[3] = ".---------------- minute (0 - 59)"
/files/etc/crontab/#comment[4] = "|  .------------- hour (0 - 23)"
/files/etc/crontab/#comment[5] = "|  |  .---------- day of month (1 - 31)"
/files/etc/crontab/#comment[6] = "|  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ..."
/files/etc/crontab/#comment[7] = "|  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat"
/files/etc/crontab/#comment[8] = "|  |  |  |  |"
/files/etc/crontab/#comment[9] = "*  *  *  *  * user-name command to be executed"
/files/etc/crontab/#comment[10] = "echo foo"
/files/etc/crontab/#comment[10]/time
/files/etc/crontab/#comment[10]/time/minute ...
(more)
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

2 followers

Stats

Asked: 2015-01-19 11:20:43 -0500

Seen: 1,868 times

Last updated: Jan 29 '15