簡體   English   中英

ModuleNotFoundError: No module named 'server_types' Ansible 模塊開發

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

我正在學習如何開發自己的 ansible 模塊。 我有這個劇本來測試我的新模塊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

我在./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

這是我的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()

我是否必須將所有 python 代碼放入realtime_server.py文件中?

當我運行我的劇本時,我得到了這個 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   

感謝如何將 a.py 文件導入 Ansible 模塊? 我能夠弄清楚該怎么做。

我將我的支持 python 代碼放在./module_utils目錄中。 然后在我的 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