Puppet - 将哈希作为类参数传递

时间:2015-10-30 18:19:46

标签: puppet

尝试做这样的事情:

# nodes.pp
node 'dev-a-1.sn1.vpc1.example.com' inherits project_j1n_sn1_vpc1_dev {

    class { 'custom::core':
        overrides => {
            'openssh' => {'settings' => {'external_access' => 'true'}}, # Allow direct mounting for dev
            'rsyslog' => {'settings' => {'role' => 'node', 'filters' => {'php' => {'target' => 'remote'}, 'mail' => {'target' => 'remote'}}}}
        }
    }
}

# custom::core
class custom::core($overrides = {}) {

    if (has_key($overrides, 'openssh')) {

        $settings = $overrides['openssh']['settings']

        # Doesn't work
        create_resources('openssh', $settings)

        # Doesn't work
        class { 'openssh': $settings }
    }
}

是否可以调用类并传递哈希作为参数?

Puppet / Puppetmaster v2.7.26-1(Centos 6.7)

2 个答案:

答案 0 :(得分:3)

Puppet 4中有一种方法。

class { 'ssh':
  * => $settings
}

roidelapluie's blog上了解所有相关信息。

答案 1 :(得分:2)

我的一位同事在傀儡的2+天里找到了类似的东西。为此使用了create_resources

http://www.followski.com/quick-note-how-to-use-create_resources-to-initialize-a-class-in-puppet/

您的代码可能如下所示:

nodes.pp

node 'dev-a-1.sn1.vpc1.example.com' inherits project_j1n_sn1_vpc1_dev {

    class { 'custom::core':
        overrides => {
            'openssh' => {'settings' => {'external_access' => 'true'}}, # Allow direct mounting for dev
            'rsyslog' => {'settings' => {'role' => 'node', 'filters' => {'php' => {'target' => 'remote'}, 'mail' => {'target' => 'remote'}}}}
        }
    }
}

定制::芯

class custom::core($overrides = {}) {

    if (has_key($overrides, 'openssh')) {

        $settings = $overrides['openssh']['settings']

        create_resources('class', { 'openssh' => $settings })
    }
}

您会注意到,在上面链接的示例中,它看起来像create_resources('class', $params),但假设您有一个哈希,其中一个键是类名(即openssh),其值为params to组。我在上面说明的例子基本上做了同样的事情。

您的节点定义也可以如下所示:

node 'dev-a-1.sn1.vpc1.example.com' inherits project_j1n_sn1_vpc1_dev {

    class { 'custom::core':
        overrides => {
            'openssh' => {'external_access' => 'true'}, # Allow direct mounting for dev
            'rsyslog' => {'role' => 'node', 'filters' => {'php' => {'target' => 'remote'}, 'mail' => {'target' => 'remote'}}}
        }
    }
}

你的班级是这样的:

class custom::core($overrides = {}) {
  if (has_key($overrides, 'openssh')) {
    create_resources('class', {'openssh' => $overrides['openssh'])
  }

  if (has_key($overrides, 'rsyslog')) {
    create_resources('class', {'rsyslog' => $overrides['rsyslog'])
  }
}

我认为你现在明白了。

几年前我们经常使用它。不过,您可能需要考虑升级您的木偶基础设施。

希望这会有所帮助,或者至少指出正确的方向。