Puppet [SOLVED]: Puppet roles/profiles, role with multiple instances of profile – how do parameters work?

Puppet [SOLVED]: Puppet roles/profiles, role with multiple instances of profile – how do parameters work?

Home Forums Automation Tools Puppet Puppet [SOLVED]: Puppet roles/profiles, role with multiple instances of profile – how do parameters work?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #229302

    Cloudy Point
    Keymaster

    QuestionQuestion

    I am just learning Puppet (we have Puppet Enterprise locally). I am trying to understand the “roles and profiles” pattern. Please pardon any nomenclature slip ups.

    How do I create a role with multiple instances of a profile, where the profile instances differ only by parameter? I’m guessing Hiera fits into this somewhere but I’m not exactly sure how.

    For example:

    Puppetfile:

    mod 'puppetlabs-apache', '2.3.0'
    

    apache.pp profile

    class profile::apache (
        String $port = '80',
    ) {
    
      class { 'apache':
        listen => $port,
      }
    
    }
    

    twoapaches.pp role

    class role::twoapaches {
      include profile::apache
      include profile::apache
    }
    

    I want an instance of the twoapaches role to have an apache at ports 90 and 100 – how do I do that?

    #229303

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You actually can’t use classes like that in Puppet; a class can only be declared once-per-node.

    You probably need some of the defined types in the puppetlabs/apache module. Defined types are used when you need to declare a user-defined “resource” more than once on a single node.

    E.g. profile might be:

    class profile::two_vhosts {
      apache::vhost { 'ip1.example.com':
        ip      => ['127.0.0.1','169.254.1.1'],
        port    => '80',
        docroot => '/var/www/ip',
      }
      apache::vhost { 'ip2.example.com':
        ip      => ['127.0.0.1'],
        port    => '8080',
        docroot => '/var/www/ip',
      }
    } 
    

    And the role might be:

    class role::two_vhosts {
      include profile::two_vhosts
      include profile::other_stuff
      ...
    }
    

    If you needed to then pass the ports in, you might have:

    class profile::two_vhosts (
      String $ip1_port,
      String $ip2_port, 
    ) {
      apache::vhost { 'ip1.example.com':
        ip      => ['127.0.0.1','169.254.1.1'],
        port    => $ip1_port,
        docroot => '/var/www/ip',
      }
      apache::vhost { 'ip2.example.com':
        ip      => ['127.0.0.1'],
        port    => $ip2_port,
        docroot => '/var/www/ip',
      }
    } 
    

    You could then have your role as:

    class role::two_vhosts {
      class { 'profile::two_vhosts':
        ip1_port => '80',
        ip2_port => '8080',
      } 
      include profile::other_stuff
      ...
    }
    

    But in practice people use the Automatic Parameter Lookup feature here in conjunction with Hiera (ref).

    Source: https://stackoverflow.com/questions/47469073/puppet-roles-profiles-role-with-multiple-instances-of-profile-how-do-paramete
    Author: Alex Harvey
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.