FreeBSD 14
Sponsored Link

Puppet : How to use [file Resource]2024/09/12

 

This is the examples for [file] resource.

[1] It manages the configuration to keep a file like follows.
If the file is none on Puppet client, it is created.
If exists, it keeps with the specified attributes.
root@dlp:~ #
vi /usr/local/etc/puppet/code/environments/production/manifests/site.pp
file { '/home/testfile.txt':
  ensure  => file,
  owner   => 'root',
  group   => 'wheel',
  mode    => '0644',
  content => 'This is the puppet test file.',
}
[2] Specify the contents with variable.
root@dlp:~ #
vi /usr/local/etc/puppet/code/environments/production/manifests/file01.pp
$contents = 'This is the test Puppet manifest.
Sample contents
Test contents
'

file { '/home/testfile2.txt':
  ensure  => file,
  owner   => 'root',
  group   => 'wheel',
  mode    => '0644',
  content => "$contents",
}
[3] Specify the source file on the Puppet server as a template.
root@dlp:~ #
vi /usr/local/etc/puppet/fileserver.conf
# create new

# any name
[extra_files]
  # path to to the directory for template files
  path /usr/local/etc/puppet/files
  # access permission
  # example below allows all
  # if set access permission, set like follows
  # ⇒ [allow 10.0.0.0/24]
  # ⇒ [allow *.srv.world]
  allow *

root@dlp:~ #
service puppetserver restart
root@dlp:~ #
vi /usr/local/etc/puppet/code/environments/production/manifests/file02.pp
file { '/home/testfile3.txt':
  ensure => file,
  owner  => 'root',
  group  => 'wheel',
  mode   => '0644',
  source => 'puppet://dlp.srv.world/extra_files/test.txt',
}

root@dlp:~ #
mkdir /usr/local/etc/puppet/files

root@dlp:~ #
echo "Puppet test file" > /usr/local/etc/puppet/files/test.txt

[4] It manages the configuration to keep a link.
For example, it keeps [/home/testfile.link] from [/home/testfile.txt].
root@dlp:~ #
vi /usr/local/etc/puppet/code/environments/production/manifests/file03.pp
file { '/home/testfile.link':
  ensure => link,
  target => '/home/testfile.txt',
}
[5] It manages the configuration to keep a file does not exist. If it exists, it is deleted.
root@dlp:~ #
vi /usr/local/etc/puppet/code/environments/production/manifests/file04.pp
file { '/home/testfile4.txt': ensure => absent }
[6] It manages the configuration to keep a directory recursively.
The example specifies [mode] as [644], however [x] is added automatically to directories as a matter of course.
Furthermore, files or directories they do not exist in source directory are deleted with the parameter [purge] and [force].
root@dlp:~ #
vi /usr/local/etc/puppet/fileserver.conf
# add to last line
# specify the directory as a template source

[extra_dir]
  path /usr/local/etc/puppet/dirs
  allow *

root@dlp:~ #
service puppetserver restart
root@dlp:~ #
vi /usr/local/etc/puppet/code/environments/production/manifests/file05.pp
file { '/home/testdir':
  ensure  => directory,
  recurse => true,
  purge   => true,
  force   => true,
  owner   => 'root',
  group   => 'wheel',
  mode    => '0644',
  source  => 'puppet://dlp.srv.world/extra_dir/testdir',
}

root@dlp:~ #
mkdir -p /usr/local/etc/puppet/dirs/testdir

root@dlp:~ #
touch /usr/local/etc/puppet/dirs/testdir/test.txt

Matched Content