Search code examples
puppet

How to loop on a resource type multiple times in v3.8?


I have a Puppet manifest with three exec resources:

exec { 'test1':
  command     => "bla1",
  require     => File['test'],
}

exec { 'test2':
  command     => "bla2",
  require     => File['test'],
}

exec { 'test3':
  command     => "bla3",
  require     => File['test'],
}

Can someone offer a way to improve the code, such as with a loop?

I'm using Puppet 3.8 and would need something supported in that release.


Solution

  • This is one way to do it:

      $cmd = [
        "command1",
        "command2",
        "command3",
      ]
    
      define run_command {
        exec { "${name}":
          path    => ['/usr/bin','/usr/sbin','/bin','/sbin'],
        }
      }
      run_command { $cmd: 
          require => File['test'];
      }