3

我正在尝试生成一个名为domain的自定义事实。这个想法是列出其中的所有目录,但删除一些默认目录,例如, , 。/homecentosec2-usermyadmin

我正在使用 bash,因为我不知道 ruby​​。到目前为止,我的脚本将列表输出到一个 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,一些方法。

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

凯文

4

2 回答 2

1

我也不熟悉红宝石,但我有一些解决方法的想法:

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

Facter.add(:domain_array) do
  setcode do
  domains = Facter.value(:domains)
  domain_array = domains.split(',')
  domain_array
  end
end
于 2015-05-07T11:16:25.953 回答
1

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

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

注意:解析器函数只能在 puppet master 中编译。有关将在代理上运行的自定义事实,请参见下文。

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]
于 2015-05-07T19:27:26.493 回答