Ask Your Question
0

Changing owner dynamically

asked 2016-06-08 03:28:56 -0500

adam93 gravatar image

updated 2016-06-08 08:39:31 -0500

I'm pretty new to puppet and have the below configuration setup on my master node, I know it's not the standard "module structure" but this fits my needs better. I want to apply the shlib.pp manifest to all nodes but under different owners per node. Is there anyway I can set the owner attribute as a variable in shlib.pp and then set it per node in site.pp rather than having to put the whole code into site.pp for each node ? Many thanks.

/etc/puppet/manifests/site.pp

node default {
   # Apply to all nodes
}

node servername1 {
    # User is usr01
    import 'sh_lib.pp'
}

node servername2 {
    # User is usr02
    import 'sh_lib.pp'
}

/etc/puppet/manifests/sh_lib.pp

file { "/path/to/target_dir":
    ensure => directory,
    recurse => remote,
    owner => ???,
    mode => 0755,
    source => "puppet:///svn_trunk/sh_lib"
}

Using puppet version 3.8.7.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2016-06-08 10:40:50 -0500

UBPClaw gravatar image

It depends on your criteria for selecting the owner of the file but Id populate the variable based on a system fact

class foo {

   $owner = $::hostname ? {
       'server01' => 'user01',
       'server02' => 'user02',
     }

 file { "/path/to/target_dir":
     ensure => directory,
     recurse => remote,
     owner => $owner,
     mode => 0755,
     source => "puppet:///svn_trunk/sh_lib"
  }
}
edit flag offensive delete link more

Comments

This works well when I just tested it but if I had loads of manifest files (one per component) I'd need to keep the owner list in each manifest file up to date manually?

adam93 gravatar imageadam93 ( 2016-06-09 06:49:07 -0500 )edit

Well not knowing what criteria youre using to determine the owner its tough to say, but essentially yes. If you were using a simple convention like my example above you could use a regex as a catch all so that server** always had the owner set as user** and the last two digits would match.

UBPClaw gravatar imageUBPClaw ( 2016-06-09 13:06:14 -0500 )edit

But I dont know if that meets your needs

UBPClaw gravatar imageUBPClaw ( 2016-06-09 13:06:28 -0500 )edit
0

answered 2016-06-08 10:16:33 -0500

DarylW gravatar image

updated 2016-06-08 10:17:05 -0500

I would recommend using a module instead of an 'import' command. That allows you to pass parameters in directly. See https://docs.puppet.com/guides/moduleguides/bgtm.html for a beginner's guide to modules. if you, for instance, created a 'shlib' module, you would then be able to call it like this..

node default {
   # Apply to all nodes
}

node servername1 {
  class{'::sh_lib' :
    user => 'usr01',
  }
}

node servername2 {
  class{'::sh_lib' :
    user => 'usr02',
  }
}

You also would be able to use hiera's automatic parameter lookup (APL) to provide the same behavior for your class on a node by node basis, and use the 'include ::sh_lib' syntax.

edit flag offensive delete link more

Comments

We don't have our svn repository setup in a module structure and that's what I'm using to serve the code to the agents. I don't want to have to either a) change the svn or b) write a script to checkout and move to a module format. Otherwise it will be just module folders with only manifest files?

adam93 gravatar imageadam93 ( 2016-06-08 10:35:27 -0500 )edit

module folders with only manifest files are also ok. I don't know how you are getting your puppet code to your nodes/master, but we currently rsync an entire hieradata,manifests,modules set of directories into our specific /etc/puppet/environment/<myenv>/

DarylW gravatar imageDarylW ( 2016-06-08 15:31:19 -0500 )edit

in the above case, you would have: manifests/site.pp, manifests/nodes.pp, modules/sh_lib/manifests/init.pp. You would then take that over into your env

DarylW gravatar imageDarylW ( 2016-06-08 15:32:58 -0500 )edit

I have a svn checkout scheduled to the master (changed fileserver location) which is then used to serve the code to agents when updated. Given that it makes it easier for us to have one central location of manifest files rather than many empty "module" structures holding them.

adam93 gravatar imageadam93 ( 2016-06-09 06:54:47 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-06-08 03:28:56 -0500

Seen: 53 times

Last updated: Jun 08 '16