简体   繁体   English

带有hiera的木偶中模块的基本用法

[英]Basic usage of modules in puppet with hiera

I want to use puppet to manage some servers. 我想使用p来管理一些服务器。 Even after reading dozens of documentation pages, it is not clear to me how to use modules and how to use them with hiera. 即使阅读了数十个文档页面,我仍然不清楚如何使用模块以及如何将模块与hiera一起使用。 As first experiment I wanted a user "admin" to be created on one node and found this module -> https://github.com/camptocamp/puppet-accounts 作为第一个实验,我希望在一个节点上创建一个用户“ admin”并找到此模块-> https://github.com/camptocamp/puppet-accounts

My /etc/puppet/hiera.yaml looks as simple as this 我的/etc/puppet/hiera.yaml看起来像这样简单

---
:backends:
  - yaml
:hierarchy:
  - node/%{::fqdn}
  - common
:yaml:
  :datadir: /etc/puppet/hieradata

My /etc/puppet/hieradata/node/node1.example.com.yaml contains this 我的/etc/puppet/hieradata/node/node1.example.com.yaml包含了这个

---
accounts::users:
  admin:
    uid: 1010
    comment: admin
accounts::ssh_keys:
  admin:
    comment: ad
    type: ssh-rsa
    public: AAAAAAAAAAAAAA

This worked after I put this in my /etc/puppet/manifests/site.pp 在我将其放入/etc/puppet/manifests/site.pp后,此方法有效

hiera_include('classes')

class
{
    'accounts':
    ssh_keys   => hiera_hash('accounts::ssh_keys', {}),
    users      => hiera_hash('accounts::users', {}),
    usergroups => hiera_hash('accounts::usergroups', {}),
}

accounts::account
{
    'admin':
}

Is this good practice? 这是好习惯吗? To me it feels wrong to put that stuff into site.pp since it gets messed up when I later use more modules. 对我来说,把这些东西放到site.pp中感觉是错误的,因为当我以后使用更多模块时,它会被弄乱。 But where else to put it? 但是还有什么地方呢? I also don't understand how this separates data from logic, since I have data in both, node1.example.com.yaml and site.pp (admin). 我还不了解如何将数据与逻辑分开,因为我在node1.example.com.yaml和site.pp(管理员)中都有数据。 Some help would be great.. 一些帮助会很棒。

To understand what hiera is, you should think simply that Hiera is a DATABASE for puppet, a database of Variables/values and nothing more. 要了解什么是hiera,您应该简单地认为Hiera是puppet的数据库,是变量/值的数据库,仅此而已。

For a beginner I would suggest to focus on other parts of the system, like how to create modules! 对于初学者,我建议您专注于系统的其他部分,例如如何创建模块! and how to manage your needs (without complexity) and then slowly build the "smart" recipes or the reusable ones... 以及如何管理您的需求(无复杂性),然后慢慢构建“智能”食谱或可重复使用的食谱...

Your puppet will first sick for a file called sites.pp (usually is on your main $confdir (puppet.conf variable. I am not going to mention environments it is for later.) 您的木偶会首先厌倦名为sites.pp的文件(通常在主$ confdir中(puppet.conf变量。我以后不会再提及环境了。)

e path is /etc/puppet inside that directory, you have a directory manifests . 如果该目录下的路径是/ etc / puppet ,则您有目录清单 There is the place for your sites.pp 有您的sites.pp的地方

usually a sites.pp structure is: 通常,sites.pp结构为:

node default {
  include *module*
  include *module2*
}

node /server\.fqdn\.local/ {
  include *module2*
  include *module3*
}

this means that you have a default Node (if the node name doesn't fit any other node, will use the default, otherwise it will use the regex matching of the node FQDN in this case server.fqdn.local . 这意味着您有一个默认节点(如果节点名称不适合其他任何节点,将使用默认节点,否则将使用节点FQDN的正则表达式匹配,在这种情况下为server.fqdn.local)

The modules (module, module2 and module3) are stored inside the $modulespath set on your puppet.conf. 模块(模块,模块2和模块3)存储在puppet.conf上设置的$ modulespath中。 In our case i will use the: /etc/puppet/modules 在我们的情况下,我将使用: / etc / puppet / modules

the tree will look like: 这棵树看起来像:

/etc/puppet/modules/
/etc/puppet/modules/module/
/etc/puppet/modules/module/manifests/
/etc/puppet/modules/module/manifests/init.pp
/etc/puppet/modules/module2/
/etc/puppet/modules/module2/manifests/
/etc/puppet/modules/module2/manifests/init.pp
/etc/puppet/modules/module3/
/etc/puppet/modules/module3/manifests/
/etc/puppet/modules/module3/manifests/init.pp

About classes: https://docs.puppetlabs.com/puppet/latest/reference/lang_classes.html generally what i explained but from puppet labs: https://docs.puppetlabs.com/puppet/latest/reference/dirs_manifest.html 关于课程: https : //docs.puppetlabs.com/puppet/latest/reference/lang_classes.html通常我解释过的内容但来自puppet实验室: https : //docs.puppetlabs.com/puppet/latest/reference/dirs_manifest.html

Please note that the example from the README 请注意, 自述文件中的示例

class { 'accounts':
  ssh_keys   => hiera_hash('accounts::ssh_keys', {}),
  users      => hiera_hash('accounts::users', {}),
  usergroups => hiera_hash('accounts::usergroups', {}),
}

is catering to users of Puppet versions before 3.x which had no automatic parameter lookup . 适用于3.x之前没有自动参数查找功能的Puppet版本的用户。 With a recent version, you should just use this manifest: 对于最新版本,您应该只使用以下清单:

include accounts

Since the Hiera keys have appropriate names, Puppet will look them up implicitly. 由于Hiera键具有适当的名称,因此Puppet会隐式查找它们。

This whole thing still makes no sense to me. 这整个事情对我来说仍然毫无意义。 Since I have to put 由于我不得不把

accounts::account
{
    'admin':
}

in a manifest file to create that user, what for is hiera useful in this case? 在清单文件中创建该用户时,hiera在这种情况下有用吗? It doesn't separate data from logic. 它不会将数据与逻辑分开。 I have data in both, the .yaml file (ssh keys, other account data) and in a manifest file (the snippet above). 我在.yaml文件(ssh密钥,其他帐户数据)和清单文件(上面的代码段)中都有数据。 By using hiera I expect to be able to create that user inside /etc/puppet/hieradata/node/node1.example.com.yaml but this is not the case. 通过使用hiera,我希望能够在/etc/puppet/hieradata/node/node1.example.com.yaml中创建该用户,但事实并非如此。 What is the right way to do this? 什么是正确的方法? What for is the example hiera file of this module useful? 此模块的示例hiera文件有什么用? Wouldn't it be easier create an account the old style way in site.pp? 用site.pp中的旧样式创建帐户会更容易吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM