Setting External Facts using Tasks

Setting External Facts using Tasks

Written: 2017-10-19
Author: WhatsARanjit
Links:

The problem

We’re taking a brownfield architecture. We decide that when we provision systems, we’re going to drop a file at /etc/puppetlabs/facter/facts.d/datacenter.txt that contains datacenter=rhode_island. We will use this fact to drive classification. This is easy to do for new systems, but how do we add this external fact to existing systems?

The fix

We can use Puppet Bolt or PE Tasks to get this done. Using WhatsARanjit/puppeteer, we can uses Tasks to write facts on existing nodes.

PE - adding datacenter fact

puppet task run puppeteer::external_fact --nodes 'node1' fact=datacenter value=rhode_island

This will by default created a file at /etc/puppetlabs/facter/facts.d/datacenter.txt or C:\ProgramData\PuppetLabs\facter\facts.d\datacenter.txt and add the content datacenter=rhode_island. If you’d like to add the datacenter fact in an existing file or a specific name in general:

puppet task run puppeteer::external_fact --nodes 'node1' fact=datacenter value=rhode_island file=config.txt

This will instead use /etc/puppetlabs/facter/facts.d/config.txt or C:\ProgramData\PuppetLabs\facter\facts.d\config.txt as the file containing the fact. If the fact already exists in that file, it will be overwritten. When you specify a JSON or YAML target, the Task will know to interpret the target file as such. Existing keys will be overwritten, not merged. For example if the file at /etc/puppetlabs/facter/facts.d/foo.yaml already contains:

---
foo: bar

…and you issue:

puppet task run puppeteer::external_fact --nodes 'node1' fact=foo value=hello file=foo.yaml

/etc/puppetlabs/facter/facts.d/foo.yaml will be updated to:

---
foo: hello

Bolt - adding datacenter fact

Running the task through Bolt is very similar:

bolt task run puppeteer::external_fact --nodes 'node1' fact=datacenter value=rhode_island

PE - removing datacenter fact

When you wish to remove an existing external fact, you can make use the of action parameter, which is set to add by default:

puppet task run puppeteer::external_fact --nodes 'node1' fact=datacenter action=remove

…the task will specifically remove a fact foo from foo.yaml if it exists. The task will ouput information like whether the operation status resulted in unchanged, changed, or created. It will also include the previous and new values.

Bolt - removing datacenter fact

Running the task through Bolt is very similar:

bolt task run puppeteer::external_fact --nodes 'node1' fact=datacenter action=remove