Search code examples
puppet

merge puppet code in manifest file


I am using puppet 3.8.7.I want to write all of the below code in a single manifest file and run it.every code works fine separately.is it possible? first, I want to install nodejs,then update my nodejs, then run my bashscript,then install git and download git repo

install nodejs:

class { 'nodejs':
  repo_url_suffix => '6.x',
}

then update node js:

exec { 'install-node-version-manager':
cwd       => '/',
path      => '/usr/bin:/bin:/usr/local/bin:/usr/lib/node_modules/npm/bin',
logoutput => 'on_failure',
command   => 'npm install -g n',
}


exec { 'install-node-version-manager':
cwd       => '/',
path      => '/usr/bin:/bin:/usr/local/bin:/usr/lib/node_modules/npm/bin',
logoutput => 'on_failure',
command   => 'n latest',

}

then run bash_script.sh

file {'/home/ec2-user/my_bash_script.sh':
 source => "puppet:///modules/mymodule/my_bash_script.sh",
 mode => '755',

}

exec {'/home/ec2-user/my_bash_script.sh':
    refreshonly => 'true',
    require => File["/home/ec2-user/my_bash_script.sh"],
    subscribe => File["/home/ec2-user/my_bash_script.sh"],
  }

then install git and download repo

package 
    { 'git':
      ensure => 'latest',
    }

vcsrepo { "/nodejs-helloworld":
        ensure   => latest,
        provider => git,
        require  => [ Package["git"] ],
        source   => "[email protected]:hello-world/nodejs-helloworld.git",
        revision => 'master',
}

Solution

  • Puppet provides various ways to establish relationships and ordering between resources. You can use meta-parameters - require, before, notify, subscribe for example. You can also use chaining arrows to control the flow of the execution. here your code, in one module -

     class installnodejs{
    
      class { 'nodejs':
        repo_url_suffix => '6.x',
        before          => Exec['install-node-version-manager-global'],
      }
    
      exec { 'install-node-version-manager-global':
        cwd       => '/',
        path      => '/usr/bin:/bin:/usr/local/bin:/usr/lib/node_modules/npm/bin',
        logoutput => 'on_failure',
        command   => 'npm install -g n',
        before    => Exec['install-node-version-manager-latest'],
      }
    
      exec { 'install-node-version-manager-latest':
        cwd       => '/',
        path      => '/usr/bin:/bin:/usr/local/bin:/usr/lib/node_modules/npm/bin',
        logoutput => 'on_failure',
        command   => 'n latest',
        before    => File['/home/ec2-user/my_bash_script.sh'],
      }
    
      file {'/home/ec2-user/my_bash_script.sh':
        source => "puppet:///modules/mymodule/my_bash_script.sh",
        mode   => '755',
        before => Exce['/home/ec2-user/my_bash_script.sh'], 
      }
    
      exec {'/home/ec2-user/my_bash_script.sh':
        refreshonly => 'true',
        require   => File["/home/ec2-user/my_bash_script.sh"],
        subscribe => File["/home/ec2-user/my_bash_script.sh"],
        before    => Vcsrepo['/nodejs-helloworld'],
      }
    
      package { 'git':
        ensure => 'latest',
      }
    
      vcsrepo { "/nodejs-helloworld":
        ensure   => latest,
        provider => git,
        require  => [ Package["git"] ],
        source   => "[email protected]:hello-world/nodejs-helloworld.git",
        revision => 'master',
      }
    
    }
    

    please notice that I've changed the names of your resources. you can't include the same resource twice in the same module.