0

I'm new to using puppet, and have a master and agent server set up. I'm having difficulty figuring out how to run a python script on the agent server.

I've followed the quick start guide and have been searching for an answer, but I can't find a clear explanation.

Currently, my site.pp has:

node default {
    class { 'helloworld':}
    class { 'helloworld::motd':}
    include python
    class { 'pythontest':}
}

the init.pp in pythontest's manifest folder has:

class pythontest {
    exec {'python etc/puppetlabs/code/environments/production/modules/pythontest/print.py':
    require => File['etc/puppetlabs/code/environments/production/modules/pythontest/print.py']
    }
}

Both are running Ubuntu 15.04

So far, Hello world is displayed, and the python module gets installed (https://forge.puppet.com/stankevich/python).

I get the error:

Error: Failed to apply catalog: Validation of Exec[etc/puppetlabs/code/environments/production/modules/pythontest/print.py] failed: 'etc/puppetlabs/code/environments/production/modules/pythontest/print.py' is not qualified and no path was specified. Please qualify the command or specify a path. at etc/puppetlabs/code/environments/production/pythontest/manifests/init.pp:2

I think I can't just put exec : python pathname, but some google searches finds some people using that method.

kckaiwei
  • 416
  • 7
  • 18

2 Answers2

1

Better still to specify your dependencies:

class pythontest {
  file { '/etc/puppetlabs/code/environments/production/modules/pythontest/':
    ensure => directory,
    mode => '0755',
  }
  file { '/etc/puppetlabs/code/environments/production/modules/pythontest/print.py':
    mode => '0644',
    source => 'puppet:///modules/pythontest/print.py',
  }
  exec { 'pythontestprint':
    path => '/usr/bin',
    command => '/usr/bin/python /etc/puppetlabs/code/environments/production/modules/pythontest/print.py',
    require => File['/etc/puppetlabs/code/environments/production/modules/pythontest/print.py'],
  }
}

Also unclear why you're trying to manage Puppet's own files (in /etc/puppetlabs/code with Puppet.

Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
  • Well, I'm totally new to puppet and trying to run a python script on the agent. Is there a better way? I figured you copy the script over, then run it. Or should I save it somewhere else? – kckaiwei Apr 17 '16 at 16:05
  • 1
    Have a look at this on how you're meant to get the code onto the Puppet Master. Puppet itself should not normally be managing the files under /etc/puppetlabs. https://docs.puppet.com/pe/latest/cmgmt_managing_code.html – Alex Harvey Apr 17 '16 at 17:13
  • Hmm, got it. Yeah, some tutorials I've been following have used Git to pull it. Here, it's a python packages that needs to be tested using this script. Would it still be better to go with git, or okay to just tell the agent to call pip install? I'll look into the code manager, see what would be better to use. Thanks! – kckaiwei Apr 18 '16 at 12:45
  • 1
    You'll need Puppet Enterprise for Code Manager, so git pull will probably be the perfect compromise if you're on open source puppet. – Alex Harvey Apr 18 '16 at 13:58
0

Figured it out!

class pythontest {
    file { '/etc/puppetlabs/code/environments/production/modules/pythontest/':
    ensure => directory,
    mode => '0755',
    }
    file { '/etc/puppetlabs/code/environments/production/modules/pythontest/print.py":
    mode => '0644',
    source => 'puppet:///modules/pythontest/print.py',
    }
    exec { 'pythontestprint':
    path => '/usr/bin',
    logoutput => true,
    command => '/usr/bin/python /etc/puppetlabs/code/environments/production/modules/pythontest/print.py',
    }
}

I missed the / before /etc. Changes were made, the 1st two file commands makes a directory and then copies the file itself using source => puppet:///

Last, the exec required me to find where python was installed on the puppet agent and use that as the command, hence the /usr/bin/python.

Gives me a Notice: /Stage[main]/Pythontest/Exec[pythontestprint]/returns: executed successfully.

adding an logoutput => true, gives me the output I was expecting.

kckaiwei
  • 416
  • 7
  • 18