Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
51c530c1
Commit
51c530c1
authored
Oct 28, 2016
by
Administrator
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[future] ansible运行结果存入数据库
parent
96bc1cd8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
33 deletions
+45
-33
settings.py
apps/jumpserver/settings.py
+4
-0
ansible_api.py
apps/ops/ansible_api.py
+20
-10
models.py
apps/ops/models.py
+21
-23
No files found.
apps/jumpserver/settings.py
View file @
51c530c1
...
...
@@ -210,6 +210,10 @@ LOGGING = {
'jumpserver.users.view'
:
{
'handlers'
:
[
'console'
,
'file'
],
'level'
:
LOG_LEVEL
,
},
'jumpserver.ops.ansible_api'
:
{
'handlers'
:
[
'console'
,
'file'
],
'level'
:
LOG_LEVEL
,
}
}
}
...
...
apps/ops/ansible_api.py
View file @
51c530c1
...
...
@@ -229,10 +229,20 @@ class CallbackModule(CallbackBase):
def
__play_uuid
(
self
):
return
self
.
results
[
-
1
][
'uuid'
]
def
save_task_result
(
self
,
result
):
def
save_task_result
(
self
,
result
,
status
):
try
:
task
=
AnsibleTask
.
objects
.
get
(
uuid
=
self
.
__task_uuid
)
host_result
=
AnsibleHostResult
(
task
=
task
,
name
=
result
.
_host
)
if
status
==
"failed"
:
host_result
.
failed
=
json
.
dumps
(
result
.
_result
)
elif
status
==
"unreachable"
:
host_result
.
unreachable
=
json
.
dumps
(
result
.
_result
)
elif
status
==
"skipped"
:
host_result
.
skipped
=
json
.
dumps
(
result
.
_result
)
elif
status
==
"success"
:
host_result
.
success
=
json
.
dumps
(
result
.
_result
)
else
:
logger
.
error
(
"No such status(failed|unreachable|skipped|success), please check!"
)
host_result
.
save
()
except
Exception
as
e
:
logger
.
error
(
"Save Ansible host result to database error!,
%
s"
%
e
.
message
)
...
...
@@ -247,17 +257,17 @@ class CallbackModule(CallbackBase):
logger
.
error
(
"Save Ansible host result to database error!,
%
s"
%
e
.
message
)
def
v2_runner_on_failed
(
self
,
result
,
ignore_errors
=
False
):
self
.
save_task_result
(
result
)
self
.
save_task_result
(
result
,
"failed"
)
host
=
result
.
_host
self
.
results
[
-
1
][
'tasks'
][
-
1
][
'failed'
][
host
.
name
]
=
result
.
_result
def
v2_runner_on_unreachable
(
self
,
result
):
self
.
save_task_result
(
result
)
self
.
save_task_result
(
result
,
"unreachable"
)
host
=
result
.
_host
self
.
results
[
-
1
][
'tasks'
][
-
1
][
'unreachable'
][
host
.
name
]
=
result
.
_result
def
v2_runner_on_skipped
(
self
,
result
):
self
.
save_task_result
(
result
)
self
.
save_task_result
(
result
,
"skipped"
)
host
=
result
.
_host
self
.
results
[
-
1
][
'tasks'
][
-
1
][
'skipped'
][
host
.
name
]
=
result
.
_result
...
...
@@ -266,7 +276,7 @@ class CallbackModule(CallbackBase):
self
.
results
[
-
1
][
'tasks'
][
-
1
][
'no_hosts'
][
'msg'
]
=
"no host to run this task"
def
v2_runner_on_ok
(
self
,
result
):
self
.
save_task_result
(
result
)
self
.
save_task_result
(
result
,
"success"
)
host
=
result
.
_host
self
.
results
[
-
1
][
'tasks'
][
-
1
][
'success'
][
host
.
name
]
=
result
.
_result
...
...
@@ -421,11 +431,10 @@ class ADHocRunner(InventoryMixin):
play
.
status_code
=
ext_code
play
.
save
()
except
Exception
as
e
:
print
e
.
message
logger
.
error
(
"Update Ansible Play Status into database error!,
%
s"
%
e
.
message
)
def
run
(
self
):
"""执行ADHoc, 执行完后, 修改AnsiblePlay的状态
"""执行ADHoc, 执行完后, 修改AnsiblePlay的状态
为完成状态.
"""
tqm
=
None
# TODO:日志和结果分析
...
...
@@ -439,11 +448,12 @@ class ADHocRunner(InventoryMixin):
passwords
=
self
.
passwords
)
ext_code
=
tqm
.
run
(
self
.
play
)
result
=
json
.
dumps
(
self
.
results_callback
.
results
)
result
=
self
.
results_callback
.
results
self
.
update_db_play
(
result
,
ext_code
)
return
ext_code
,
result
ret
=
json
.
dumps
(
result
)
return
ext_code
,
ret
finally
:
if
tqm
:
...
...
@@ -478,7 +488,7 @@ def test_run():
"gather_facts"
:
"no"
,
"tasks"
:
[
dict
(
action
=
dict
(
module
=
'setup'
)),
dict
(
action
=
dict
(
module
=
'command'
,
args
=
'ls'
))
dict
(
action
=
dict
(
module
=
'command'
,
args
=
'ls
ss
'
))
]
}
hoc
=
ADHocRunner
(
conf
,
play_source
,
*
assets
)
...
...
apps/ops/models.py
View file @
51c530c1
...
...
@@ -2,6 +2,7 @@
from
__future__
import
unicode_literals
,
absolute_import
import
logging
import
json
from
django.db
import
models
from
django.utils.translation
import
ugettext_lazy
as
_
...
...
@@ -48,28 +49,25 @@ class AnsibleHostResult(models.Model):
def
__unicode__
(
self
):
return
"AnsibleHostResult:
%
s<
%
s>"
%
(
self
.
name
,
str
(
self
.
status
))
@property
def
is_failed
(
self
):
pass
def
result
(
self
):
pass
class
AnsibleSetup
(
models
.
Model
):
task
=
models
.
ForeignKey
(
AnsibleTask
,
related_name
=
'host_results'
,
blank
=
True
)
name
=
models
.
CharField
(
max_length
=
128
,
blank
=
True
,
verbose_name
=
_
(
'Name'
))
status
=
models
.
BooleanField
(
blank
=
True
,
default
=
False
,
verbose_name
=
_
(
'Status'
))
success
=
models
.
TextField
(
blank
=
True
,
verbose_name
=
_
(
'Success'
))
skipped
=
models
.
TextField
(
blank
=
True
,
verbose_name
=
_
(
'Skipped'
))
failed
=
models
.
TextField
(
blank
=
True
,
verbose_name
=
_
(
'Failed'
))
unreachable
=
models
.
TextField
(
blank
=
True
,
verbose_name
=
_
(
'Unreachable'
))
no_host
=
models
.
TextField
(
blank
=
True
,
verbose_name
=
_
(
'NoHost'
))
def
__unicode__
(
self
):
return
"AnsibleHostResult:
%
s<
%
s>"
%
(
self
.
name
,
str
(
self
.
status
))
if
self
.
failed
or
self
.
unreachable
or
self
.
no_host
:
return
True
return
False
@property
def
success_data
(
self
):
if
self
.
success
:
return
json
.
loads
(
self
.
success
)
elif
self
.
skipped
:
return
json
.
loads
(
self
.
skipped
)
@property
def
failed_data
(
self
):
if
self
.
failed
:
return
json
.
loads
(
self
.
failed
)
elif
self
.
unreachable
:
return
json
.
loads
(
self
.
unreachable
)
elif
self
.
no_host
:
return
{
"msg"
:
self
.
no_host
}
def
is_failed
(
self
):
pass
def
result
(
self
):
pass
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment