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
e8d8f7c4
Commit
e8d8f7c4
authored
Aug 30, 2016
by
yumaojun03
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ops: ansible_api add ansible api 2.0 adhoc runner
parent
f6b2abb1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
11 deletions
+42
-11
ansible_api.py
apps/ops/ansible_api.py
+42
-11
No files found.
apps/ops/ansible_api.py
View file @
e8d8f7c4
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
os
import
os
import
json
from
ansible.executor.task_queue_manager
import
TaskQueueManager
from
ansible.executor.task_queue_manager
import
TaskQueueManager
from
ansible.inventory
import
Inventory
,
Host
,
Group
from
ansible.inventory
import
Inventory
,
Host
,
Group
from
ansible.vars
import
VariableManager
from
ansible.vars
import
VariableManager
...
@@ -10,6 +11,7 @@ from ansible.executor import playbook_executor
...
@@ -10,6 +11,7 @@ from ansible.executor import playbook_executor
from
ansible.utils.display
import
Display
from
ansible.utils.display
import
Display
from
ansible.playbook.play
import
Play
from
ansible.playbook.play
import
Play
import
ansible.constants
as
default_config
import
ansible.constants
as
default_config
from
ansible.plugins.callback
import
CallbackBase
class
AnsibleError
(
StandardError
):
class
AnsibleError
(
StandardError
):
...
@@ -142,7 +144,7 @@ class MyInventory(object):
...
@@ -142,7 +144,7 @@ class MyInventory(object):
if
key
not
in
[
"name"
,
"port"
,
"ip"
,
"username"
,
"password"
,
"key"
]:
if
key
not
in
[
"name"
,
"port"
,
"ip"
,
"username"
,
"password"
,
"key"
]:
host
.
set_variable
(
key
,
value
)
host
.
set_variable
(
key
,
value
)
for
g
in
self
.
groups
:
for
g
in
self
.
groups
:
if
g
.
name
==
asset
[
'group'
]
:
if
g
.
name
==
asset
.
get
(
'group'
,
'default'
)
:
g
.
add_host
(
host
)
g
.
add_host
(
host
)
def
validate
(
self
):
def
validate
(
self
):
...
@@ -151,6 +153,7 @@ class MyInventory(object):
...
@@ -151,6 +153,7 @@ class MyInventory(object):
def
gen_inventory
(
self
):
def
gen_inventory
(
self
):
self
.
validate
()
self
.
validate
()
i
=
Inventory
(
loader
=
self
.
loader
,
variable_manager
=
self
.
variable_manager
,
host_list
=
[])
i
=
Inventory
(
loader
=
self
.
loader
,
variable_manager
=
self
.
variable_manager
,
host_list
=
[])
self
.
__gen_group
()
for
g
in
self
.
groups
:
for
g
in
self
.
groups
:
i
.
add_group
(
g
)
i
.
add_group
(
g
)
self
.
variable_manager
.
set_inventory
(
i
)
self
.
variable_manager
.
set_inventory
(
i
)
...
@@ -223,7 +226,7 @@ class PlayBookRunner(object):
...
@@ -223,7 +226,7 @@ class PlayBookRunner(object):
class
ADHocRunner
(
object
):
class
ADHocRunner
(
object
):
"""ADHoc接口
"""ADHoc接口
"""
"""
def
__init__
(
self
,
inventory
,
config
,
play_data
,
become_pass
,
verbosity
=
0
):
def
__init__
(
self
,
inventory
,
config
,
become_pass
=
None
,
verbosity
=
0
):
"""
"""
:param inventory: myinventory实例
:param inventory: myinventory实例
:param config: Config实例
:param config: Config实例
...
@@ -251,22 +254,34 @@ class ADHocRunner(object):
...
@@ -251,22 +254,34 @@ class ADHocRunner(object):
self
.
options
.
become_user
=
'root'
self
.
options
.
become_user
=
'root'
self
.
passwords
=
{
'become_pass'
:
become_pass
}
self
.
passwords
=
{
'become_pass'
:
become_pass
}
# 初始化
Play
# 初始化
callback插件
self
.
play
=
Play
()
.
load
(
play_data
,
variable_manager
=
inventory
.
variable_manager
,
loader
=
inventory
.
loader
)
# self.results_callback = ResultCallback(
)
self
.
inventory
=
inventory
.
inventory
# 初始化Play
play_source
=
{
"name"
:
"Ansible Play"
,
"hosts"
:
"*"
,
"gather_facts"
:
"no"
,
"tasks"
:
[
dict
(
action
=
dict
(
module
=
'shell'
,
args
=
'id'
),
register
=
'shell_out'
),
dict
(
action
=
dict
(
module
=
'debug'
,
args
=
dict
(
msg
=
'{{shell_out.stdout}}'
)))
]
}
self
.
play
=
Play
()
.
load
(
play_source
,
variable_manager
=
inventory
.
variable_manager
,
loader
=
inventory
.
loader
)
self
.
inventory
=
inventory
def
run
(
self
,
play_data
):
def
run
(
self
):
"""执行ADHoc 记录日志, 处理结果
"""执行ADHoc 记录日志, 处理结果
"""
"""
tqm
=
None
tqm
=
None
# TODO:日志和结果分析
try
:
try
:
# TODO: 日志和结果分析
tqm
=
TaskQueueManager
(
tqm
=
TaskQueueManager
(
inventory
=
self
.
inventory
,
inventory
=
self
.
inventory
.
inventory
,
variable_manager
=
self
.
inventory
.
variable_manager
,
variable_manager
=
self
.
inventory
.
variable_manager
,
loader
=
self
.
inventory
.
loader
,
loader
=
self
.
inventory
.
loader
,
stdout_callback
=
default_config
.
DEFAULT_STDOUT_CALLBACK
,
options
=
self
.
options
,
options
=
self
.
options
,
passwords
=
self
.
passwords
passwords
=
self
.
passwords
)
)
...
@@ -275,4 +290,21 @@ class ADHocRunner(object):
...
@@ -275,4 +290,21 @@ class ADHocRunner(object):
return
result
return
result
finally
:
finally
:
if
tqm
:
if
tqm
:
tqm
.
cleanup
()
tqm
.
cleanup
()
\ No newline at end of file
if
__name__
==
"__main__"
:
conf
=
Config
()
assets
=
[{
"name"
:
"localhost"
,
"ip"
:
"localhost"
,
"port"
:
"22"
,
"username"
:
"yumaojun"
,
"password"
:
"xxx"
,
"key"
:
"asset_private_key"
,
}]
inv
=
MyInventory
(
*
assets
)
print
inv
.
inventory
.
get_group
(
'default'
)
.
get_hosts
()
hoc
=
ADHocRunner
(
inv
,
conf
,
'xxx'
)
hoc
.
run
()
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