【问题标题】:Custom fact with home directorys as domains for puppet将主目录作为 puppet 域的自定义事实
【发布时间】:2015-07-17 21:49:52
【问题描述】:

我正在尝试生成一个名为 domains 的自定义 fact。 想法是列出/home 中的所有目录,但删除一些默认目录,例如centosec2-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, 一些方法。

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

凯文

【问题讨论】:

    标签: bash puppet theforeman facter


    【解决方案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
    

    【讨论】:

      【解决方案2】:

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

       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]
      

      【讨论】:

      • 这将在 puppet master 上运行,这是@Kevin 要求的吗?
      猜你喜欢
      • 2018-12-03
      • 2017-10-11
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 2021-12-17
      相关资源
      最近更新 更多