user3463832
user3463832

Reputation: 15

Ansible playbook with nested python scripts

I am trying to execute a ansible playbook which uses the script module to run a custom python script. This custom python script is importing another python script. On execution of the playbook the ansible command fails while trying to import the util script. I am new to ansible, please help!!

helloWorld.yaml:

- hosts: all
  tasks:
  - name: Create a directory
    script: /ansible/ems/ansible-mw-tube/modules/createdirectory.py "{{arg1}}"

createdirectory.py -- Script configured in YAML playbook

#!/bin/python

import sys
import os
from hello import HelloWorld


class CreateDir:
        def create(self, dirName,HelloWorldContext):
                output=HelloWorld.createFolder(HelloWorldContext,dirName)
                print output
                return output
def main(dirName, HelloWorldContext):
        c = CreateDir()
        c.create(dirName, HelloWorldContext)
if __name__ == "__main__":
        HelloWorldContext = HelloWorld()
        main(sys.argv[1],HelloWorldContext)
        HelloWorldContext = HelloWorld()

hello.py -- util script which is imported in the main script written above

#!/bin/python

import os
import sys

class HelloWorld:

         def createFolder(self, dirName):
             print dirName
             if not os.path.exists(dirName):
                os.makedirs(dirName)
                print dirName
                if os.path.exists(dirName):
                   return "sucess"
                else:
                   return "failure"

Ansible executable command

ansible-playbook -v -i /ansible/ems/ansible-mw-tube/inventory/helloworld_host /ansible/ems/ansible-mw-tube/playbooks/helloWorld.yml -e "arg1=/opt/logs/helloworld"

Ansible version

ansible --version
[WARNING]: log file at /opt/ansible/ansible.log is not writeable and we cannot create it, aborting

ansible 2.2.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

Upvotes: 1

Views: 1812

Answers (1)

Kraylog
Kraylog

Reputation: 7563

The script module copies the script to the remote server and executes it there using the shell command. It can't find the util script, since it doesn't transfer that file - it doesn't know that it needs to do it.

You have several options, such as use copy to move both files to the server and use shell to execute them. But since what you seem to be doing is creating a directory, the file module can do that for you with no scripts necessary.

Upvotes: 2

Related Questions