0

I have config file /home/ipeacocks/Dropbox/nscd/nscd.conf:

$ cat home/ipeacocks/Dropbox/nscd/nscd.conf

        logfile                 /var/log/nscd.log
        threads                 4
        max-threads             32
        server-user             nobody
        stat-user               somebody
        debug-level             0
        reload-count            5
        paranoia                no
        restart-interval        3600

With puppet I want to change 2 lines:

        server-user             nobody
        paranoia                no

To these lines:

        server-user             nscd
        paranoia                yes

So for changing one first line I can use such manifest:

include nscd

class nscd {

    define line_replace ($line, $match) {
        file_line {'some useful info':
            path => '/home/ipeacocks/Dropbox/nscd/nscd.conf',  
            line => $line,
            match => $match
        }
    }

    anchor{'nscd::begin':}
    ->
    package { 'nscd': 
        ensure => installed,
    }
    ->

    line_replace {'test':
        line => "server-user             nscd",
        match => "^\s*server-user.*$"
        }
        ->

    service { 'nscd':
        ensure  => running,
        enable  => "true",
    }
    ->
    anchor{'nscd::end':}

}

Puppet launch:

» sudo puppet apply /home/ipeacocks/Dropbox/nscd/nscd.pp
Notice: Compiled catalog for softserve-pc.ddns.softservecom.com in environment production in 0.37 seconds
Notice: /Stage[main]/Nscd/Nscd::Line_replace[test]/File_line[some useful info]/ensure: created
Notice: Finished catalog run in 0.22 seconds

But cant when 2 lines (using declared function twice):

include nscd

class nscd {

    define line_replace ($line, $match) {
        file_line {'some useful info':
            path => '/home/ipeacocks/Dropbox/nscd/nscd.conf',  
            line => $line,
            match => $match
        }
    }

    anchor{'nscd::begin':}
    ->
    package { 'nscd': 
        ensure => installed,
    }
    ->

    line_replace {'test':
        line => "server-user             nscd",
        match => "^\s*server-user.*$"
        }
        ->

    line_replace {'test2':
        line => "paranoia                yes",
        match => "^\s*paranoia.*$"
        }
        ->

    service { 'nscd':
        ensure  => running,
        enable  => "true",
    }
    ->
    anchor{'nscd::end':}

}

Launching again:

» sudo puppet apply /home/ipeacocks/Dropbox/nscd/nscd.pp                                                                                                                1 ↵
Error: Duplicate declaration: File_line[some useful info] is already declared in file /home/ipeacocks/Dropbox/nscd/nscd.pp:10; cannot redeclare at /home/ipeacocks/Dropbox/nscd/nscd.pp:10 on node softserve-pc.ddns.softservecom.com
Error: Duplicate declaration: File_line[some useful info] is already declared in file /home/ipeacocks/Dropbox/nscd/nscd.pp:10; cannot redeclare at /home/ipeacocks/Dropbox/nscd/nscd.pp:10 on node softserve-pc.ddns.softservecom.com

What can be wrong? Is it possible to pass two pairs of vars to declared function at once (with arrays or like that)?

I have tried this solution but it doesn't work for me:

https://stackoverflow.com/a/19034077/2971192

Community
  • 1
  • 1
ipeacocks
  • 2,187
  • 3
  • 32
  • 47

2 Answers2

1

Replace your define with this:

define line_replace ($line, $match) {
    file_line {$name:
        path => '/home/ipeacocks/Dropbox/nscd/nscd.conf',  
        line => $line,
        match => $match
    }
}

I changes the file_line resource name from a constant to the $name parameter of your define.

cristi
  • 2,019
  • 1
  • 22
  • 31
1

Change the 'some useful info' to $name in file_line -

define line_replace ($line, $match) {
    file_line {$name:
        path => '/home/ipeacocks/Dropbox/nscd/nscd.conf',  
        line => $line,
        match => $match
    }
}

The problem you are facing is because the second call to line_replace causes call to file_line with resource name 'some useful info' which is already declared.

Anshu Prateek
  • 3,011
  • 1
  • 18
  • 33
  • Thank you @anshu-prateek. Is it possible to send these variables as array to defined function? – ipeacocks Oct 30 '15 at 13:39
  • What the heck Anshu, it looks like you just copied my answer 1 minute after I post it. – cristi Nov 01 '15 at 19:38
  • 1
    @cristi - the code snippets are same because thats the correct answer. You get 100 folks in a room and ask them to do 1+1, 99 of them will have the same answer, that doesn't mean the rest of 98 copied the first one :). Explain your answers, why you did what you did, why is the OP facing problems and you will have higher "accepted" answers. Hope you can see that in my answer above. – Anshu Prateek Nov 02 '15 at 04:50
  • @ipeacocks it can be done, but since there are multiple params involved, its not straightforward. I see you have a separate q for it now, lets see if that gets answered else I will try another jab at it. – Anshu Prateek Nov 02 '15 at 04:53