简体   繁体   中英

ModuleNotFoundError: No module named 'server_types' Ansible module development

I am learning about developing my own ansible modules. I have this playbook to test my new module, test_playbook.yml :

---
-
  hosts: all

  tasks:
    - name: Suspend MNR
      realtime_server:
        server_type: 'MNR'
        action: 'suspend'
      ignore_errors: True

    - name: All done
      local_action: debug msg="All done"
      run_once: True

I have my python code in the ./library directory:

ansible@ubuntu-c:~/realtime_server/TEST_LAB_FILES/library$ ls -la
total 16
drwxr-xr-x  8 ansible ansible  256 Aug  1 01:09 .
drwxr-xr-x 16 ansible ansible  512 Aug  1 01:10 ..
-rw-r--r--  1 ansible ansible  352 Aug  1 00:44 object_factory.py
-rw-r--r--  1 ansible ansible 1067 Aug  1 01:05 realtime_server.py
-rw-r--r--  1 ansible ansible 3745 Aug  1 01:06 server_types.py

Here is the contents of my realtime_server.py file:

#!/usr/bin/python3
from ansible.module_utils.basic import AnsibleModule
import server_types

ANSIBLE_METADATA = {
    'metadata_version': '1.1',
    'status': ['preview'],
    'supported_by': 'community'
}

DOCUMENTATION = r''' '''

EXAMPLES = r''' '''


def run_server_action(server_type, action):
    rt_server = server_types.factory.create(name=server_type, action=action)
    rt_server.execute(action=action)


def run_module():
    # define the available arguments/parameters that a user can pass to the module
    module_args = dict( server_type=dict(type='str', required=True), action=dict(type='str', required=True) )
    result = dict( changed=False )
    module = AnsibleModule( argument_spec=module_args, supports_check_mode=True )

    if module.check_mode:
        return result

    if module.params.get('server_type'):
        if module.params.get('action'):
            run_server_action(module.params.get('server_type'),
                              module.params.get('action'))


def main():
    run_module()

if __name__ == '__main__':
    main()

Do I have to put all my python code into the realtime_server.py file?

When I run my playbook I get this output:

ansible@ubuntu-c:~/realtime_server/TEST_LAB_FILES$ ansible-playbook -i hosts -l centos1 test_playbook.yml 

PLAY [all] **********************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************
ok: [centos1]

TASK [Suspend MNR] **************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'server_types'
fatal: [centos1]: FAILED! => {"changed": false, "module_stderr": "Shared connection to centos1 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n  File \"/root/.ansible/tmp/ansible-tmp-1659317009.2831638-936-151587777307338/AnsiballZ_realtime_server.py\", line 100, in <module>\r\n    _ansiballz_main()\r\n  File \"/root/.ansible/tmp/ansible-tmp-1659317009.2831638-936-151587777307338/AnsiballZ_realtime_server.py\", line 92, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/root/.ansible/tmp/ansible-tmp-1659317009.2831638-936-151587777307338/AnsiballZ_realtime_server.py\", line 41, in invoke_module\r\n    run_name='__main__', alter_sys=True)\r\n  File \"/usr/lib64/python3.6/runpy.py\", line 205, in run_module\r\n    return _run_module_code(code, init_globals, run_name, mod_spec)\r\n  File \"/usr/lib64/python3.6/runpy.py\", line 96, in _run_module_code\r\n    mod_name, mod_spec, pkg_name, script_name)\r\n  File \"/usr/lib64/python3.6/runpy.py\", line 85, in _run_code\r\n    exec(code, run_globals)\r\n  File \"/tmp/ansible_realtime_server_payload_56b21nxt/ansible_realtime_server_payload.zip/ansible/modules/realtime_server.py\", line 3, in <module>\r\nModuleNotFoundError: No module named 'server_types'\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
...ignoring

TASK [All done] *****************************************************************************************************************************
ok: [centos1 -> localhost] => {
    "msg": "All done"
}

PLAY RECAP **********************************************************************************************************************************
centos1                    : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1   

Thanks to How to import a.py file into an Ansible Module? I was able to figure out what to do.

I put my supporting python code in the ./module_utils directory. Then in my ansible module file, realtime_server.py I have:

try:
    from ansible.module_utils.server_types import factory
except:
    from server_types import factory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM