木偶。使用带有2个变量的define

时间:2015-10-30 12:19:01

标签: puppet declaration

我有配置文件/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

使用木偶我想改变2行:

        server-user             nobody
        paranoia                no

到这些方面:

        server-user             nscd
        paranoia                yes

因此,为了改变第一行,我可以使用这样的清单:

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发布:

» 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

但当2行(使用声明的函数两次)时不能使用:

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':}

}

再次启动:

» 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

有什么不对?是否可以同时将两对变量传递给声明的函数(使用数组或类似的变量)?

我尝试过这个解决方案,但它对我不起作用:

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

2 个答案:

答案 0 :(得分:1)

用以下内容替换你的定义:

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

我将file_line资源名称从常量更改为定义的$ name参数。

答案 1 :(得分:1)

更改一些有用的信息'到file_line中的$ name -

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

您遇到的问题是因为第二次调用line_replace会导致调用带有资源名称的file_line'一些有用的信息'已经宣布。