User fact with home directories as domains for dolls

I am trying to create a custom fact called domains. The idea is to list all of the directories in /home

, but remove some default directory, for example centos

, ec2-user

, myadmin

.

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

My script looks like this:

#!/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 delete lost+found,

somehow.

Any help or advice would be appreciated

Kevin

+3


source to share


2 answers


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

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



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

      

+1


source


You can put a parser function for this. Parser functions come in:

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

      

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

getdomain.rb

may 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 manifest and assign it 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 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 fact getdomain

in any manifest, for example, by calling it from the same module init.pp

:

 notify { "$::getdomain" : }

      

will result in something like this:

Notice: /Stage[main]/Testmodule/Notify[user1,user2,user3]

      

+1


source







All Articles