Re: [Puppet Users] Iterate over array to mount NFS directories

Luke Bigum
Thu, 27 Sep 2012 08:23:34 -0000

Hi Forrie,

Good to see you are almost there! As you''ve discovered the
"looping" in
Puppet isn''t *really* looping, it''s just a shorthand way of
creating
multiple resources. However, by combining that with a Defined Type, you
can effectively reference the same array element multiple times using
$name in the define - as we do with the File and Mount resources - and
you achieve an approximation of a loop.

Also remember there was a bug in my original post, the File resource
needs "default => directory" to create a directory, not
"present", as
that would create a file instead, but it looks like you''ve figured that
out anyway.

Unfortunately there is no way to recursively create directories in
Puppet. You will need to manage the File[/dce/prod/] directory outside
of the nfs_mount resources. Why? If you have more than one Prod
nfs_mount, you will get duplicate definitions when you try create
/dce/prod/ inside the nfs_mount define.

In your example below you have a class called prod-nfs-mounts. Inside
this class you could have a file { "/dce/prod": ensure => directory
}
resource to ensure the parent directory is created.

One mor bug: the Mount resource should have a dependency on the File
resource - you can''t mount before the mount point is there. Add a
parameter ''require => File[$mount_point]'' to the mount
resource in the
define.

Also just so you know, older versions of Puppet don''t like dashes (-)
in
class and variable names, so I would recommend you use underscores where
possible.

-Luke

On 26/09/12 21:59, Forrest Aldrich wrote:
> I did some tinkering around and came up with something that partially
> works.  The one problem I ran into was this:
>
> Sep 26 16:53:55 test-fms puppet-agent[11974]:
>
(/Stage[main]/Prod-nfs-mounts/Prod-nfs-mounts::Nfs_mount[201202]/File[/dce/prod/201202]/ensure)
> change from absent to present failed: Could not set ''present on
> ensure: No such file or directory - /dce/prod/201202 at
> /etc/puppet/manifests/classes/prod-nfs-mounts.pp:19
>
> I changed the default for the "ensure" value to be
"directory" and
> that didn''t help, but this is strange:
>
> Sep 26 16:56:50 test-fms puppet-agent[12776]:
>
(/Stage[main]/Prod-nfs-mounts/Prod-nfs-mounts::Nfs_mount[201202]/File[/dce/prod/201202]/ensure)
> change from absent to directory failed: Cannot create
> /dce/prod/201202; parent directory /dce/prod does not exist
> Sep 26 16:56:51 test-fms puppet-agent[12776]: Finished catalog run in
> 6.08 seconds
>
> I can''t recall, but there is a way to ensure the directory mount
point
> is recursively present before a mount is attempted.
>
> It should not be trying to create a local directory, but mounting from
> the remote host.    I think I''m almost there, but here''s
the test
> class I have thus far:
>
>
>
> $production = [ "201201", "201202", "201203"
]
> $server     = "de-prod-nas.ourdom.com"
> $prefix     = "/dce/prod"
> $nfsopts    = "tcp,hard,intr,rw,bg"
>
> class prod-nfs-mounts {
>
>         define nfs_mount ( $server, $prefix, $state = "mounted" )
{
>
>                         $mount_point = "${prefix}/${name}"
>
>                     # If the state is "unmounted" the mount point
> ''File'' needs to be removed somehow, after
>                     file { $mount_point:
>                         ensure => $state ? {
>                             "unmounted" => absent,
>                             "absent"    => absent,
>                             default     => present,
>                         }
>                     }
>
>                     mount { $mount_point:
>                         ensure  => $state,
>                         atboot  => true,
>                         options => "$nfsopts",
>                         device  => "{$server}:${mount_point}",
>                     }
>         }
>
>         nfs_mount { $production: server => $server, prefix => $prefix
}
> }
>
>
>
>
>
> Thanks again ! :-)