Bolt plans

If Puppet tasks are our imperative resources, Puppet plans are our Puppet manifests. Here, we combine multiple tasks and commands to form an orchestrated plan. These plans are written in the same DSL as Puppet code, although at the time of writing this book, only puppet functions can be used, and not many objects like resources or class are included.

In our sample plan, we're going to introduce two parameters: 

  • $enterprise: This is used to determine if pe-console-services should be checked in the plan (it is possible to use facts from the target or PuppetDB as well)
  • $servers: This is a list of servers that's passed as a comma-separated list

Our task will clean up any existing stored logs and build a fresh set. This script will run the log scraper task we built in the last section for each section, and aggregate all the logs together. Enterprise, as an optional flag, will determine if pe-console-services.log is included as well. After we've built the log, we'll simply read the log file and ensure that it is returned to the command line with the return function. Finally, we'll clean up after ourselves and clean the aggregated log we just built in /tmp:

# logs/plans/puppetserver.pp
plan logs::puppetserver (
Boolean $enterprise,
TargetSpec $servers,
) {

run_command('rm -f /tmp/puppetlog.log', $servers)
run_task('logs::puppetserver', $servers, log => 'puppetserver', store => true)
run_task('logs::puppetserver', $servers, log => 'puppetdb', store => true)

if $enterprise == true {
run_task('logs::puppetserver', $servers, log => 'console', store => true)
}

return run_command('cat /tmp/puppetlog.log', $servers)
run_command('rm -f /tmp/puppetlog.log', $servers)

}

Once we've built our plan, we can run bolt plan run, passing our modulepath and parameters:

rary at Ryans-MacBook-Pro-3 in ~/workspace/packt/logs
$ bolt plan run logs::puppetserver --modulepath .. --no-host-key-check enterprise=false servers=root@pe-puppet-master
Starting: plan logs::puppetserver
Starting: command 'rm -f /tmp/puppetlog.log' on root@pe-puppet-master
Finished: command 'rm -f /tmp/puppetlog.log' with 0 failures in 0.38 sec
Starting: task logs::puppetserver on root@pe-puppet-master
Finished: task logs::puppetserver with 0 failures in 0.39 sec
Starting: task logs::puppetserver on root@pe-puppet-master
Finished: task logs::puppetserver with 0 failures in 0.45 sec
Starting: command 'cat /tmp/puppetlog.log' on root@pe-puppet-master
Finished: command 'cat /tmp/puppetlog.log' with 0 failures in 0.15 sec
Finished: plan logs::puppetserver in 1.39 sec
[
{
"node": "root@pe-puppet-master",
"status": "success",
"result": {
"stdout": "puppetserver ============ 2018-09-23T00:20:54.905Z INFO [qtp417202273-69] [puppetserver] Puppet 'replace_facts' command for pe-puppet-master submitted to PuppetDB with UUID fc691079-debf-4c99-896b-3244f353a753 2018-09-23T00:20:55.268Z ERROR [qtp417202273-69] [puppetserver] Puppet Could not find node statement with name 'default' or 'pe-puppet-master' on node pe-puppet-master ...",
"stderr": "",
"exit_code": 0
}
}
]

You may notice that the log comes back as a big JSON object, with no line breaks represented. If you want to view this aggregated log file for yourself, try running the following command and inspecting the new puppetlog.log file:

$ rm -f *.log;bolt plan run logs::puppetserver --modulepath .. --no-host-key-check enterprise=false servers=root@pe-puppet-master > compressed.log; head -n 6 compressed.log | tail -n 1 | awk '{gsub("\\n","
")};1' > puppetlog.log
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.230.107