[Danny Berger](https://dpb587.me/ "Home")

# Using Facter in Ant Scripts

February 19, 2013

After using [puppet](https://puppetlabs.com/puppet/what-is-puppet/) for a while I have become use to some of the facts that [facter](https://puppetlabs.com/puppet/related-projects/facter/) automatically provides. When working with [ant](http://ant.apache.org/) 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.

## Reader Comments

Copyright © 2026 // [dpb587.me](https://dpb587.me/) is a [personal](https://dpb587.me/projects/website), [open source](https://github.com/dpb587/dpb587.me/blob/main/content/post/2013/using-facter-in-ant-scripts-20130219.md) site.
