Task

Our task will be a simple shell script that reads a named file based on our input parameters, makes a decision on whether or not to store the output, and then returns the output as JSON to Bolt. It's important that our return comes back as JSON so that it can be picked up by Bolt. In more complex use cases, we could even use this JSON to pass key value pairs to a follow-on task in plans, which we'll cover in the next section.

A task can be written in any language available to the system. This example will use Bash, as nearly every administrator has worked with it. If you haven't tried writing scripts in Python, Ruby, Golang, or any other scripting language outside of shell, give it a shot. These tasks actually become easier to write in these more advanced languages.

There are a few things worth noting in our shell script:

  • Values returned from our JSON parameters file become environment variables, and start with PT_. Our script refers to $PT_log and $PT_store to check the values that will be sent over the command line.
  • We're using a case statement to map $PT_log to a log file. This use is similar to a selector statement in Puppet.
  • If $PT_store is true, we'll build a log file that can be appended to.
  • The log is printed out in the final line as JSON so that Puppet Tasks knows it is a valid output to the command line:
# logs/tasks/puppetserver.sh
#!/bin/sh

# Map $PT_log to a $logfile variable
case "$PT_log" in
'console') logfile='/var/log/puppetlabs/console-services/console-services.log' ;;
'puppetdb') logfile='/var/log/puppetlabs/puppetdb/puppetdb.log' ;;
'puppetserver') logfile='/var/log/puppetlabs/puppetserver/puppetserver.log' ;;
esac

# Variable that stores all the text from inside the logfile
log=`cat $logfile`

# If store is true, build a header and then print out $log
if [ $PT_store == 'true' ]
then
echo "${PT_log} ============" >> /tmp/puppetlog.log
echo $log >> /tmp/puppetlog.log
fi

# print out the key value of "<chosen log>":"all log contents" in JSON to be
# read by the Bolt interpreter
echo -e "{'${PT_log}':'$log'}"

Let's double check that the files we've written are in the proper location before we run our command:

logs
├── files
├── manifests
├── tasks
│ ├── puppetserver.json
│ └── puppetserver.sh
└── templates

We can then run our command on the command line. We've added some parameters that help along the way:

  • nodes: This determines which nodes based on our inventory file to run on.
  • modulepath: Where to look for modules. Because we're working on this module directly, we've just set the modulepath to the directory above the module.
  • --no-host-key-check: You may not need this, but to ease troubleshooting of SSH in this section, we'll use this flag.
  • log=puppetdb: This is the parameter which we wrote in our JSON file. It will be transformed into $PT_log and used in our shell script:
$ bolt task run logs::puppetserver --nodes puppetserver --modulepath .. log=puppetdb --no-host-key-check

Started on pe-puppet-master.puppet.net...
Finished on pe-puppet-master.puppet.net:
{'puppetdb':'2018-09-23T00:20:55.115Z INFO [p.p.command] [8-1537662054876] [212 ms] 'replace facts' command processed for pe-puppet-master
2018-09-23T00:21:12.077Z INFO [p.p.command] [9-1537662071679] [370 ms] 'store report' puppet v5.5.2 command processed for pe-puppet-master
2018-09-23T00:21:53.936Z INFO [p.p.c.services] Starting sweep of stale nodes (threshold: 7 days)
...'}
{
}
Successful on 1 node: pe-puppet-master.puppet.net
Ran on 1 node in 0.89 seconds

Try the command out for yourself. It will return a different log file for each command, and if you pass store=true, it will even start appending this log to a file in /tmp named 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.191.181.231