# How to get namevar value inside File at class?

I have following code:

class scripts($owner,$group) {

$scripts_list = ['1.sh', '2.sh'] file {$scripts_list:
ensure  => 'file',
source  => "puppet:///modules/scripts/${title}", mode => '0755', owner =>$owner,
group   => $group } }  The problem is that I can't get name of the script to use it inside source attribute. I tried $path but it expands to $PATH. I tried $title and $name but they are expands to name of the class, not the file. Is there a way how to get value of namevar of file? edit retag close merge delete ## 2 Answers Sort by » oldest newest most voted You can't do this directly. Resources do not expose their variables like that. The $title would refer to the title of the class in this case, as you discovered.

What you need to do is create a defined type that wraps the file resource.

For example:

class scripts($owner,$group) {

$scripts_list = ['1.sh', '2.sh'] script_file {$scripts_list:
owner   => $owner, group =>$group
}
}

define script_file (
$owner,$group,
){
file { $title: ensure => 'file', source => "puppet:///modules/scripts/${title}",
mode    => '0755',
owner   => $owner, group =>$group
}
}

more

I wanted to hear is this impossible or it's possible but I don't know how to do it. I knew about define when I asked. So, first sentence ...(more)

( 2013-09-05 13:28:49 -0500 )edit

I usually just throw all of my script into a directory, and then sync the entire directory.

file {'script_dir':
ensure  => 'directory',
recurse => true,
purge   => true,
owner   => "${owner}", group => "${group}",
mode    => '0755',
source  => 'puppet:///modules/scripts',
}


Otherwise, you could wrap your File resource in a defined type.

class scripts($owner,$group) {

$scripts_list = ['1.sh', '2.sh'] define files_from_array() { file {$title:
ensure  => 'file',
path    => "your/path/${title}", source => "puppet:///modules/scripts/${title}",
mode    => '0755',
owner   => "${owner}", group => "${group}",
}
}

files_from_array {\$scripts_list: }
}

more