Ask Your Question
1

How to get namevar value inside File at class?

asked 2013-09-05 06:13:55 -0500

Slava Semushin gravatar image

updated 2013-09-12 01:01:30 -0500

golja gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-09-05 10:43:35 -0500

llowder gravatar image

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
  }
}
edit flag offensive delete link more

Comments

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)

Slava Semushin gravatar imageSlava Semushin ( 2013-09-05 13:28:49 -0500 )edit
2

answered 2013-09-05 10:44:12 -0500

Ancillas gravatar image

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: }                                         
}
edit flag offensive delete link more

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: 2013-09-05 06:13:55 -0500

Seen: 1,056 times

Last updated: Sep 05 '13