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
c234b5b2
Commit
c234b5b2
authored
Mar 06, 2017
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update AdHocRunner
parent
eb5a9dd2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
72 deletions
+61
-72
task.py
apps/ops/models/task.py
+12
-38
ansible_api.py
apps/ops/utils/ansible_api.py
+0
-0
callback.py
apps/ops/utils/callback.py
+4
-1
runner.py
apps/ops/utils/runner.py
+45
-33
No files found.
apps/ops/models/task.py
View file @
c234b5b2
...
...
@@ -10,49 +10,23 @@ from ops.models import TaskRecord
from
django.db
import
models
from
django.utils.translation
import
ugettext_lazy
as
_
__all__
=
[
"Task"
,
"SubTask"
]
logger
=
logging
.
getLogger
(
__name__
)
__all__
=
[
"Task"
]
class
Task
(
models
.
Model
):
record
=
models
.
OneToOneField
(
TaskRecord
)
name
=
models
.
CharField
(
max_length
=
128
,
blank
=
True
,
verbose_name
=
_
(
'Name'
))
is_gather_facts
=
models
.
BooleanField
(
default
=
False
,
verbose_name
=
_
(
'Is Gather Ansible Facts'
))
assets
=
models
.
ManyToManyField
(
Asset
,
related_name
=
'tasks'
)
"""
Ansible 的Task
"""
name
=
models
.
CharField
(
max_length
=
128
,
verbose_name
=
_
(
'Task name'
))
module_name
=
models
.
CharField
(
max_length
=
128
,
verbose_name
=
_
(
'Task module'
))
module_args
=
models
.
CharField
(
max_length
=
512
,
blank
=
True
,
verbose_name
=
_
(
"Module args"
))
def
__unicode__
(
self
):
return
"
%
s"
%
self
.
name
@property
def
ansible_assets
(
self
):
return
[]
def
run
(
self
):
from
ops.utils.ansible_api
import
ADHocRunner
,
Options
conf
=
Options
()
gather_facts
=
"yes"
if
self
.
is_gather_facts
else
"no"
play_source
=
{
"name"
:
"Ansible Play"
,
"hosts"
:
"default"
,
"gather_facts"
:
gather_facts
,
"tasks"
:
[
dict
(
action
=
dict
(
module
=
'ping'
)),
]
}
hoc
=
ADHocRunner
(
conf
,
play_source
,
*
self
.
ansible_assets
)
uuid
=
"tasker-"
+
uuid4
()
.
hex
ext_code
,
result
=
hoc
.
run
(
"test_task"
,
uuid
)
print
(
ext_code
)
print
(
result
)
class
SubTask
(
models
.
Model
):
task
=
models
.
ForeignKey
(
Task
,
related_name
=
'sub_tasks'
,
verbose_name
=
_
(
'Ansible Task'
))
module_name
=
models
.
CharField
(
max_length
=
128
,
verbose_name
=
_
(
'Ansible Module Name'
))
module_args
=
models
.
CharField
(
max_length
=
512
,
blank
=
True
,
verbose_name
=
_
(
"Ansible Module Args"
))
register
=
models
.
CharField
(
max_length
=
128
,
blank
=
True
,
verbose_name
=
_
(
'Ansible Task Register'
))
def
__unicode__
(
self
):
return
"
%
s
%
s"
%
(
self
.
module_name
,
self
.
module_args
)
class
Play
(
models
.
Model
):
"""
Playbook 模板, 定义好Template后生成 Playbook
"""
apps/ops/utils/ansible_api.py
deleted
100644 → 0
View file @
eb5a9dd2
This diff is collapsed.
Click to expand it.
apps/ops/utils/callback.py
View file @
c234b5b2
...
...
@@ -12,7 +12,10 @@ class AdHocResultCallback(CallbackBase):
super
(
AdHocResultCallback
,
self
)
.
__init__
(
display
)
def
gather_result
(
self
,
n
,
res
):
self
.
result_q
[
n
]
.
update
({
res
.
_host
.
name
:
res
.
_result
})
if
res
.
_host
.
name
in
self
.
result_q
[
n
]:
self
.
result_q
[
n
][
res
.
_host
.
name
]
.
append
(
res
.
_result
)
else
:
self
.
result_q
[
n
][
res
.
_host
.
name
]
=
[
res
.
_result
]
def
v2_runner_on_ok
(
self
,
result
):
self
.
gather_result
(
"contacted"
,
result
)
...
...
apps/ops/utils/runner.py
View file @
c234b5b2
# ~*~ coding: utf-8 ~*~
# ~*~ coding: utf-8 ~*~
# from __future__ import unicode_literals, print_function
import
os
from
collections
import
namedtuple
import
sys
from
collections
import
namedtuple
,
defaultdict
from
ansible.executor.task_queue_manager
import
TaskQueueManager
from
ansible.vars
import
VariableManager
from
ansible.parsing.dataloader
import
DataLoader
from
ansible.executor.playbook_executor
import
PlaybookExecutor
from
ansible.playbook.play
import
Play
from
ansible.plugins.callback
import
CallbackBase
import
ansible.constants
as
C
from
ansible.utils.vars
import
load_extra_vars
from
ansible.utils.vars
import
load_options_vars
...
...
@@ -128,7 +125,7 @@ class PlayBookRunner(object):
return
self
.
callbackmodule
.
output
class
A
D
HocRunner
(
object
):
class
A
d
HocRunner
(
object
):
"""
ADHoc接口
"""
...
...
@@ -141,12 +138,8 @@ class ADHocRunner(object):
def
__init__
(
self
,
hosts
=
C
.
DEFAULT_HOST_LIST
,
task_name
=
'Ansible Ad-hoc'
,
module_name
=
C
.
DEFAULT_MODULE_NAME
,
# * command
module_args
=
C
.
DEFAULT_MODULE_ARGS
,
# * 'cmd args'
forks
=
C
.
DEFAULT_FORKS
,
# 5
timeout
=
C
.
DEFAULT_TIMEOUT
,
# SSH timeout = 10s
pattern
=
"all"
,
# all
remote_user
=
C
.
DEFAULT_REMOTE_USER
,
# root
module_path
=
None
,
# dirs of custome modules
connection_type
=
"smart"
,
...
...
@@ -159,12 +152,9 @@ class ADHocRunner(object):
private_key_file
=
None
,
gather_facts
=
'no'
):
self
.
pattern
=
pattern
self
.
pattern
=
''
self
.
variable_manager
=
VariableManager
()
self
.
loader
=
DataLoader
()
self
.
module_name
=
module_name
self
.
module_args
=
module_args
self
.
check_module_args
()
self
.
gather_facts
=
gather_facts
self
.
results_callback
=
AdHocResultCallback
()
self
.
options
=
self
.
Options
(
...
...
@@ -186,18 +176,41 @@ class ADHocRunner(object):
self
.
passwords
=
passwords
or
{}
self
.
inventory
=
JMSInventory
(
hosts
)
self
.
variable_manager
.
set_inventory
(
self
.
inventory
)
self
.
tasks
=
[]
self
.
play_source
=
None
self
.
play
=
None
self
.
runner
=
None
@staticmethod
def
check_module_args
(
module_name
,
module_args
=
''
):
if
module_name
in
C
.
MODULE_REQUIRE_ARGS
and
not
module_args
:
err
=
"No argument passed to '
%
s' module."
%
module_name
print
(
err
)
return
False
return
True
def
run
(
self
,
task_tuple
,
pattern
=
'all'
,
task_name
=
'Ansible Ad-hoc'
):
"""
:param task_tuple: (('shell', 'ls'), ('ping', ''))
:param pattern:
:param task_name:
:return:
"""
for
module
,
args
in
task_tuple
:
if
not
self
.
check_module_args
(
module
,
args
):
return
self
.
tasks
.
append
(
dict
(
action
=
dict
(
module
=
module
,
args
=
args
,
))
)
self
.
play_source
=
dict
(
name
=
task_name
,
hosts
=
self
.
pattern
,
hosts
=
pattern
,
gather_facts
=
self
.
gather_facts
,
tasks
=
[
dict
(
action
=
dict
(
module
=
self
.
module_name
,
args
=
self
.
module_args
,
)
)
]
tasks
=
self
.
tasks
)
self
.
play
=
Play
()
.
load
(
...
...
@@ -215,12 +228,6 @@ class ADHocRunner(object):
stdout_callback
=
self
.
results_callback
,
)
def
check_module_args
(
self
):
if
self
.
module_name
in
C
.
MODULE_REQUIRE_ARGS
and
not
self
.
module_args
:
err
=
"No argument passed to '
%
s' module."
%
self
.
module_name
raise
AnsibleError
(
err
)
def
run
(
self
):
if
not
self
.
inventory
.
list_hosts
(
"all"
):
raise
AnsibleError
(
"Inventory is empty."
)
...
...
@@ -242,9 +249,13 @@ class ADHocRunner(object):
self
.
loader
.
cleanup_all_tmp_files
()
def
clean_result
(
self
):
failed
=
self
.
results_callback
.
result_q
[
'dark'
]
.
keys
()
success
=
self
.
results_callback
.
result_q
[
'contacted'
]
.
keys
()
return
{
'failed'
:
failed
,
'success'
:
success
}
result
=
defaultdict
(
dict
)
for
host
,
msgs
in
self
.
results_callback
.
result_q
[
'contacted'
]
.
items
():
result
[
host
][
'success'
]
=
len
(
msgs
)
for
host
,
msgs
in
self
.
results_callback
.
result_q
[
'dark'
]
.
items
():
result
[
host
][
'failed'
]
=
len
(
msgs
)
return
result
def
test_run
():
...
...
@@ -257,8 +268,9 @@ def test_run():
"password"
:
"redhat"
,
},
]
hoc
=
ADHocRunner
(
module_name
=
'shell'
,
module_args
=
'ls'
,
hosts
=
assets
)
ret
=
hoc
.
run
()
task_tuple
=
((
'shell'
,
'ls'
),
(
'ping'
,
''
))
hoc
=
AdHocRunner
(
hosts
=
assets
)
ret
=
hoc
.
run
(
task_tuple
)
print
(
ret
)
play
=
PlayBookRunner
(
assets
,
playbook_path
=
'/tmp/some.yml'
)
...
...
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