mirror of https://github.com/jumpserver/jumpserver
				
				
				
			perf: 自动化资产探活支持Telnet方式 (#12728)
Co-authored-by: jiangweidong <weidong.jiang@fit2cloud.com>pull/12752/head
							parent
							
								
									18707d365b
								
							
						
					
					
						commit
						518ae3fa09
					
				| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
- hosts: custom
 | 
			
		||||
  gather_facts: no
 | 
			
		||||
  vars:
 | 
			
		||||
    ansible_connection: local
 | 
			
		||||
    ansible_shell_type: sh
 | 
			
		||||
 | 
			
		||||
  tasks:
 | 
			
		||||
    - name: Test asset connection (telnet)
 | 
			
		||||
      telnet_ping:
 | 
			
		||||
        login_host: "{{ jms_asset.address }}"
 | 
			
		||||
        login_port: "{{ jms_asset.port }}"
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,16 @@
 | 
			
		|||
id: ping_by_telnet
 | 
			
		||||
name: "{{ 'Ping by telnet' | trans }}"
 | 
			
		||||
category:
 | 
			
		||||
  - device
 | 
			
		||||
  - host
 | 
			
		||||
type:
 | 
			
		||||
  - all
 | 
			
		||||
method: ping
 | 
			
		||||
protocol: telnet
 | 
			
		||||
priority: 50
 | 
			
		||||
 | 
			
		||||
i18n:
 | 
			
		||||
  Ping by telnet:
 | 
			
		||||
    zh: '使用 Python 模块 telnet 测试主机可连接性'
 | 
			
		||||
    en: 'Ping by telnet module'
 | 
			
		||||
    ja: 'Pythonモジュールtelnetを使用したホスト接続性のテスト'
 | 
			
		||||
| 
						 | 
				
			
			@ -7,7 +7,7 @@ __metaclass__ = type
 | 
			
		|||
 | 
			
		||||
DOCUMENTATION = '''
 | 
			
		||||
---
 | 
			
		||||
module: custom_ssh_ping
 | 
			
		||||
module: ssh_ping
 | 
			
		||||
short_description: Use ssh to probe whether an asset is connectable
 | 
			
		||||
description:
 | 
			
		||||
    - Use ssh to probe whether an asset is connectable
 | 
			
		||||
| 
						 | 
				
			
			@ -16,7 +16,7 @@ description:
 | 
			
		|||
EXAMPLES = '''
 | 
			
		||||
- name: >
 | 
			
		||||
    Ping asset server.
 | 
			
		||||
  custom_ssh_ping:
 | 
			
		||||
  ssh_ping:
 | 
			
		||||
    login_host: 127.0.0.1
 | 
			
		||||
    login_port: 22
 | 
			
		||||
    login_user: jms
 | 
			
		||||
| 
						 | 
				
			
			@ -25,15 +25,10 @@ EXAMPLES = '''
 | 
			
		|||
 | 
			
		||||
RETURN = '''
 | 
			
		||||
is_available:
 | 
			
		||||
  description: MongoDB server availability.
 | 
			
		||||
  description: Ping server availability.
 | 
			
		||||
  returned: always
 | 
			
		||||
  type: bool
 | 
			
		||||
  sample: true
 | 
			
		||||
conn_err_msg:
 | 
			
		||||
  description: Connection error message.
 | 
			
		||||
  returned: always
 | 
			
		||||
  type: str
 | 
			
		||||
  sample: ''
 | 
			
		||||
'''
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,70 @@
 | 
			
		|||
#!/usr/bin/python
 | 
			
		||||
 | 
			
		||||
from __future__ import absolute_import, division, print_function
 | 
			
		||||
 | 
			
		||||
__metaclass__ = type
 | 
			
		||||
 | 
			
		||||
DOCUMENTATION = '''
 | 
			
		||||
---
 | 
			
		||||
module: telnet_ping
 | 
			
		||||
short_description: Use telnet to probe whether an asset is connectable
 | 
			
		||||
description:
 | 
			
		||||
    - Use telnet to probe whether an asset is connectable
 | 
			
		||||
'''
 | 
			
		||||
 | 
			
		||||
EXAMPLES = '''
 | 
			
		||||
- name: >
 | 
			
		||||
    Telnet asset server.
 | 
			
		||||
  telnet_ping:
 | 
			
		||||
    login_host: localhost
 | 
			
		||||
    login_port: 22
 | 
			
		||||
'''
 | 
			
		||||
 | 
			
		||||
RETURN = '''
 | 
			
		||||
is_available:
 | 
			
		||||
  description: Telnet server availability.
 | 
			
		||||
  returned: always
 | 
			
		||||
  type: bool
 | 
			
		||||
  sample: true
 | 
			
		||||
'''
 | 
			
		||||
 | 
			
		||||
import telnetlib
 | 
			
		||||
 | 
			
		||||
from ansible.module_utils.basic import AnsibleModule
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# =========================================
 | 
			
		||||
# Module execution.
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
def common_argument_spec():
 | 
			
		||||
    options = dict(
 | 
			
		||||
        login_host=dict(type='str', required=False, default='localhost'),
 | 
			
		||||
        login_port=dict(type='int', required=False, default=22),
 | 
			
		||||
        timeout=dict(type='int', required=False, default=10),
 | 
			
		||||
    )
 | 
			
		||||
    return options
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    options = common_argument_spec()
 | 
			
		||||
    module = AnsibleModule(argument_spec=options, supports_check_mode=True, )
 | 
			
		||||
 | 
			
		||||
    result = {
 | 
			
		||||
        'changed': False, 'is_available': True
 | 
			
		||||
    }
 | 
			
		||||
    host = module.params['login_host']
 | 
			
		||||
    port = module.params['login_port']
 | 
			
		||||
    timeout = module.params['timeout']
 | 
			
		||||
    try:
 | 
			
		||||
        client = telnetlib.Telnet(host, port, timeout=timeout)
 | 
			
		||||
        client.close()
 | 
			
		||||
    except Exception as err: # noqa
 | 
			
		||||
        result['is_available'] = False
 | 
			
		||||
        module.fail_json(msg='Unable to connect to asset: %s' % err)
 | 
			
		||||
 | 
			
		||||
    return module.exit_json(**result)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    main()
 | 
			
		||||
		Loading…
	
		Reference in New Issue