3

domainというカスタムファクトを生成しようとしています。アイデアは、内のすべてのディレクトリをリストすることですが、、、などのデフォルトのディレクトリを削除します。/homecentosec2-usermyadmin

ruby がわからないので bash を使っています。これまでのところ、私のスクリプトはリストをtxtファイルに出力し、それから要因の答えを見つけます。しかし、配列のように複数ではなく、1つの長い回答として扱われますか?

私のスクリプトは次のとおりです。

#!/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

これを行うためにパーサー関数を配置できます。パーサー関数は内部に入ります:

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

注: パーサー関数は、puppet マスターでのみコンパイルされます。エージェントで実行されるカスタム ファクトについては、以下を参照してください。

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 に答える
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 に答える