我正在学习如何开发自己的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
文件中吗?
当我运行我的剧本时,我会得到这样的输出:
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.rn", "module_stdout": "Traceback (most recent call last):rn File "/root/.ansible/tmp/ansible-tmp-1659317009.2831638-936-151587777307338/AnsiballZ_realtime_server.py", line 100, in <module>rn _ansiballz_main()rn File "/root/.ansible/tmp/ansible-tmp-1659317009.2831638-936-151587777307338/AnsiballZ_realtime_server.py", line 92, in _ansiballz_mainrn invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)rn File "/root/.ansible/tmp/ansible-tmp-1659317009.2831638-936-151587777307338/AnsiballZ_realtime_server.py", line 41, in invoke_modulern run_name='__main__', alter_sys=True)rn File "/usr/lib64/python3.6/runpy.py", line 205, in run_modulern return _run_module_code(code, init_globals, run_name, mod_spec)rn File "/usr/lib64/python3.6/runpy.py", line 96, in _run_module_codern mod_name, mod_spec, pkg_name, script_name)rn File "/usr/lib64/python3.6/runpy.py", line 85, in _run_codern exec(code, run_globals)rn File "/tmp/ansible_realtime_server_payload_56b21nxt/ansible_realtime_server_payload.zip/ansible/modules/realtime_server.py", line 3, in <module>rnModuleNotFoundError: No module named 'server_types'rn", "msg": "MODULE FAILUREnSee 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
感谢如何将.py文件导入Ansible模块?我想好了该怎么办。
我把我支持的python代码放在./module_utils
目录中。然后在我的ansible模块文件realtime_server.py中,我有:
try:
from ansible.module_utils.server_types import factory
except:
from server_types import factory