Accessing Specific Chunk of Aggregate Fact

52 views
Skip to first unread message

Steve Harp

unread,
Feb 6, 2015, 3:13:21 PM2/6/15
to puppet...@googlegroups.com
Hi Guys,

I have a json configuration file on each Puppet Agent that I need to parse and return the values as custom facts.  Each json file will contain an array of from 0 to n elements each of which will have needed configuration settings.  I think what I need to write is an aggregate fact where the specific elements in the array will be represented as chunks.  Here's a simplified example of the json file.

{
        "Sites":
        [
                {
                        "WEB_ID": "0006",
                        "BACKUP_LIVE": "True",
                        "BACKUP_TEST": "False",
                        "LIVE_DIR": "/srv/www/live-0006.net",
                        "TEST_DIR": "/srv/www/test-0006.net",
                },
                {
                        "WEB_ID": "0145",
                        "BACKUP_LIVE": "True",
                        "BACKUP_TEST": "False",
                        "LIVE_DIR": "/srv/www/live-0145.net",
                        "TEST_DIR": "/srv/www/test-0145.net"
                },
                {
                        "WEB_ID": "0000",
                        "BACKUP_LIVE": "True",
                        "BACKUP_TEST": "False",
                        "LIVE_DIR": "/srv/www/live-0000.net",
                        "TEST_DIR": "/srv/www/test-0000.net"
                }
        ]
}

I don't think writing the fact would be too difficult but I don't know how to reference the facts from within a manifest on the Puppet Master.  I would like to be able to access these settings using a custom fact from within a manifest.  Something like:

        value=Sites[2]["LIVE_DIR"] 

and have the variable, value, set to '/srv/www/test-0000.net'.

If you have an aggregate fact, how to you use it in a manifest?

Is this possible?

Thanks.....

jcbollinger

unread,
Feb 9, 2015, 9:13:17 AM2/9/15
to puppet...@googlegroups.com


On Friday, February 6, 2015 at 2:13:21 PM UTC-6, Steve Harp wrote:
Hi Guys,

I have a json configuration file on each Puppet Agent that I need to parse and return the values as custom facts.  Each json file will contain an array of from 0 to n elements each of which will have needed configuration settings.  I think what I need to write is an aggregate fact where the specific elements in the array will be represented as chunks.


The Puppet term for what you appear to want is a "structured fact".  The Facter documentation contains a section on writing structured facts.

 
I don't think writing the fact would be too difficult


Probably not, supposing you have a JSON parser in your local Ruby lib.  Such a parser probably produces an appropriate fact value in about one line.

 
but I don't know how to reference the facts from within a manifest on the Puppet Master.  I would like to be able to access these settings using a custom fact from within a manifest.  Something like:

        value=Sites[2]["LIVE_DIR"] 

and have the variable, value, set to '/srv/www/test-0000.net'.

If you have an aggregate fact, how to you use it in a manifest?


Referencing facts is no different from referencing any other Puppet variable.  If the name of the fact were "sites" (should be lowercase) and its value were structured as an array of hashes, then you would use

$value = $sites[2]["LIVE_DIR"]

(The Puppet documentation describes the syntax for referencing array and hash elements.


John

Reply all
Reply to author
Forward
0 new messages