user5154816
user5154816

Reputation:

copy shell script from puppet master to puppet agent and execute it on puppet agent

I am trying to copy a shell script from puppet master to puppet client.

This is my shell script on puppet master:

cat /etc/puppetlabs/code/environments/production/modules/mymodule/mybash.sh
echo hi hello

I have copied the same shell script in /home/myserver/mybash.sh also.

This is my site.pp file on puppet master:

cat /etc/puppetlabs/code/environments/production/manifests/site.pp

notify{"Message : I am a message from puppet master": }

class myfile {
  file { '/home/myserver/mybash.sh':
    mode   => '0755',
    owner  => 'root',
    source => 'puppet:///modules/mymodule/mybash.sh',
    notify => Exec['run_my_script'],
  }
  exec { 'run_my_script':
    command => '/home/npatel/mybash.sh',
  }
}
notify{"Message : new msg test": }

This is the output from puppet agent:

myclient:~$ sudo /opt/puppetlabs/bin/puppet agent --test
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for myclient.test.com
Info: Applying configuration version '1483393652'
Notice: Message : I am a message from puppet master
Notice: /Stage[main]/Main/Notify[Message : I am a message from puppet master]/message: defined 'message' as 'Message : I am a message from puppet master'
Notice: Message : new msg test
Notice: /Stage[main]/Main/Notify[Message : new msg test]/message: defined 'message' as 'Message : new msg test'
Notice: Applied catalog in 0.20 seconds

It looks like only lines with notify{"Message : are getting executed on puppet agent, not the lines in between them.

I don't see mybash.sh getting copied in /home/my-client path as mentioned in file section and there are no logs also to make sure that the file gets copied or not.

Did I miss anything in the source value?

Upvotes: 0

Views: 1804

Answers (1)

Peter Souter
Peter Souter

Reputation: 5210

In Puppet, defining a class doesn't automatically include it in a configuration, it simply makes it available to be declared. Defining a class is similar to defining a function in a language like Ruby, Python, or C. The function only ever has effect when it is invoked. Similarly, Puppet class definitions don't have any effect until we declare them.

If you want to actually use the class, you can declare it using the include function. This tells Puppet to evaluate the class and manage all the resources declared within it.

Besides the include function, the resource-like class {'myfile':} syntax can be used.

So in your code, if you want to declare the class, you'd have to do something like this:

/etc/puppetlabs/code/environments/production/manifests/site.pp

notify{"Message : I am a message from puppet master": }

class myfile {
  file { "/home/myserver/mybash.sh":
    mode   => '0755',
    owner  => 'root',
    source => 'puppet:///modules/mymodule/mybash.sh',
    notify => Exec['run_my_script'],
  }
  exec { 'run_my_script':
    command => '/home/npatel/mybash.sh',
  }
}

include myfile
notify{"Message : new msg test": }

I would also recommend putting your class code into the module manifest directly, then including that class in your site.pp file:

/etc/puppetlabs/code/environments/production/modules/mymodule/init.pp

class myfile {
  file { "/home/myserver/mybash.sh":
    mode   => '0755',
    owner  => 'root',
    source => 'puppet:///modules/mymodule/mybash.sh',
    notify => Exec['run_my_script'],
  }
  exec { 'run_my_script':
    command => '/home/npatel/mybash.sh',
  }
}

And then declare the class in your site.pp:

node default {
  include myclass
} 

Upvotes: 3

Related Questions