Ask Your Question
0

How do I iterate through similar "file" resources

asked 2016-04-29 02:04:45 -0600

felipe1982 gravatar image

My Sample code shown below. How can I simplify this into just one file resource? My collection will so grow to dozens of files, and I could eventually use the recurse => true attribute for file resource.

1) how can I simplify my code

2) will recurse => true purge files that are not managed by puppet (/usr/local/bin) ?

class profiles::freshlocalbin {

  file { 'cls':
    ensure => present,
    path   => '/usr/local/bin/cls',
    mode   => '0755',
    owner  => 'root',
    group  => 'root',
    source => 'puppet:///modules/profiles/freshlocalbin/cls',
  }
  file { 'chenv':
    ensure => present,
    path   => '/usr/local/bin/chenv',
    mode   => '0755',
    owner  => 'root',
    group  => 'root',
    source => 'puppet:///modules/profiles/freshlocalbin/chenv',
  }
}
edit retag flag offensive close merge delete

Comments

what is your puppet version, if it's puppet 4 then you use "each" iteration.

Chutki gravatar imageChutki ( 2016-04-29 03:57:09 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-04-29 03:57:10 -0600

updated 2016-04-29 04:03:29 -0600

1)

This is probably what you're after:

Puppet 3.x:

define freshlocalbin () {
  file { $name:
    ensure => present,
    path   => "/usr/local/bin/$name",
    mode   => '0755',
    owner  => 'root',
    group  => 'root',
    source => "puppet:///modules/profiles/freshlocalbin/$name",
  }
}

$files = ['cls', 'chenv']
freshlocalbin($files)

Puppet 4.x:

$files = ['cls', 'chenv']
$files.each |String $file| {
  file { $file:
    ensure => present,
    path   => "/usr/local/bin/$file",
    mode   => '0755',
    owner  => 'root',
    group  => 'root',
    source => "puppet:///modules/profiles/freshlocalbin/$file",
  }
}

2)

If you want to recursively manage directories, and purge unmanaged files:

file { '/some/dir':
  ensure  => directory,
  recurse => true,
  purge   => true,
  source  => 'puppet://file_server/some/dir',
}
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: 2016-04-29 02:04:45 -0600

Seen: 185 times

Last updated: Apr 29 '16