自定义事实与主目录作为木偶的域



我正在尝试生成一个自定义 fact 称为 domains 。这个想法是列出/home中的所有目录,但请删除一些默认目录,例如centosec2-usermyadmin

我不知道Ruby,我正在使用bash。到目前为止,我的脚本将列表输出到一个TXT文件中,然后将其猫归因于因子的答案。但是它被视为一个长答案,而不是一个数组?

我的脚本如下:

#!/bin/bash
ls -m /home/ | sed -e 's/, /,/g' | tr -d 'n' > /tmp/domains.txt  
cat /tmp/domains.txt | awk '{gsub("it_support,", "");print}'| awk  '{gsub("ec2-user,", "");print}'| awk '{gsub("myadmin,", "");print}'| awk  '{gsub("nginx", "");print}'| awk '{gsub("lost+found,", "");print}' >  /tmp/domains1.txt
echo "domains={$(cat /tmp/domains1.txt)}"
exit

Foremans将我的域视为

facts.domains = "{domain1,domain2,domain3,domain4,lost+found,}"

我还需要删除lost+found,一些方法。

任何帮助或建议将不胜感激

凯文

我也不熟悉Ruby,但我有一个解决方法的想法:

请查看以下有关返回网络接口数组的示例。现在创建 domain_array 事实使用以下代码:

Facter.add(:domain_array) do
  setcode do
  domains = Facter.value(:domains)
  domain_array = domains.split(',')
  domain_array
  end
end

您可以放置一个解析器函数来执行此操作。解析器功能进入:

 modules/<modulename>/lib/puppet/parser/functions/getdomain.rb

注意:解析器函数仅在木偶大师中编译。有关将在代理商上运行的自定义事实,请参见下文。

getdomain.rb可以包含以下目的类似的内容:

module Puppet::Parser::Functions
  newfunction(:getdomain, :type => :rvalue) do |args|
    dnames=Array.new
    Dir.foreach("/home/") do |d|
      # Avoid listing directories starts with . or ..
      if !d.start_with?('.') then
        # You can put more names inside the [...] that you want to avoid
        dnames.push(d) unless ['lost+found','centos'].include?(d)
      end
    end
    domainlist=dnames.join(',')
    return domainlist
 end
end

您可以从清单中调用它并分配到变量:

$myhomedomains=getdomain()

$myhomedomains应该返回与此类似的东西: user1,user2,user3

  .......

对于具有类似代码的自定义事实。您可以将其放入:

 modules/<modulename>/lib/facter/getdomain.rb

getdomain.rb的内容:

Facter.add(:getdomain) do
  setcode do
    dnames=Array.new
    Dir.foreach("/home/") do |d|
      # Avoid listing directories starts with . or ..
      if !d.start_with?('.') then
        # You can put more names inside the [...] that you want to avoid
        dnames.push(d) unless ['lost+found','centos'].include?(d)
      end
    end
    getdomain=dnames.join(',')
    getdomain
  end
end

您可以在任何清单中调用getdomain事实,例如,从同一模块的init.pp称呼它:

 notify { "$::getdomain" : }

将产生类似的东西:

Notice: /Stage[main]/Testmodule/Notify[user1,user2,user3]

最新更新