3

I'm trying to generate a custom fact called domains. the idea is to list all the directories within /home but remove some default directory's such as centos, ec2-user, myadmin.

I'm using bash as I don't know ruby. so far my script outputs the list into a txt file which it then cats the answer for factors. but it is treated as one long answer and not multiple like an array?

My script is as follows:

#!/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 sees my domains as

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

I also need to remove lost+found, some how.

Any help or advice would be appreciated

Kevin

Chris Pitman
  • 12,990
  • 3
  • 41
  • 56
Kevin
  • 123
  • 2
  • 10

2 Answers2

1

I'm also not familiar with ruby, but I have an idea for some workaround:

Please look at the following example about returning an array of network interfaces. Now to create domain_array fact use the following code:

Facter.add(:domain_array) do
  setcode do
  domains = Facter.value(:domains)
  domain_array = domains.split(',')
  domain_array
  end
end
kkamil
  • 2,593
  • 11
  • 21
1

You can put a parser function to do this. Parser functions go inside:

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

Note: Parser function will compile only in the puppet master. See below for a custom fact that will run on the agent.

getdomain.rb can contain something like the following for your purpose:

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

You can call it from a manifest and assign to a variable:

$myhomedomains=getdomain()

$myhomedomains should return something similar to this : user1,user2,user3

  .......

For a custom fact with similar code. You can put it in :

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

Content of 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

You can call the getdomain fact in any manifest, for example, calling it from the same module's init.pp :

 notify { "$::getdomain" : }

will result in something similar :

Notice: /Stage[main]/Testmodule/Notify[user1,user2,user3]
iamauser
  • 11,119
  • 5
  • 34
  • 52