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
df9d879f
Commit
df9d879f
authored
9 years ago
by
Zi Chuanxiu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ansible api base complete...
parent
dd4ac4e6
master
auditor_jym
audits
dev
dev_beta
dev_beta_db
gengmei
lagacy-0.4.0
node_service
password
rbac
restrict_access
test
v52
wph
1.5.2
1.5.1
1.5.0
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.3
1.3.2
1.3.1
1.3.0
1.2.1
1.2.0
1.1.1
1.1.0
1.0.0
0.3.3
0.3.2
0.3.2-rc2
0.3.1
0.3.0-beta
v1.4.10
v1.4.7
v1.4.4
No related merge requests found
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
118 additions
and
44 deletions
+118
-44
ansible_api.py
jperm/ansible_api.py
+118
-44
No files found.
jperm/ansible_api.py
View file @
df9d879f
...
@@ -7,6 +7,14 @@ from ansible.inventory import Inventory
...
@@ -7,6 +7,14 @@ from ansible.inventory import Inventory
from
ansible.runner
import
Runner
from
ansible.runner
import
Runner
from
ansible.playbook
import
PlayBook
from
ansible.playbook
import
PlayBook
from
ansible
import
callbacks
from
ansible
import
utils
from
passlib.hash
import
sha512_crypt
import
os.path
JPERM_DIR
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
ANSIBLE_DIR
=
os
.
path
.
join
(
JPERM_DIR
,
'playbooks'
)
class
AnsibleError
(
StandardError
):
class
AnsibleError
(
StandardError
):
"""
"""
...
@@ -38,21 +46,28 @@ class MyInventory(object):
...
@@ -38,21 +46,28 @@ class MyInventory(object):
def
__init__
(
self
,
resource
):
def
__init__
(
self
,
resource
):
"""
"""
resource :
resource :
必须是一个字典列表,比如
resource的数据格式是一个列表字典,比如
[{"hostname": "10.10.10.10", "port": "22",
{
"username": "test", "password": "mypass"}, ...]
"group1": [{"hostname": "10.10.10.10", "port": "22",
"username": "test", "password": "mypass"}, ...],
"group2": [{"hostname": "10.10.10.10", "port": "22",
"username": "test", "password": "mypass"}, ...]
}
如果你只传入1个列表,这默认该列表内的所有主机属于my_group组,比如
[{"hostname": "10.10.10.10", "port": "22",
"username": "test", "password": "mypass"}, ...]
"""
"""
self
.
resource
=
resource
self
.
resource
=
resource
self
.
_gen_i
nventory
()
self
.
inventory
=
I
nventory
()
self
.
gen_inventory
()
def
_gen_inventory
(
self
):
def
add_group
(
self
,
hosts
,
groupname
):
"""
"""
add hosts to
inventory.
add hosts to
a group
"""
"""
my_group
=
Group
(
name
=
'my_group'
)
my_group
=
Group
(
name
=
groupname
)
for
host
in
hosts
:
for
host
in
self
.
resource
:
hostname
=
host
.
get
(
"hostname"
)
hostname
=
host
.
get
(
"hostname"
)
hostport
=
host
.
get
(
"hostport"
)
hostport
=
host
.
get
(
"hostport"
)
username
=
host
.
get
(
"username"
)
username
=
host
.
get
(
"username"
)
...
@@ -64,11 +79,17 @@ class MyInventory(object):
...
@@ -64,11 +79,17 @@ class MyInventory(object):
my_host
.
set_variable
(
'ansible_ssh_pass'
,
password
)
my_host
.
set_variable
(
'ansible_ssh_pass'
,
password
)
my_group
.
add_host
(
my_host
)
my_group
.
add_host
(
my_host
)
my_inventory
=
Inventory
()
self
.
inventory
.
add_group
(
my_group
)
my_inventory
.
add_group
(
my_group
)
my_inventory
.
subset
(
'my_group'
)
self
.
inventory
=
my_inventory
def
gen_inventory
(
self
):
"""
add hosts to inventory.
"""
if
isinstance
(
self
.
resource
,
list
):
self
.
add_group
(
self
.
resource
,
'my_group'
)
elif
isinstance
(
self
.
resource
,
dict
):
for
groupname
,
hosts
in
self
.
resource
.
iteritems
():
self
.
add_group
(
hosts
,
groupname
)
class
Command
(
MyInventory
):
class
Command
(
MyInventory
):
...
@@ -77,7 +98,6 @@ class Command(MyInventory):
...
@@ -77,7 +98,6 @@ class Command(MyInventory):
"""
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
Command
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
super
(
Command
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
def
run
(
self
,
command
,
module_name
=
"command"
,
timeout
=
5
,
forks
=
10
):
def
run
(
self
,
command
,
module_name
=
"command"
,
timeout
=
5
,
forks
=
10
):
"""
"""
...
@@ -99,7 +119,6 @@ class Command(MyInventory):
...
@@ -99,7 +119,6 @@ class Command(MyInventory):
self
.
results
=
hoc
.
run
()
self
.
results
=
hoc
.
run
()
return
self
.
stdout
return
self
.
stdout
@property
@property
def
raw_results
(
self
):
def
raw_results
(
self
):
"""
"""
...
@@ -107,7 +126,6 @@ class Command(MyInventory):
...
@@ -107,7 +126,6 @@ class Command(MyInventory):
"""
"""
return
self
.
results
return
self
.
results
@property
@property
def
exec_time
(
self
):
def
exec_time
(
self
):
"""
"""
...
@@ -121,7 +139,6 @@ class Command(MyInventory):
...
@@ -121,7 +139,6 @@ class Command(MyInventory):
"end"
:
value
.
get
(
"end"
),
"end"
:
value
.
get
(
"end"
),
"delta"
:
value
.
get
(
"delta"
),}
"delta"
:
value
.
get
(
"delta"
),}
return
result
return
result
@property
@property
def
stdout
(
self
):
def
stdout
(
self
):
...
@@ -133,7 +150,6 @@ class Command(MyInventory):
...
@@ -133,7 +150,6 @@ class Command(MyInventory):
for
key
,
value
in
all
.
iteritems
():
for
key
,
value
in
all
.
iteritems
():
result
[
key
]
=
value
.
get
(
"stdout"
)
result
[
key
]
=
value
.
get
(
"stdout"
)
return
result
return
result
@property
@property
def
stderr
(
self
):
def
stderr
(
self
):
...
@@ -163,7 +179,6 @@ class Tasks(Command):
...
@@ -163,7 +179,6 @@ class Tasks(Command):
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
Tasks
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
super
(
Tasks
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
def
__run
(
self
,
module_args
,
module_name
=
"command"
,
timeout
=
5
,
forks
=
10
):
def
__run
(
self
,
module_args
,
module_name
=
"command"
,
timeout
=
5
,
forks
=
10
):
"""
"""
run command from andible ad-hoc.
run command from andible ad-hoc.
...
@@ -180,41 +195,81 @@ class Tasks(Command):
...
@@ -180,41 +195,81 @@ class Tasks(Command):
self
.
results
=
hoc
.
run
()
self
.
results
=
hoc
.
run
()
@property
def
push_key
(
self
,
user
,
key_path
):
def
msg
(
self
):
"""
"""
push the ssh authorized key to target.
get the contacted and dark msg
"""
"""
module_args
=
'user="
%
s" key="{{ lookup("file", "
%
s") }}"'
%
(
user
,
key_path
)
self
.
__run
(
module_args
,
"authorized_key"
)
msg
=
{}
msg
=
{}
for
result
in
[
"contacted"
,
"dark"
]:
for
result
in
[
"contacted"
,
"dark"
]:
all
=
self
.
results
.
get
(
result
)
all
=
self
.
results
.
get
(
result
)
for
key
,
value
in
all
.
iteritems
():
for
key
,
value
in
all
.
iteritems
():
if
value
.
get
(
"msg"
):
if
value
.
get
(
"msg"
):
msg
[
key
]
=
value
.
get
(
"msg"
)
msg
[
key
]
=
value
.
get
(
"msg"
)
return
msg
return
{
"status"
:
"ok"
}
if
msg
else
{
"status"
:
"failed"
,
"msg"
:
msg
}
def
push_key
(
self
,
user
,
key_path
):
"""
push the ssh authorized key to target.
"""
module_args
=
'user="
%
s" key="{{ lookup("file", "
%
s") }}"'
%
(
user
,
key_path
)
self
.
__run
(
module_args
,
"authorized_key"
)
return
{
"status"
:
"failed"
,
"msg"
:
self
.
msg
}
if
self
.
msg
else
{
"status"
:
"ok"
}
def
add_user
(
self
,
user
):
def
add_user
(
self
,
user
name
,
password
):
"""
"""
add a host user.
add a host user.
"""
"""
pass
encrypt_pass
=
sha512_crypt
.
encrypt
(
password
)
module_args
=
'name=
%
s shell=/bin/bash password=
%
s'
%
(
username
,
encrypt_pass
)
self
.
__run
(
module_args
,
"user"
)
return
{
"status"
:
"failed"
,
"msg"
:
self
.
msg
}
if
self
.
msg
else
{
"status"
:
"ok"
}
def
del_user
(
self
,
user
):
def
del_user
(
self
,
user
name
):
"""
"""
delete a host user.
delete a host user.
"""
"""
pass
module_args
=
'name=
%
s state=absent remove=yes move_home=yes force=yes'
%
(
username
)
self
.
__run
(
module_args
,
"user"
)
return
{
"status"
:
"failed"
,
"msg"
:
self
.
msg
}
if
self
.
msg
else
{
"status"
:
"ok"
}
class
CustomAggregateStats
(
callbacks
.
AggregateStats
):
"""
Holds stats about per-host activity during playbook runs.
"""
def
__init__
(
self
):
super
(
CustomAggregateStats
,
self
)
.
__init__
()
self
.
results
=
[]
def
compute
(
self
,
runner_results
,
setup
=
False
,
poll
=
False
,
ignore_errors
=
False
):
"""
Walk through all results and increment stats.
"""
super
(
CustomAggregateStats
,
self
)
.
compute
(
runner_results
,
setup
,
poll
,
ignore_errors
)
self
.
results
.
append
(
runner_results
)
def
summarize
(
self
,
host
):
"""
Return information about a particular host
"""
summarized_info
=
super
(
CustomAggregateStats
,
self
)
.
summarize
(
host
)
# Adding the info I need
summarized_info
[
'result'
]
=
self
.
results
return
summarized_info
class
MyPlaybook
(
MyInventory
):
class
MyPlaybook
(
MyInventory
):
"""
"""
this is my playbook object for execute playbook.
this is my playbook object for execute playbook.
...
@@ -223,11 +278,32 @@ class MyPlaybook(MyInventory):
...
@@ -223,11 +278,32 @@ class MyPlaybook(MyInventory):
super
(
MyPlaybook
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
super
(
MyPlaybook
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
def
deploy
(
self
):
def
run
(
self
,
playbook_relational_path
):
"""
run ansible playbook,
only surport relational path.
"""
stats
=
CustomAggregateStats
()
playbook_cb
=
callbacks
.
PlaybookCallbacks
(
verbose
=
utils
.
VERBOSITY
)
runner_cb
=
callbacks
.
PlaybookRunnerCallbacks
(
stats
,
verbose
=
utils
.
VERBOSITY
)
playbook_path
=
os
.
path
.
join
(
ANSIBLE_DIR
,
playbook_relational_path
)
pb
=
PlayBook
(
playbook
=
playbook_path
,
stats
=
stats
,
callbacks
=
playbook_cb
,
runner_callbacks
=
runner_cb
,
inventory
=
self
.
inventory
,
check
=
True
)
self
.
results
=
pb
.
run
()
@property
def
raw_results
(
self
):
"""
"""
use ansible playbook to deploy a applicatio
n.
get the raw results after playbook ru
n.
"""
"""
pas
s
return
self
.
result
s
class
App
(
MyPlaybook
):
class
App
(
MyPlaybook
):
...
@@ -240,15 +316,13 @@ class App(MyPlaybook):
...
@@ -240,15 +316,13 @@ class App(MyPlaybook):
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
resource
=
[
{
"hostname"
:
"192.168.10.128"
,
"port"
:
"22"
,
"username"
:
"root"
,
"password"
:
"xxx"
}]
resource
=
{
"test"
:
[{
"hostname"
:
"192.168.10.128"
,
"port"
:
"22"
,
"username"
:
"root"
,
"password"
:
"xxx"
}]}
task
=
Tasks
(
resource
)
playbook
=
MyPlaybook
(
resource
)
print
task
.
push_key
(
'root'
,
'/root/.ssh/id_rsa.pub'
)
playbook
.
run
(
'test.yml'
)
print
playbook
.
raw_results
# print task.add_user('test', 'mypass')
# print task.del_user('test')
# print task.push_key('root', '/root/.ssh/id_rsa.pub')
This diff is collapsed.
Click to expand it.
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