1

I am implementing a defined resource in puppet to create a number of websites.

One of the steps is creating the documentroot directory for the website.

My problem is that some websites can share a common documentroot. When that happens I get the error:

Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Resource Statement, Cannot alias File[documentroot-redirpt] to ["/var/www/vhosts/parkingyredir"] at /etc/puppetlabs/code/environments/production/modules/xxx_corp_webserver/manifests/website.pp:164; resource ["File", "/var/www/vhosts/parkingyredir"] already declared at /etc/puppetlabs/code/environments/production/modules/xxx_corp_webserver/manifests/website.pp:164 at /etc/puppetlabs/code/environments/production/modules/xxx_corp_webserver/manifests/website.pp:164:3 at /etc/puppetlabs/code/environments/production/modules/xxx_corp_webserver/manifests/init.pp:10 on node llim605 

So, how can I create create a resource such as that it will create the directory but not complain if I try to create it serveral times?

The owner and permissions for the documentroot directory are constant.

1
  • Sorry, figured out the answer a few minutes after posting the question. Commented Nov 8, 2019 at 11:55

2 Answers 2

2

Sorry, figured out the answer a few minutes after posting the question.

If there is potential for the resource to be redefined, just check if it has already been defined.

 if !defined(File[$documentroot]) { file { "documentroot-${title}": ensure => 'directory', path => $documentroot, group => '0', mode => '0755', owner => '0', } } 
0
1

The Puppet documentation states:

Warning: Avoid relying on the result of the defined function in modules, as you might not be able to guarantee the evaluation order well enough to produce consistent results. This can cause other code that relies on the function’s result to behave inconsistently or fail.

An alternative (and perhaps more reliable) solution would be to use the ensure_resource function from puppetlabs-stdlib. It creates the resource only if it doesn't already exist and only raises a duplicate declaration error if the resource already exists and doesn't have matching parameters.

ensure_resource('file', $documentroot, { ensure => 'directory', owner => '0', group => '0', mode => '0755', }) 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.