Delete previously added hosts form one of the environments

57 Views Asked by At

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!

1

There are 1 best solutions below

0
Kai Burghardt On

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

  • all resource titles of one resource type (e. g. file), as well as
  • all resource name variables of one resource type (e. g. path in the case of file)

must be unique within the same catalog. (Exported resources set further restrictions.) Although, given that the if branch’s condition evaluates to true, you have two resources with differing titles

  • Host['storage.googleapis.com'], and
  • Host['remove_storage_googleapis_com']

their name variables – in the case of the host resource type the name attribute – 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.

    if $::site == 'site' and $::env == 'env'{
        $ensure_value == 'absent'
    }
    else {
        $ensure_value == 'present'
    }

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 if statement yields a value, namely the last expression’s value in either branch (or undef should no branch apply).

Your problem is that == identifies a comparison. In both cases, since $ensure_value is undefined thus resolving to undef, the comparison yields the Boolean value false, so the entire if “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.