Diwas
Diwas

Reputation: 33

How to use list in an if statement in puppet?

class am_rgw_profile::am_rgw_ports {
  firewalld::custom_service{'am_rgw_ports':
    short       => 'am_rgw_ports',
    description => 'IIQ IaaS gateway wg6561',
    port        => [
    if ('xarsiiq1xd' in $hostname) {  [
        {
            'port'     => '2470',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2472',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2474',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2476',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2478',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2480',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2482',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2484',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2486',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2490',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2492',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2494',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2496',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2498',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2500',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2502',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2504',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2506',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2508',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2510',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2512',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2514',
            'protocol' => 'tcp',
        },
      ]
    }elsif ('xarsiiq1xe' in $hostname) { [
      {
            'port'     => '2492',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2516',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2518',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2520',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2522',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2524',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2526',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2528',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2530',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2532',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2534',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2536',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2538',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2540',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2542',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2544',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2546',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2548',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2550',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2552',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2554',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2556',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2558',
            'protocol' => 'tcp',
      },
      ]
    }else {
      {
            'port'     => '2492',
            'protocol' => 'tcp',
      }
    }
  ]
  }
  firewalld_service { 'Allow am_racf_ports services for IIQ gateway servers':
    ensure  => 'present',
    service => 'am_rgw_ports',
    zone    => 'Cali'
  }
}

when my code goes to the if condition, this is the error I get:

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 Function Call, Failed to parse template firewalld/service.xml.erb: Filepath: org/jruby/RubyArray.java Line: 1489 Detail: no implicit conversion of String into Integer (file: /etc/puppetlabs/code/common_modules/modules/firewalld/manifests/custom_service.pp, line: 66, column: 16) (file: /etc/puppetlabs/code/environments/community_am_racf_gateway/profiles/am_rgw_profile/manifests/am_rgw_ports.pp, line: 2) on node xarsiiq1xd.opr.test.zone.org Warning: Not using cache on failed catalog Error: Could not retrieve catalog; skipping run

What am I doing wrong?

Upvotes: 0

Views: 540

Answers (2)

balder
balder

Reputation: 799

small optimisation on Alex Harvey's answer

$port = $facts['hostname'] ? {
  /xarsiiq1xd/ => [
                    {'port' => '2470', 'protocol' => 'tcp'},
                    {'port' => '2472', 'protocol' => 'tcp'},
                  ]
  /xarsiiq1xe/ => [
                    {'port' => '2492', 'protocol' => 'tcp'},
                    {'port' => '2516', 'protocol' => 'tcp'},
                  ]
  default =>   
                  [{'port' => '2492', 'protocol' => 'tcp'},]
}
firewalld::custom_service { 'am_rgw_ports':
  short       => 'am_rgw_ports',
  description => 'IIQ IaaS gateway wg6561',
  port        => $port,
}
firewalld_service { 'Allow am_racf_ports services for IIQ gateway servers':
  ensure  => 'present',
  service => 'am_rgw_ports',
  zone    => 'Cali'
}

Note, you can put the selector statement inline, instead of first assigning it to the port variable e.g.

firewalld::custom_service { 'am_rgw_ports':
  short       => 'am_rgw_ports',
  description => 'IIQ IaaS gateway wg6561',
  port        => $facts['hostname'] ? { /xarsiiq1xd/ => [{...}], /xarsiiq1x3/ => [{...}],}
}

but it looks ugly

Upvotes: 0

Alex Harvey
Alex Harvey

Reputation: 15472

The error message may be a bit confusing, but the syntax you are trying to use is simply incorrect.

What you appear to be trying to do is something like this:

class am_rgw_profile::am_rgw_ports {
  if ('xarsiiq1xd' in $hostname) {
    $port = [
      {
        'port'     => '2470',
        'protocol' => 'tcp',
      },
      {
        'port'     => '2472',
        'protocol' => 'tcp',
      }, # etc
    ]
  } elsif ('xarsiiq1xe' in $hostname) {
    $port = [
      {
        'port'     => '2492',
        'protocol' => 'tcp',
      },
      {
        'port'     => '2516',
        'protocol' => 'tcp',
      },
    ]
  }
  firewalld::custom_service { 'am_rgw_ports':
    short       => 'am_rgw_ports',
    description => 'IIQ IaaS gateway wg6561',
    port        => $port,
  }
  firewalld_service { 'Allow am_racf_ports services for IIQ gateway servers':
    ensure  => 'present',
    service => 'am_rgw_ports',
    zone    => 'Cali'
  }
}

Upvotes: 2

Related Questions