Querying system configuration

11 March 2009

One of the nice things about having your configuration data in a uniform data structure like Augeas’ tree is that you can query it in a uniform way. A couple of releases ago, I added support for querying the tree with XPath — it was surprising, even to me, how easy some tasks of finding your way through config files has become with that. And there’s no need to worry whether the file is a inistyle file, stores one record per line, or contains shell variables.

To whet your appetite, here are a few examples; if you want to play along at home, you can just type them into augtool (you need Augeas 0.4.2 for them to work properly). As a simple example, let’s look up the canonical name of a host with a given IP address:

  augtool> match "/files/etc/hosts/*/canonical[../ipaddr = '127.0.0.1']"
  /files/etc/hosts/1/canonical = localhost.localdomain

A little more complicated is listing all the enabled yum repos that do not have gpgcheck turned on:

  augtool> match "/files/etc/yum.repos.d/*/*[enabled = '1'][gpgcheck = '0']"
    (no matches)

In a similar vein, we can find all the bridges that are configured to come up at boot (for readability, I’ll write $net for the string /files/etc/sysconfig/network-scripts):

  augtool> match "$net/*[TYPE = 'Bridge' and ONBOOT = 'yes']/DEVICE"
  /files/etc/sysconfig/network-scripts/ifcfg-br0/DEVICE = br0

While that is still fairly easy to do with a one-liner from the shell, finding all the network interfaces that get enslaved into such a bridge using grep or awk requires a small amount of head scratching; as a path expression it’s lengthy but straightforward (again, for readability, I’ll write $bridge for the path expresion above):

  augtool> match "$net/*[$bridge = BRIDGE]/DEVICE"
  /files/etc/sysconfig/network-scripts/ifcfg-eth0/DEVICE = eth0

Creative Commons License Watzmann.Blog by David Lutterkort is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.

Generated with Jekyll