Puppet : How to use [file Resource]2025/10/15 |
|
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 /etc/puppet/code/environments/production/manifests/site.pp
file { '/home/testfile.txt':
ensure => file,
owner => 'root',
group => 'root',
mode => '0644',
content => 'This is the puppet test file.',
}
|
| [2] | Specify the contents with variable. |
|
root@dlp:~#
vi /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 => 'root',
mode => '0644',
content => "$contents",
}
|
| [3] | Specify the source file on the Puppet server as a template. |
|
root@dlp:~#
vi /etc/puppet/fileserver.conf # create new # any name [extra_files] # path to to the directory for template files path /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:~#
systemctl reload puppetserver
root@dlp:~#
vi /etc/puppet/code/environments/production/manifests/file02.pp
file { '/home/testfile3.txt':
ensure => file,
owner => 'root',
group => 'root',
mode => '0644',
source => 'puppet://dlp.srv.world/extra_files/test.txt',
}
mkdir /etc/puppet/files root@dlp:~# echo "Puppet test file" > /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 /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 /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 /etc/puppet/fileserver.conf # add to last line # specify the directory as a template source [extra_dir] path /etc/puppet/dirs allow *
root@dlp:~#
systemctl reload puppetserver
root@dlp:~#
vi /etc/puppet/code/environments/production/manifests/file05.pp
file { '/home/testdir':
ensure => directory,
recurse => true,
purge => true,
force => true,
owner => 'root',
group => 'root',
mode => '0644',
source => 'puppet://dlp.srv.world/extra_dir/testdir',
}
mkdir -p /etc/puppet/dirs/testdir root@dlp:~# touch /etc/puppet/dirs/testdir/test.txt |
| Sponsored Link |
|
|