diff --git a/connect.py b/connect.py index 26b6c8709..dbd0c3e06 100644 --- a/connect.py +++ b/connect.py @@ -585,7 +585,6 @@ class Nav(object): else: res = gen_resource({'user': self.user, 'asset': assets, 'role': role}, perm=self.user_perm) runner = MyRunner(res) - logger.debug("批量执行res: %s" % res) asset_name_str = '' print "匹配主机:" for inv in runner.inventory.get_hosts(pattern=pattern): diff --git a/jperm/ansible_api.py b/jperm/ansible_api.py index 077fa5dfe..0b40d93a5 100644 --- a/jperm/ansible_api.py +++ b/jperm/ansible_api.py @@ -159,12 +159,16 @@ class MyRunner(MyInventory): if contacted: for host, info in contacted.items(): - if info.get('failed'): - result['failed'][host] = info.get('msg') + info.get('stderr', '') - elif info.get('stderr') and info.get('module_name') in ['shell', 'command', 'raw']: - result['failed'][host] = info.get('stderr') + str(info.get('warnings')) + if info.get('invocation').get('module_name') in ['raw', 'shell', 'command', 'script']: + if info.get('rc') == 0: + result['ok'][host] = info.get('stdout') + info.get('stderr') + else: + result['failed'][host] = info.get('stdout') + info.get('stderr') else: - result['ok'][host] = info.get('stdout') + if info.get('failed'): + result['failed'][host] = info.get('msg') + else: + result['ok'][host] = info.get('changed') return result @@ -369,7 +373,6 @@ class MyTask(MyRunner): for role in role_list: sudo_user[role.name] = ','.join(sudo_alias.keys()) - print sudo_alias, sudo_user sudo_j2 = get_template('jperm/role_sudo.j2') sudo_content = sudo_j2.render(Context({"sudo_alias": sudo_alias, "sudo_user": sudo_user})) diff --git a/jperm/views.py b/jperm/views.py index 6e5bdb658..feaeada44 100644 --- a/jperm/views.py +++ b/jperm/views.py @@ -526,14 +526,18 @@ def perm_sudo_add(request): if request.method == "POST": # 获取参数: name, comment - name = request.POST.get("sudo_name").strip() + name = request.POST.get("sudo_name").strip().upper() comment = request.POST.get("sudo_comment").strip() commands = request.POST.get("sudo_commands").strip() + pattern = re.compile(r'[ \n,\r]') + commands = ', '.join(list_drop_str(pattern.split(commands), u'')) + logger.debug(u'添加sudo %s: %s' % (name, commands)) + if get_object(PermSudo, name=name): error = 'Sudo别名 %s已经存在' % name else: - sudo = PermSudo(name=name.strip(), comment=comment, commands=commands.strip()) + sudo = PermSudo(name=name.strip(), comment=comment, commands=commands) sudo.save() msg = u"添加Sudo命令别名: %s" % name # 渲染数据 @@ -555,11 +559,16 @@ def perm_sudo_edit(request): sudo = PermSudo.objects.get(id=sudo_id) if request.method == "POST": - name = request.POST.get("sudo_name") + name = request.POST.get("sudo_name").upper() commands = request.POST.get("sudo_commands") comment = request.POST.get("sudo_comment") + + pattern = re.compile(r'[ \n,\r]') + commands = ', '.join(list_drop_str(pattern.split(commands), u'')).strip() + logger.debug(u'添加sudo %s: %s' % (name, commands)) + sudo.name = name.strip() - sudo.commands = commands.strip() + sudo.commands = commands sudo.comment = comment sudo.save() diff --git a/jumpserver/api.py b/jumpserver/api.py index af7924964..723a4e9e7 100644 --- a/jumpserver/api.py +++ b/jumpserver/api.py @@ -48,6 +48,13 @@ def set_log(level): return logger_f +def list_drop_str(a_list, a_str): + for i in a_list: + if i == a_str: + a_list.remove(a_str) + return a_list + + def get_asset_info(asset): """ 获取资产的相关管理账号端口等信息 diff --git a/templates/jperm/perm_sudo_add.html b/templates/jperm/perm_sudo_add.html index f99587596..aeef9a53f 100644 --- a/templates/jperm/perm_sudo_add.html +++ b/templates/jperm/perm_sudo_add.html @@ -69,7 +69,7 @@ $('#sudoForm').validator({ timely: 2, theme: "yellow_right_effect", rules: { - check_name: [/^\w{2,20}$/, '大小写字母数字和下划线,2-20位'] + check_name: [/^\w{2,20}$/, '大写字母,2-20位'] }, fields: {