Search code examples
pythonubuntuserverpuppetpuppet-enterprise

Running a python script on puppet


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.


Solution

  • 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.