Infrastructure – News

New facts from configuration parameters

5 December 2012

On the #augeas IRC channel, it is common to get questions such as:

“How can I append to the value of this configuration parameter using Augeas in Puppet?”

The answer is invariably:

“This is not directly possible, you’d need to either use a Ruby provider or define a fact with the value, and use this fact in your Puppet recipes. You can actually use the Augeas Ruby bindings to write the fact.”

While the first option is being taken care of in the Augeasproviders project, which implements alternative native providers (and often new types) using the Augeas Ruby bindings, you would still have to write a fact in Ruby if you simply wanted to retrieve a configuration parameter to use it in your recipes. Well, not anymore.

The Augeasfacter module aims to make it as easy as possible to define new facts from single configuration parameters, by using Augeas as a parser.

Installing it in Puppet (2.7.14+) is as easy as:

puppet module install camptocamp/augeasfacter

The plugin makes use of pluginsync to deploy itself and its configurations, so there is no need to write any bit of Puppet code to get it to work.

Say, for example, that you want a fact returning the shell for the root user. In the module of your choice, simply add a `lib/augeasfacters/root_shell.conf` file containing:

path = /files/etc/passwd/root/shell

That’s it. The configuration file will get synced via pluginsync and the augeasfacter Facter plugin will use it to declare a new fact called root_shell, containing the value of the /files/etc/passwd/root/shell node in the Augeas tree.

Another example? What if you wanted a fact returning the filesystem type of / as seen in /etc/mtab? You could add a file containing:

path = /files/etc/fstab/*[file='/']/vfstype

Sometimes you might want to get the label of a node instead of its value. For example, if you wanted the name of the user with uid 1000. You could use method = label (the default being “method = value“) for that:

path = /files/etc/passwd/*[uid='1000']
method = label

In some other cases you would like to return multiple values with a separator (since Facter doesn’t support arrays). You can use type = multiple (the default being “type = single“) for that, and optionally define the sep parameter (the default being “,“):

path = /files/etc/passwd/*
type = multiple
method = label
sep = :

Finally, you might want to query from a file that Augeas doesn’t know about (because it is not in a standard location). You can then manually map the file to a lens, using the lens and incl parameters as you would in the augeas Puppet type:

path = /files/usr/share/myapp/example.json/dict/entry[8]
lens = Json.lns
incl = /usr/share/myapp/example.json

The above example will parse /usr/share/myapp/example.json using the Json.lns Augeas lens, and return the value of dict/entry[8] in this file as the json_entry fact in Facter.

Comments, bug reports, patches are most welcome!

Leave a comment

Your email address will not be published. Required fields are marked *