I use CFengine for automation of different taks like copying config files from one place to nodes, reloading services after this, etc. And recently i configured it to check and install packages for different boxes depending on their role. For example, if box acts as web node, following packages need to be installed: apache2, apache2-mpm-prefork, libapache2-mod-php5, etc
So i added into control: section following lines:
debian|ubuntu::
DefaultPkgMgr = ( dpkg )
DPKGInstallCommand = ( "/usr/bin/apt-get -y install %s" )
DPKGRemoveCommand = ( "/usr/bin/apt-get -y purge %s" )
gentoo::
DefaultPkgMgr = ( portage )
PortageInstallCommand = ( "/usr/bin/emerge --nocolor %s" )
centos::
DefaultPkgMgr = ( rpm )
DPKGInstallCommand = ( "/usr/bin/yum -y install %s" )
Not sure about gentoo/centos though. And after that, listed required packages in packages: section:
nodes.(debian|ubuntu)::
apache2 action=install
apache2-mpm-prefork action=install
libapache2-mod-geoip action=install
libapache2-mod-php5 action=install
php5 action=install
php5-curl action=install
...
Now cfengine should install packages if box is either ubuntu or debian and class nodes is defined. But there's one little problem: if you copy config file for some of those packages, or for some reason it resides on the box, apt-get/dpkg will ask you what to do with old/new config (use old or install new version). And '-y' option won't help in this situation.
I noticed that packages use ucf - Update Configuration File. And this tool respects following
variables:
UCF_FORCE_CONFFNEW - if set, forces the new file to always overwrite the installed destination file
UCF_FORCE_CONFFOLD - if set silently retains the installed file
So, i changed definition of DPKGInstallCommand:
DPKGInstallCommand = ( "/usr/bin/env UCF_FORCE_CONFFOLD=1 /usr/bin/apt-get -y install %s" )
And it worked. But as i found later, not all packages seem use ucf for handling config files. So the final version of DPKGInstallCommand looks like this:
DPKGInstallCommand = ( "/usr/bin/env UCF_FORCE_CONFFOLD=1 /usr/bin/apt-get -y -o 'Dpkg::Options::=' '--force-confold' install %s" )