Search code examples
templatespuppetssh-keys

Puppet - including another file


is there a way to insert one Puppet file into other Puppet files ?

We do have a lot of servers with SSH key definitions managed by Puppet and I would like to have something like a separate file for all the SSH key definitions. The main goal is to update the key definitions only in a few places instead of all our server definitions.

Thank you very much


Solution

  • Use a class - it's a container for resources (like your SSH key resources) that you can add to nodes or from other classes with a single line. When you add it, the node gets all of the resources inside.

    class sshkeys {
      ssh_authorized_key { '[email protected]':
        ensure => present,
        user   => 'foo',
        type   => 'ssh-rsa',
        key    => 'AAAAB3Nza[...]qXfdaQ==',
      }
    
      ssh_authorized_key { '[email protected]':
        # more...
      }
    }
    

    And then in your node block, use include:

    node foo.example.com {
      include sshkeys
    }
    

    The class definition can either be in the same or another file in the manifests directory, or preferably you can start putting them into a module layout, i.e. /etc/puppetlabs/puppet/environments/production/sshkeys/manifests/init.pp where Puppet will automatically load it. Modules help you encapsulate functionality, as you can store files, templates and more specific manifests and classes together.