Skip to content

Commit

Permalink
添加ansible debug功能
Browse files Browse the repository at this point in the history
  • Loading branch information
welliamcao committed Dec 2, 2017
1 parent d661159 commit e4f12f5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
11 changes: 10 additions & 1 deletion OpsManage/templates/apps/apps_model.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,16 @@ <h1 class="page-header"><i class="fa fa-gears"></i><code>Ansible</code>模块管
<div class="col-sm-6">
<input id="ansible_args" type="text" class="form-control" placeholder="输入模块参数" name="ansible_agrs" value="">
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Debug</label>
<div class="col-sm-6">
<select class="form-control" name="ansible_debug">
<option value="off" name="ansible_model">关闭</option>
<option value="on" name="ansible_model">开启</option>
</select>
</div>
</div>
</fieldset>
<legend></legend>
<div class="form-group">
Expand Down
12 changes: 9 additions & 3 deletions OpsManage/templates/apps/apps_playbook.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ <h1 class="page-header"><i class="fa fa-gears"></i><code>Ansible</code>剧本管
<div class="col-lg-5">
<legend><i class="fa fa-paper-plane"></i>Ansible服务器</legend>
<form role="form" id="ansiblePlayBookRun" class="main form-horizontal" >{% csrf_token %}
<fieldset>
<div class="form-group">
<label class="col-sm-2 control-label">剧本名称</label>
<div class="col-sm-6">
Expand Down Expand Up @@ -77,8 +76,15 @@ <h1 class="page-header"><i class="fa fa-gears"></i><code>Ansible</code>剧本管
{% endif %}
</div>
</div>
</fieldset>
</fieldset>
<div class="form-group">
<label class="col-sm-2 control-label">Debug</label>
<div class="col-sm-6">
<select class="form-control" name="ansible_debug">
<option value="off" name="ansible_model">关闭</option>
<option value="on" name="ansible_model">开启</option>
</select>
</div>
</div>
<legend></legend>
<div class="form-group">
<label class="col-sm-3 control-label"></label>
Expand Down
2 changes: 1 addition & 1 deletion OpsManage/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ <h4 class="timeline-title">{{ ds.order_subject }}</h4>

</div>
<!-- /.list-group -->
<a href="/assets_log/" class="btn btn-default btn-block">查看操作记录</a>
<a href="/assets_log/1/" class="btn btn-default btn-block">查看操作记录</a>
</div>
<!-- /.panel-body -->
</div>
Expand Down
12 changes: 7 additions & 5 deletions OpsManage/utils/ansible_api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from OpsManage.data.DsMySQL import AnsibleSaveResult



class MyInventory(Inventory):
"""
this is my ansible inventory object.
Expand Down Expand Up @@ -352,13 +353,13 @@ def __init__(self,resource,redisKey=None,logId=None,*args, **kwargs):
self.options = None
self.passwords = None
self.callback = None
self.__initializeData()
self.__initializeData(kwargs)
self.results_raw = {}
self.redisKey = redisKey
self.logId = logId


def __initializeData(self):
def __initializeData(self,kwargs):
""" 初始化ansible """
Options = namedtuple('Options', ['connection','module_path', 'forks', 'timeout', 'remote_user',
'ask_pass', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
Expand All @@ -368,9 +369,10 @@ def __initializeData(self):
self.variable_manager = VariableManager()
self.loader = DataLoader()
self.options = Options(connection='smart', module_path=None, forks=100, timeout=10,
remote_user='root', ask_pass=False, private_key_file=None, ssh_common_args=None, ssh_extra_args=None,
sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None,
become_user='root', ask_value_pass=False, verbosity=None, check=False, listhosts=False,
remote_user='root', ask_pass=False, private_key_file=None, ssh_common_args=None,
ssh_extra_args=None,sftp_extra_args=None, scp_extra_args=None, become=None,
become_method=None,become_user='root', ask_value_pass=False,
verbosity=kwargs.get('verbosity',None),check=False, listhosts=False,
listtasks=False, listtags=False, syntax=False)

self.passwords = dict(sshpass=None, becomepass=None)
Expand Down
6 changes: 4 additions & 2 deletions OpsManage/views/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def apps_model(request):
logId = AnsibleRecord.Model.insert(user=str(request.user),ans_model=model_name,ans_server=','.join(sList),ans_args=request.POST.get('ansible_agrs',None))
DsRedis.OpsAnsibleModel.delete(redisKey)
DsRedis.OpsAnsibleModel.lpush(redisKey, "[Start] Ansible Model: {model} ARGS:{args}".format(model=model_name,args=request.POST.get('ansible_agrs',"None")))
ANS = ANSRunner(resource,redisKey,logId)
if request.POST.get('ansible_debug') == 'on':ANS = ANSRunner(resource,redisKey,logId,verbosity=4)
else:ANS = ANSRunner(resource,redisKey,logId)
ANS.run_model(host_list=sList,module_name=model_name,module_args=request.POST.get('ansible_agrs',""))
DsRedis.OpsAnsibleModel.lpush(redisKey, "[Done] Ansible Done.")
return JsonResponse({'msg':"操作成功","code":200,'data':[]})
Expand Down Expand Up @@ -272,7 +273,8 @@ def apps_playbook_run(request,pid):
logId = AnsibleRecord.PlayBook.insert(user=str(request.user),ans_id=playbook.id,ans_name=playbook.playbook_name,
ans_content="执行Ansible剧本",ans_server=','.join(sList))
#执行ansible playbook
ANS = ANSRunner(resource,redisKey=playbook.playbook_uuid,logId=logId)
if request.POST.get('ansible_debug') == 'on':ANS = ANSRunner(resource,redisKey=playbook.playbook_uuid,logId=logId,verbosity=4)
else:ANS = ANSRunner(resource,redisKey=playbook.playbook_uuid,logId=logId)
ANS.run_playbook(host_list=sList, playbook_path=playbook_file,extra_vars=playbook_vars)
#获取结果
result = ANS.get_playbook_result()
Expand Down

0 comments on commit e4f12f5

Please sign in to comment.