家庭导演作为木偶领域的自定义事实

时间:2015-05-07 10:57:17

标签: bash puppet theforeman facter

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

我使用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,一些方法。

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

凯文

2 个答案:

答案 0 :(得分:1)

我也不熟悉ruby,但我对某些解决方法有所了解:

请查看以下有关returning an array of network interfaces的示例。现在要创建 domain_array ,请使用以下代码:

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

答案 1 :(得分: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]