简体   繁体   English

ModuleNotFoundError: No module named 'server_types' Ansible 模块开发

[英]ModuleNotFoundError: No module named 'server_types' Ansible module development

I am learning about developing my own ansible modules.我正在学习如何开发自己的 ansible 模块。 I have this playbook to test my new module, test_playbook.yml :我有这个剧本来测试我的新模块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:我在./library目录中有我的 python 代码:

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:这是我的realtime_server.py文件的内容:

#!/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?我是否必须将所有 python 代码放入realtime_server.py文件中?

When I run my playbook I get this output:当我运行我的剧本时,我得到了这个 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?感谢如何将 a.py 文件导入 Ansible 模块? I was able to figure out what to do.我能够弄清楚该怎么做。

I put my supporting python code in the ./module_utils directory.我将我的支持 python 代码放在./module_utils目录中。 Then in my ansible module file, realtime_server.py I have:然后在我的 ansible 模块文件 realtime_server.py 我有:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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