尝试做这样的事情:

# 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)

最佳答案

我的一个同事想出了一个很好的解决方案,可以解决 puppet 在旧2+时代的类似问题。为此使用了create_resources

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

您的代码如下所示:

节点

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),其值为要设置的参数。我在上面陈述的示例基本上执行相同的操作。

您的节点定义也可以如下所示:
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)是这样的:
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'])
  }
}

我想您现在明白了。

几年前,我们经常使用它。但是,您可能需要考虑升级 puppet 基础结构。

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

关于 puppet -将哈希作为类参数传递,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33442533/

10-13 05:37