Using Facter in Ant Scripts

February 19, 2013

After using puppet for a while I have become use to some of the facts that facter automatically provides. When working with ant build scripts, I started wishing I didn't have to generate similar facts myself through various exec calls.

One Fact

Instead of fragile lookups like...

<exec executable="/bin/bash" outputproperty="lookup.eth0">
    <arg value="-c" />
    <arg value="/sbin/ifconfig eth0 | grep 'inet addr' | awk -F: '{print $2}' | awk '{print $1}'" />
</exec>

I can simplify it with...

<exec executable="/usr/bin/facter" outputproperty="lookup.eth0">
    <arg value="ipaddress_eth0" />
</exec>

In Bulk

Or I can load all facts with...

<tempfile property="tmp.facter.properties" deleteonexit="true" />
<exec executable="/bin/bash" output="${tmp.facter.properties}" failonerror="true">
    <arg value="-c" />
    <arg value="/usr/bin/facter -p | /bin/sed -e 's/ => /=/'" />
</exec>
<property file="${tmp.facter.properties}" prefix="facter" />

And reference a fact in my task...

<exec executable="${basedir}/bin/configure-env">
    <arg value="--set-listen" />
    <arg value="${facter.ipaddress_eth0}" />
</exec>

Summary

So now it's much easier to reference environment information from property files (via interpolation), make targets more conditional, and, of course, within actual tasks.