Y. Leonce Eyog
Y. Leonce Eyog

Reputation: 903

Ansible script: python: ModuleNotFoundError

I'm trying to run my python main file using ansible.

  name: Run project
  become: yes
  script: "{{repo}}/main.py --arg1 {{repo}}/configfile.py"
  args:
     chdir: "{{repo}}"
     executable: "{{repo}}/venv/bin/python"

This file has multiple imports from my project. I'm able to source the virtual env and run the main script using ansible. However, I get this error

However, I get this error:

fatal: [local_machine]: FAILED! => {"changed": true, "msg": "non-zero return code", "rc": 1, "stderr": "Traceback (most recent call last):\n  File \"/home/vagrant/.ansible/tmp/ansible-tmp-1571132915.6405714-115359809713305/main.py\", line 1, in <module>\n    import utils\nModuleNotFoundError: No module named 'utils'\n", "stderr_lines": ["Traceback (most recent call last):", "  File \"/home/vagrant/.ansible/tmp/ansible-tmp-1571132915.6405714-115359809713305/watch_scraper.py\", line 1, in <module>", "    import utils", "ModuleNotFoundError: No module named 'utils'"], "stdout": "", "stdout_lines": []}

How do I get ansible to run the script and get it aware of its conxtext?

Upvotes: 0

Views: 2508

Answers (1)

error404
error404

Reputation: 2823

The script you are trying to execute might need utils installed for execution.

Since there is no delegation to localhost or local_action is not being used. The execution is happening on the target node.

You need to install the utils package.

In python the pip(package manager) is associated with specific version of python i.e. pip3 for python3, pip2 for python2.

Since you are using venv you need to install the packages before executing the script.

I would suggest to pass the requirements.txt and use that as a reference to install the modules.

Upvotes: 1

Related Questions