AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 512203
Accepted
Son of the Wai-Pan
Son of the Wai-Pan
Asked: 2013-06-01 00:08:21 +0800 CST2013-06-01 00:08:21 +0800 CST 2013-06-01 00:08:21 +0800 CST

如何在 Puppet 中使用数组配置参数化类?

  • 772

我的目标是定义一个类,该类将一对数组作为输入。由于我不确定如何定义成对的数组,因此我将改为使用一对数组。

class student($username, $full_name){
  notify {"user":  message => "username: ${username}\n"}
  notify {"fullname":  message => "fullname: ${full_name}\n"}
  exec {'finger': command => "/usr/bin/finger ${username}", logoutput => true }
}

$users = ['elion', 'azee', 'root']
$names = ['El Lion', 'Avery Zee', "Rooty Root"]

class { 'student':
  username => $users,
  full_name => $names
}

在执行“puppet apply file.pp”后,我希望输出遍历两个数组,打印出通知并按照数组给定的顺序指法学生(这听起来很糟糕)。我尝试了这个,结果发生了什么,木偶将数组合并为一个字符串,所以 $users 值最终为:elionazeeroot并且 $names 值最终为El Lion Avery Zee Rooty Root.

两个问题:

  1. 有没有办法定义一个类来处理一组数据,就像我在这里尝试做的那样?
  2. 我怀疑我没有使用正确的结构来实现我的目标。我应该怎么做呢?
puppet
  • 1 1 个回答
  • 8651 Views

1 个回答

  • Voted
  1. Best Answer
    asciiphil
    2013-06-01T05:23:03+08:002013-06-01T05:23:03+08:00

    用编程语言表示数据的自然方式是像这样的哈希:

    $users = {
      elion => 'El Lion',
      azee  => 'Avery Zee',
      root  => 'Rooty Root,
    }
    

    虽然在 Puppet 中这样做是可能的,但我发现最好决定单个条目的资源定义是什么,然后将散列结构化为可以传递给create_resources()的内容。在这种情况下,这意味着这样的事情:

    define student ($full_name) {
      notify { "user ${title}":
        message => "username: ${title}\n",
      }
      notify { "fullname ${title}":
        message => "fullname: ${full_name}\n",
      }
      exec { "finger ${title}":
        command   => "/usr/bin/finger ${title}",
        logoutput => true,
      }
    }
    
    $users = {
      elion => { full_name => 'El Lion'   },
      azee  => { full_name => 'Avery Zee' },
      root  => { full_name => 'Rooty Root },
    }
    
    create_resources(student, $users)
    

    然而,正如 Ger 指出的那样,如果您使用Hiera,您会过得最好。Hiera 允许您将代码(清单定义)与数据(在本例中为学生的特定姓名)分开。完整地说,这是我将为您的设置做的事情:

    • 将学生数据放入 hiera 文件中。您可以使用 JSON 或 YAML;我喜欢 YAML。它会是这样的:

      classroom::users:
        elion:
          full_name: El Lion
        azee:
          full_name: Avery Zee
        root:
          full_name: Rooty Root
      
    • 为学生定义一个资源类型并将其放入classroom/manifests/student.ppPuppet 的$modulepath. 它看起来像student上面的资源类型,但将被命名为classroom::student.

    • 使用以下内容定义一个classroom类:classroom/manifests/init.pp

      class classroom (
        $users = hiera('classroom::users', undef),
      ) {
        create_resources(classroom::student, $users)
      }
      

    如果您使用的是 Puppet 3.x,hiera()类参数中的调用是多余的,但如果您仍在使用 Puppet 2.7,则需要它。

    • 4

相关问题

  • 如何修复 Puppet 完全限定的参数路径错误?

  • puppet:修改配置文件后强制重启服务

  • 木偶模块资源

  • 傀儡主机名问题

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve