I am learning Puppet and trying to remove previously added host overrides from one of the environments I created.
class profile::project (
$key = ''
)
{
file {'path':
content => $key,
ensure => present,
group => 'root',
mode => '0644',
owner => 'user'
}
host { 'storage.googleapis.com':
name => 'storage.googleapis.com',
ensure => 'present',
ip => '000.00.00.00'
}
host { 'bigquery.googleapis.com':
name => 'bigquery.googleapis.com',
ensure => 'present',
ip => '000.00.00.00'
}
if $::site = 'site' and $::env = 'env'{
host { 'remove_storage_googleapis_com':
name => 'storage.googleapis.com',
ensure => 'absent',
ip => '000.00.00.00'
}
host {'remove_bigquery_googleapis_com:
name => 'bigquery.googleapis.com',
ensure => 'absent',
ip => '000.00.00.00'
}
}
}
I receive an error:
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Resource Statement, Cannot alias Host[remove_storage_googleapis_com] to ["storage.googleapis.com"] at file/path/filename.pp resource ["Host", "storage.googleapis.com"] already declared.
I tried not declaring the resource again with
class profile::project (
$key = ''
)
{
file {'path':
content => $key,
ensure => present,
group => 'root',
mode => '0644',
owner => 'user'
}
if $::site == 'site' and $::env == 'env'{
$ensure_value == 'absent'
}
else {
$ensure_value == 'present'
}
host { 'storage.googleapis.com':
name => 'storage.googleapis.com',
ensure => $ensure_value,
ip => '000.00.00.00'
}
host { 'bigquery.googleapis.com':
name => 'bigquery.googleapis.com',
ensure => $ensure_value,
ip => '000.00.00.00'
}
}
but it is giving an error that
if statement has no effect. A value was produced and then forgotten (one or more preceding expressions may have the wrong form)
What am I missing here? Any help is appreciated!
Puppet is a descriptive, not imperative language. You describe the desired state, not a series of steps for how to achieve it. Specifically in Puppet
file), as well aspathin the case offile)must be unique within the same catalog. (Exported resources set further restrictions.) Although, given that the
ifbranch’s condition evaluates totrue, you have two resources with differing titlesHost['storage.googleapis.com'], andHost['remove_storage_googleapis_com']their name variables – in the case of the
hostresource type thenameattribute – is the same value. This is not permitted as per above rule, two co‑existing resources of the same type with identical name variable values.Many but not all Puppet language constructs yield a value. For example a resource declaration also yields a value, an array of resource references to the just declared resources. Also an
ifstatement yields a value, namely the last expression’s value in either branch (orundefshould no branch apply).Your problem is that
==identifies a comparison. In both cases, since$ensure_valueis undefined thus resolving toundef, the comparison yields the Boolean valuefalse, so the entireif“returns”false. Puppet complains that you do not use this value (false). You meant to use a single equals sign=to achieve a variable assignment.