Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module is missing interpreter line - running python module #40561

Closed
ajayreddy28390 opened this issue May 22, 2018 · 8 comments
Closed

module is missing interpreter line - running python module #40561

ajayreddy28390 opened this issue May 22, 2018 · 8 comments
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@ajayreddy28390
Copy link

ajayreddy28390 commented May 22, 2018

SUMMARY

Running python script using Ansible boiler_plate, I get this error "module (run_cmd) is missing interpreter line"

ISSUE TYPE
  • Bug Report
COMPONENT NAME

lib/ansible/plugins/action/__init__.py

ANSIBLE VERSION
ansible 2.5.3
  config file = None
  configured module search path = ['/home/amudimel/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/xxxxxx/ansible_env/lib/python3.6/site-packages/ansible
  executable location = /home/xxxxxx/ansible_env/bin/ansible
  python version = 3.6.3 (default, Feb  8 2018, 12:39:01) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
CONFIGURATION
OS / ENVIRONMENT
STEPS TO REPRODUCE
---
- hosts: server
  gather_facts: yes
  tasks:
    - name: run_command
      action: run_cmd cmd=date
      register: user

    - debug: msg="{{ user.msg }}"

run_cmd.py:
#!/bin/python

def run_command(cmd):
        try:
                import os
                return(os.system(cmd))
        except:
                module.fail_json(msg='Running command failed')

def main():
    module = AnsibleModule(
        argument_spec = dict(
            cmd = dict(required=True)
        )
    )   
    cmd = module.params.get('cmd')
    #exists = is_user_exists(username)
    date_op = run_command(cmd)
    if date_op:
        status = date_op
    else:
        status = 'could not run command: ' % cmd
    module.exit_json(changed=True, msg=str(status))

from ansible.module_utils.basic import *
main()
EXPECTED RESULTS

I expect to get date output

ACTUAL RESULTS
task path: /home/xxxxxx/learn_ansible/run_cmd.yml:5
Using module file /home/xxxxxx/learn_ansible/run_cmd.retry
fatal: [172.28.112.28]: FAILED! => {
    "msg": "module (run_cmd) is missing interpreter line"
}
Using module file /home/xxxxxx/learn_ansible/run_cmd.retry
fatal: [172.28.116.28]: FAILED! => {
    "msg": "module (run_cmd) is missing interpreter line"
}
        to retry, use: --limit @/home/xxxxxx/learn_ansible/run_cmd.retry
@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels May 22, 2018
@sivel
Copy link
Member

sivel commented May 22, 2018

I cannot reproduce this. Running your module I actually got the following:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: not all arguments converted during string formatting

I fixed some things up and converted a few things to the way we expect modules to work:

#!/usr/bin/python

from ansible.module_utils.basic import AnsibleModule


def main():
    module = AnsibleModule(
        argument_spec=dict(
            cmd=dict(required=True)
        )
    )
    cmd = module.params.get('cmd')
    rc, stdout, stderr = module.run_command(cmd)
    if stdout:
        status = stdout.strip()
    else:
        status = 'could not run command: %s' % cmd
    module.exit_json(changed=True, msg=str(status))


if __name__ == '__main__':
    main()

This produces:

TASK [run_command] ***************************************************************************************************************************************************************************************************************************
changed: [localhost] => {"changed": true, "msg": "Tue May 22 14:57:03 CDT 2018"}

TASK [debug] *********************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Tue May 22 14:57:03 CDT 2018"
}

needs_info

@sivel sivel removed the needs_triage Needs a first human triage before being processed. label May 22, 2018
@ansibot ansibot added the needs_info This issue requires further information. Please answer any outstanding questions. label May 22, 2018
@ajayreddy28390
Copy link
Author

Hi Sivel,

I used above python module on Linux 3.10.0-693.11.6.el7.x86_64. Still same error.
"msg": "module (run_cmd) is missing interpreter line"

@ansibot ansibot removed the needs_info This issue requires further information. Please answer any outstanding questions. label May 22, 2018
@peterjanes
Copy link
Contributor

I ran into the same error with a different module (ghetto_json) and finally tracked it down to having saved its license in a file called ghetto_json.license in the library directory. That is, Ansible was trying to run library/ghetto_json.license as a module rather than library/ghetto_json. (I suspect the original report failed because Ansible was trying to execute the run_cmd.retry file.)

This behaviour isn't reproducible across instances, but is 100% reproducible in the instance where it did happen, which leads me to believe it may have something to do with how Ansible enumerates the library directory (perhaps by inode order?). A better error message that identifies the specific module file rather than just the module name would help in debugging this in the future; having a definitive (and documented) module resolution algorithm would also help.

@esamattis
Copy link

I hit this too and seems to happen randomly on different servers. I had two python modules in the library directory. Neither of them had shebang hash but issue occurred only with the other one. Obvious fix seemed to add the shebang line but that caused import error on the ansible module. I tried to add #!/usr/bin/env python3 as the script require Python 3.

I shuffled some import lines on top of the module and now I'm not able to reproduce this. No idea if that shuffling had anything to do with it thou.

@johnmcn1
Copy link

I had this too.

fatal: [abcdef.example.net]: FAILED! => { "msg": "module (cron) is missing interpreter line" }

Turns out it was caused by a playbook I'd created with the same name as the cron module: playbooks/examples/cron.yaml. Something I'd created when delivering an Ansible workshop for my team and left hanging around.

@bcoca
Copy link
Member

bcoca commented Feb 28, 2019

with -vvv you should see message Using module file ... that tells you exact file it is loading

@ansibot
Copy link
Contributor

ansibot commented Oct 10, 2020

@ajayreddy28390: Greetings! Thanks for taking the time to open this issue. In order for the community to handle your issue effectively, we need a bit more information.

Here are the items we could not find in your description:

  • component name

Please set the description of this issue with an appropriate template from:
https://github.com/ansible/ansible/tree/devel/.github/ISSUE_TEMPLATE

click here for bot help

@ansibot ansibot added needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. and removed needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. labels Oct 10, 2020
@Akasurde
Copy link
Member

Closing this issue due to inactivity. Please feel free to open a new issue if the problem persists.

@ansible ansible locked and limited conversation to collaborators Jun 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

8 participants