Using Augeas with Vagrant shell provisioning
Augeas, Bash, Vagrant | September 12, 2015
I'm a big fan of simple. And I prefer Bash for provisioning Vagrant boxes. I admit that sometimes changing configuration files is not that easy with sed, awk, echo, ... or at least requires reading docs to figure out how it works. So, I decided to replace some of those parts with Augeas.
Augeas is a configuration editing tool. It knows how to parse various configuration files and transforms them into a tree. The tree then can be easily manipulated and saved. This has the additional benefit of making the bash scripts more idempotent. Augeas is also used by the Puppet configuration management system.
Install Augeas on Debian based systems with apt-get install augeas-tools, and run augtool.
To give you one example, I'm going to change the value of date.timezone in the PHP configuration.
augtool> print /files/etc/php5/cli/php.ini ... /files/etc/php5/cli/php.ini/Date/date.timezone = "Europe/London" ...
Change (or set) the date.timezone:
augtool> set /files/etc/php5/cli/php.ini/Date/date.timezone Europe/Brussels augtool> save Saved 1 file(s)
$ php -i | grep date.timezone date.timezone => Europe/Brussels => Europe/Brussels
Using with Vagrant shell provisioning
It's possible to just use augtool from the commandline, so it's easy to just use it in a Bash script. An example:
$ sudo augtool set /files/etc/php5/cli/php.ini/Date/date.timezone Europe/Brussels
In our free online Secret Santa gift exchange organizer, I have more examples of how this can be used with Vagrant shell provisioning. But like all tools, use Augeas only when it adds value. For example, in my opinion:
sed -i '/# Alias \/roundcube/s/#//' /etc/apache2/conf.d/roundcube
is still a lot simpler than
augeas << EOF ins directive before /files/etc/apache2/conf.d/roundcube/Directory set /files/etc/apache2/conf.d/roundcube/directive Alias set /files/etc/apache2/conf.d/roundcube/directive/arg /roundcube set /files/etc/apache2/conf.d/roundcube/directive/arg /var/lib/roundcube save EOF
Augeas can't uncomment entries and it's not always easy to add new entries. If you want to know more about Augeas I suggest you read the docs.