On 1/13/17 4:15 PM, James Perry wrote: > After spending most of they day digging around and researching, I find > Puppet's immutable variables are keeping me from properly handling what > I'm trying to do, so I want to see if anyone else has some suggestions > on how to handle was I need to accomplish. > > Goal: Ingest a CSV file provided by a user and generate a start / stop > script, dynamically, for every server in scope, based on CSV file. > > CSV Format: > SERVER,start command > > Example. > SERVERA, /usr/local/bin/prog start databasea > SERVERA, /usr/local/bin/prog start databaseb > SERVER1, /usr/local/bin/prog start database123 > > > The basic design I had in mind for the manifest is to: > 1. Read in the file as provided, > 2. Convert <A>,<B> to downcase(A) => B > 3. if $hostname == A > $my_server_script_lines = $my_hash[A][B] > file { > ... > content => template("basic_start_script"), > } > > 4. Create a template that runs through the $my_server_script_lines to > put each start line under start) and under stop) after doing a substr > replacement of start for stop in B. > > Code so far > include stdlib > $my_data = file("/home/me/database.csv") > $my_subst = downcase(split($my_data2,'[,\n]')) > $my_hash = hash($my_subst) > > notice ($my_hash[SERVERA]) > > $ puppet apply --verbose test.pp > Info: Loading facts > *Notice: Scope(Class[main]): '/usr/local/bin/prog start databaseb'* > Notice: Compiled catalog for myhost.net in environment production in > 0.16 seconds > Info: Applying configuration version '1484340247' > Notice: Applied catalog in 0.03 seconds > > Here are the values of the variables as it processes through > > $my_data = "SERVERA,/usr/local/bin/prog start databasea > SERVERA,/usr/local/bin/prog start databaseb > SERVERB,/usr/local/bin/prog start database123" > > $my_subst = [servera, '/usr/local/bin/prog start databasea' , servera, > '/usr/local/bin/prog start databaseb' , serverb, '/usr/local/bin/prog > start database123' ] > > $my_hash = {servera => '/usr/local/bin/prog start databaseb' , serverb > => '/usr/local/bin/prog start database123' } > > So I already know why the hash conversion dropped the "start databasea" > for the servera key, what I can't seem to figure out is how to have it > convert into a array of value pairs for a specific key. > > { servera => ['/usr/local/bin/prog start > databasea', '/usr/local/bin/prog start databaseb'], serverb => > ['/usr/local/bin/prog start database123'] } > > I tried various iterations of .each to try to create and fill the array > pointed to by the hash, but Puppet doesn't permit that as it would be > changing an already assigned variable / hash. > > I was able use the $my_subst variable in an erb template to create the > start/stop lines. It worked ok for the 3 line example above, but when I > got to dozens of servers / start lines being applied to hundreds of > servers on each check-in it soon killed the CPU in my master server as > it ran through a loop checking if $hostname == servername. > > Is it possible to have Puppet handle parsing the data in $my_substr, or > even right from the raw file data to do the following? > 1. Run through incoming data to fill start command array. > ['/usr/local/bin/prog start databasea', '/usr/local/bin/prog start > databaseb'] > 2. Assign that to the array of key-pairs. { servera => > ['/usr/local/bin/prog start databasea', '/usr/local/bin/prog start > databaseb'], serverb => ['/usr/local/bin/prog start database123'] } > > Thanks! >
Hi James, One approach would be to not do it within a puppet manifest and instead transform that data with a language you are familiar with and have it write to its own file in Hiera as YAML or JSON. Once the data structure is there, you can use the create_resources() function to create the resources from the data in Hiera. Another approach would be to write a custom function or ENC that uses your CSV as the data store and for a given server respond with the start command. If you are not familiar with ruby, the custom ENC would be easier, since it can be in any language. Instead of a CSV, you might want to look at Consul which can host key/value pairs for you. You can then query it to see which databases are associated with a given server. Best regards, -g -- Garrett Honeycutt @learnpuppet Puppet Training with LearnPuppet.com Mobile: +1.206.414.8658 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/fd4a21e3-1820-ace5-36f9-407a5d980c20%40garretthoneycutt.com. For more options, visit https://groups.google.com/d/optout.