ansible api fix

pull/26/head
ibuler 2015-12-09 14:50:48 +08:00
parent 16b94c1089
commit d3fd9e05ca
5 changed files with 30 additions and 12 deletions

View File

@ -585,7 +585,6 @@ class Nav(object):
else: else:
res = gen_resource({'user': self.user, 'asset': assets, 'role': role}, perm=self.user_perm) res = gen_resource({'user': self.user, 'asset': assets, 'role': role}, perm=self.user_perm)
runner = MyRunner(res) runner = MyRunner(res)
logger.debug("批量执行res: %s" % res)
asset_name_str = '' asset_name_str = ''
print "匹配主机:" print "匹配主机:"
for inv in runner.inventory.get_hosts(pattern=pattern): for inv in runner.inventory.get_hosts(pattern=pattern):

View File

@ -159,12 +159,16 @@ class MyRunner(MyInventory):
if contacted: if contacted:
for host, info in contacted.items(): for host, info in contacted.items():
if info.get('failed'): if info.get('invocation').get('module_name') in ['raw', 'shell', 'command', 'script']:
result['failed'][host] = info.get('msg') + info.get('stderr', '') if info.get('rc') == 0:
elif info.get('stderr') and info.get('module_name') in ['shell', 'command', 'raw']: result['ok'][host] = info.get('stdout') + info.get('stderr')
result['failed'][host] = info.get('stderr') + str(info.get('warnings')) else:
result['failed'][host] = info.get('stdout') + info.get('stderr')
else: 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 return result
@ -369,7 +373,6 @@ class MyTask(MyRunner):
for role in role_list: for role in role_list:
sudo_user[role.name] = ','.join(sudo_alias.keys()) sudo_user[role.name] = ','.join(sudo_alias.keys())
print sudo_alias, sudo_user
sudo_j2 = get_template('jperm/role_sudo.j2') sudo_j2 = get_template('jperm/role_sudo.j2')
sudo_content = sudo_j2.render(Context({"sudo_alias": sudo_alias, "sudo_user": sudo_user})) sudo_content = sudo_j2.render(Context({"sudo_alias": sudo_alias, "sudo_user": sudo_user}))

View File

@ -526,14 +526,18 @@ def perm_sudo_add(request):
if request.method == "POST": if request.method == "POST":
# 获取参数: name, comment # 获取参数: name, comment
name = request.POST.get("sudo_name").strip() name = request.POST.get("sudo_name").strip().upper()
comment = request.POST.get("sudo_comment").strip() comment = request.POST.get("sudo_comment").strip()
commands = request.POST.get("sudo_commands").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): if get_object(PermSudo, name=name):
error = 'Sudo别名 %s已经存在' % name error = 'Sudo别名 %s已经存在' % name
else: else:
sudo = PermSudo(name=name.strip(), comment=comment, commands=commands.strip()) sudo = PermSudo(name=name.strip(), comment=comment, commands=commands)
sudo.save() sudo.save()
msg = u"添加Sudo命令别名: %s" % name msg = u"添加Sudo命令别名: %s" % name
# 渲染数据 # 渲染数据
@ -555,11 +559,16 @@ def perm_sudo_edit(request):
sudo = PermSudo.objects.get(id=sudo_id) sudo = PermSudo.objects.get(id=sudo_id)
if request.method == "POST": if request.method == "POST":
name = request.POST.get("sudo_name") name = request.POST.get("sudo_name").upper()
commands = request.POST.get("sudo_commands") commands = request.POST.get("sudo_commands")
comment = request.POST.get("sudo_comment") 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.name = name.strip()
sudo.commands = commands.strip() sudo.commands = commands
sudo.comment = comment sudo.comment = comment
sudo.save() sudo.save()

View File

@ -48,6 +48,13 @@ def set_log(level):
return logger_f 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): def get_asset_info(asset):
""" """
获取资产的相关管理账号端口等信息 获取资产的相关管理账号端口等信息

View File

@ -69,7 +69,7 @@ $('#sudoForm').validator({
timely: 2, timely: 2,
theme: "yellow_right_effect", theme: "yellow_right_effect",
rules: { rules: {
check_name: [/^\w{2,20}$/, '大写字母数字和下划线,2-20位'] check_name: [/^\w{2,20}$/, '大写字母,2-20位']
}, },
fields: { fields: {