Ask Your Question
0

puppet code to execute bash script

asked 2016-01-15 04:08:58 -0500

viki gravatar image

updated 2016-01-15 13:50:02 -0500

lavaman gravatar image

My requirement is, I want to execute a shell script with puppet?

I Tried below code it did not work:-

class mymodule::myklass{
  file {
    'my_bash_script':
      ensure => 'file',
       source => 'puppet:///modules/mymodule/showip.sh',
      path => '/etc/puppet/modules/mymodule/showip.sh',
      owner => 'root',
      group => 'root',
      mode  => '0755', # Use 0700 if it is sensitive
      notify => Exec['run_my_script'],
  }
exec {
    'run_my_script':
  #path => ["/usr/bin", "/usr/sbin", "/bin/bash"],
 command => '/etc/puppet/modules/mymodule/showip.sh',
 refreshonly => true
  }
}

Thanks,viki

edit retag flag offensive close merge delete

Comments

You must clarify what you mean by "did not work." Did it do nothing? Did you get an error of some sort?

lavaman gravatar imagelavaman ( 2016-01-15 13:51:08 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-01-16 16:07:30 -0500

viki gravatar image

updated 2016-01-17 01:34:05 -0500

Thank you Alex,

Below script worked for me :)

file {
    '/etc/puppet/modules/mymodule/showip.sh':
      ensure => 'file',
      path => '/etc/puppet/modules/mymodule/showip.sh',
      owner => 'root',
      group => 'root',
      mode  => '0755', # Use 0700 if it is sensitive
      notify => Exec['extract_editor_script'],
  }
exec { 'extract_editor_script':
  command => "/bin/bash -c '/vagrant/showip.sh'",
}
edit flag offensive delete link more

Comments

Well that's great, however you really shouldn't do that, refer to my previous comments. :)

Alex Harvey gravatar imageAlex Harvey ( 2016-01-17 01:34:24 -0500 )edit
0

answered 2016-01-16 06:38:23 -0500

updated 2016-01-16 06:44:18 -0500

I got the code to work after making a minor change to it:

# cat /tmp/foo.pp 
class mymodule::myklass{
  file { 'my_bash_script':
      ensure => 'file',
      content => '#!/bin/bash
touch /tmp/alex
',
      path  => '/etc/puppet/modules/mymodule/showip.sh',
      owner => 'root',
      group => 'root',
      mode  => '0755', # Use 0700 if it is sensitive
      notify => Exec['run_my_script'],
  }
  exec { 'run_my_script':
      #path => ["/usr/bin", "/usr/sbin", "/bin/bash"],
      command => '/etc/puppet/modules/mymodule/showip.sh',
      refreshonly => true
  }
}
include mymodule::myklass

Then I puppet apply it and I get:

[root@centos-66-x64 ~]# puppet apply /tmp/foo.pp                                                                                                                                                           
Notice: Compiled catalog for centos-66-x64.home in environment production in 0.07 seconds
Notice: /Stage[main]/Mymodule::Myklass/File[my_bash_script]/ensure: defined content as '{md5}f1dd631bed253f872fc27662e292dc70'
Notice: /Stage[main]/Mymodule::Myklass/Exec[run_my_script]: Triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.12 seconds

Very important point: You should not have puppet install the file showip.sh in the same location (path => '/etc/puppet/modules/mymodule/showips.sh') that it is sourced from in your Puppet code (source => 'puppet:///modules/mymodule/showips.sh'). Rather, you should install the file via the path attribute in a sensible location such as in /usr/local.

If you were executing this code on a Puppet master such that the path and source corresponded to exactly the same file on disk, that may have been your problem, although I've never tried this, so can't be sure what would happen.

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-01-15 04:08:58 -0500

Seen: 8,053 times

Last updated: Jan 17 '16