puppet - 可选择将参数传递给 puppet 资源

标签 puppet

有没有办法仅在已设置的情况下将参数传递给 puppet 资源?例如,假设我有一个创建新用户的 list ,我可能想也可能不想手动设置 uid 和 gid。我能想到的最好方法是这样的:

class example (
    $uid      = undef,
    $gid      = undef,
    $username = 'default'
) {
    user { $username:
        ensure => present,
        # etc
    }

    if $uid != undef {
        user { "uid-${username}":
            name    => $username,
            uid     => $uid,
            require => User[$username]
        }
    }

    if $gid != undef {
        user { "gid-${username}":
            name    => $username,
            gid     => $gid,
            require => User[$username]
        }
    }
}

但这有很多代码只是为了确定是否发送 uid 和/或 gid。有更好的办法吗?

最佳答案

我认为您的示例由于多个声明而无法编译,因为 name关于user资源是正在检查唯一性的内容。话虽这么说,在我看来,有几种方法可以让它稍微好一点。

第一种方式涉及属性修改:

...
user { $username:
    ensure => present,
    # etc
}

if $uid != undef {
    User[$username] { uid => $uid }
}

if $gid != undef {
    User[$username] { gid => $gid }
}

第二个涉及从哈希设置属性:

class example (
    Hash $id         = {},
    String $username = 'default'
) {
    user { $username:
        ensure => present,
        *      => $id,
        # etc
    }
}

然后通过声明传递附加参数:

class example { 'foo':
  username => 'bar',
  id       => { uid => 1000, gid => 1000 }
}

或使用类似于以下内容的自动 Hiera 查找:

example::id:
  uid: 1000
  gid: 1000

请注意,未指定 id在任何一种情况下(声明或 Hiera)仍然会导致 user 的正确且预期的行为。资源无uidgid已更改。

您可以在 https://docs.puppet.com/puppet/latest/reference/lang_resources_advanced.html 中找到记录的两种方法。 ,您甚至可以从该文档中找出另一种或两种方法。

关于puppet - 可选择将参数传递给 puppet 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40530656/

相关文章:

puppet : How to execute a single class?

puppet 根据键将数组转换为哈希值

cron - puppet 安装的 cron 作业位于哪里?

ruby - erb sudoers 迭代数组的哈希值

ruby - 事实文件已解析但返回空数据集

centos - 使用 puppet 将 exclude 添加到现有的 yum repo 配置

apache - 在 Vagrant 上从 Puppetlabs 安装 Apache

ruby - 在自定义事实中使用其他事实

ruby - 为什么我的 ruby​​ 代码中有单例 Puppet Arrays 字符串?

java - %JAVA_HOME% 在 Windows PATH 中如何工作?