There are two main approaches when managing a file from Puppet:
Manage the file in parts:
host
, mailalias
, augeasproviders resource types, etc.)concat
resourceaugeas
resourcefile_line
resourceManage the file in its entirety:
file
resource with content (template, variables)file
resource with source (using fileserver or local file)One case that is not possible with these is to:
The augeas_file
type allows to do that. It typically allows to use
an example configuration file provided by a system package
and tune it to create a configuration file, idempotently.
You can use augeas_file
to manage a file entirely with one resource, listing
Augeas changes in the changes
parameter:
# augeas_file doesn't manage file rights
# use the file resource type for that
file { '/etc/apt/sources.list.d/jessie.list':
ensure => file,
owner => 'root',
group => 'root',
}
augeas_file { '/etc/apt/sources.list.d/jessie.list':
lens => 'Aptsources.lns',
base => '/usr/share/doc/apt/examples/sources.list',
changes => ['setm ./*[distribution] distribution jessie'],
}
You might want to wrap this in a defined resource type:
define apache::userdir (
$directory,
) {
file { $title:
ensure => file,
owner => 'www-data',
group => 'root',
}
augeas_file { $title:
base => '/usr/share/doc/apache2.2-common/examples/apache2/extra/httpd-userdir.conf',
lens => 'Httpd.lns',
changes => [
"set Directory/arg '\"${directory}\"'",
'rm Directory/directive/arg[.="Indexes"]',
],
}
}
apache::userdir { '/var/www/blog/conf/userdir.conf':
directory => '/var/www/blog/htdocs',
}
The augeas_file
has the possibility of working with other augeas
resources whose incl
parameter matches their name:
file { '/var/www/blog/conf/userdir.conf':
ensure => file,
owner => 'www-data',
group => 'root',
mode => '2570',
}
augeas_file { '/var/www/blog/conf/userdir.conf':
base => '/usr/share/doc/apache2.2-common/examples/apache2/extra/httpd-userdir.conf',
}
augeas { 'userdir directory':
incl => '/var/www/blog/conf/userdir.conf',
lens => 'Httpd.lns',
changes => 'set Directory/arg "\"/var/www/blog/htdocs\""',
}
augeas { 'userdir no indexes':
incl => '/var/www/blog/conf/userdir.conf',
lens => 'Httpd.lns',
changes => 'rm Directory/directive/arg[.="Indexes"]',
}
This allows to split the changes into individual resources.