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
82286ea7
Commit
82286ea7
authored
Nov 06, 2015
by
Zi Chuanxiu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix set host vars and add set group vars
parent
0f8abb60
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
55 deletions
+62
-55
ansible_api.py
jperm/ansible_api.py
+62
-55
No files found.
jperm/ansible_api.py
View file @
82286ea7
...
@@ -7,17 +7,16 @@ from ansible.inventory import Inventory
...
@@ -7,17 +7,16 @@ 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
callbacks
from
ansible
import
utils
from
ansible
import
utils
from
passlib.hash
import
sha512_crypt
from
passlib.hash
import
sha512_crypt
from
utils
import
get_rand_pass
from
utils
import
get_rand_pass
import
os.path
JPERM_DIR
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
ANSIBLE_DIR
=
os
.
path
.
join
(
JPERM_DIR
,
'playbooks'
)
import
os.path
API_DIR
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
ANSIBLE_DIR
=
os
.
path
.
join
(
API_DIR
,
'playbooks'
)
...
@@ -50,38 +49,48 @@ class MyInventory(object):
...
@@ -50,38 +49,48 @@ class MyInventory(object):
"""
"""
def
__init__
(
self
,
resource
):
def
__init__
(
self
,
resource
):
"""
"""
resource :
resource的数据格式是一个列表字典,比如
resource的数据格式是一个列表字典,比如
{
{
"group1": {
"group1": [{"hostname": "10.10.10.10", "port": "22",
"hosts": [{"hostname": "10.10.10.10", "port": "22", "username": "test", "password": "mypass"}, ...],
"username": "test", "password": "mypass"}, ...],
"vars": {"var1": value1, "var2": value2, ...}
"group2": [{"hostname": "10.10.10.10", "port": "22",
}
"username": "test", "password": "mypass"}, ...]
}
}
如果你只传入1个列表,这默认该列表内的所有主机属于my_group组,比如
如果你只传入1个列表,这默认该列表内的所有主机属于my_group组,比如
[{"hostname": "10.10.10.10", "port": "22",
[{"hostname": "10.10.10.10", "port": "22", "username": "test", "password": "mypass"}, ...]
"username": "test", "password": "mypass"}, ...]
"""
"""
self
.
resource
=
resource
self
.
resource
=
resource
self
.
inventory
=
Inventory
()
self
.
inventory
=
Inventory
()
self
.
gen_inventory
()
self
.
gen_inventory
()
def
add_group
(
self
,
hosts
,
groupname
):
def
add_group
(
self
,
hosts
,
groupname
,
groupvars
=
None
):
"""
"""
add hosts to a group
add hosts to a group
"""
"""
my_group
=
Group
(
name
=
groupname
)
my_group
=
Group
(
name
=
groupname
)
# if group variables exists, add them to group
if
groupvars
:
for
key
,
value
in
groupvars
.
iteritems
():
my_group
.
set_variable
(
key
,
value
)
# add hosts to group
for
host
in
hosts
:
for
host
in
hosts
:
hostname
=
host
.
get
(
"hostname"
)
# set connection variables
hostport
=
host
.
get
(
"hostport"
)
hostname
=
host
.
pop
(
"hostname"
)
username
=
host
.
get
(
"username"
)
hostport
=
host
.
pop
(
"port"
)
password
=
host
.
get
(
"password"
)
username
=
host
.
pop
(
"username"
)
password
=
host
.
pop
(
"password"
)
my_host
=
Host
(
name
=
hostname
,
port
=
hostport
)
my_host
=
Host
(
name
=
hostname
,
port
=
hostport
)
my_host
.
set_variable
(
'ansible_ssh_host'
,
hostname
)
my_host
.
set_variable
(
'ansible_ssh_host'
,
hostname
)
my_host
.
set_variable
(
'ansible_ssh_port'
,
hostport
)
my_host
.
set_variable
(
'ansible_ssh_port'
,
hostport
)
my_host
.
set_variable
(
'ansible_ssh_user'
,
username
)
my_host
.
set_variable
(
'ansible_ssh_user'
,
username
)
my_host
.
set_variable
(
'ansible_ssh_pass'
,
password
)
my_host
.
set_variable
(
'ansible_ssh_pass'
,
password
)
# set other variables
for
key
,
value
in
host
.
iteritems
():
my_host
.
set_variable
(
key
,
value
)
# add to group
my_group
.
add_host
(
my_host
)
my_group
.
add_host
(
my_host
)
self
.
inventory
.
add_group
(
my_group
)
self
.
inventory
.
add_group
(
my_group
)
...
@@ -93,8 +102,8 @@ class MyInventory(object):
...
@@ -93,8 +102,8 @@ class MyInventory(object):
if
isinstance
(
self
.
resource
,
list
):
if
isinstance
(
self
.
resource
,
list
):
self
.
add_group
(
self
.
resource
,
'my_group'
)
self
.
add_group
(
self
.
resource
,
'my_group'
)
elif
isinstance
(
self
.
resource
,
dict
):
elif
isinstance
(
self
.
resource
,
dict
):
for
groupname
,
hosts
in
self
.
resource
.
iteritems
():
for
groupname
,
hosts
_and_vars
in
self
.
resource
.
iteritems
():
self
.
add_group
(
hosts
,
groupname
)
self
.
add_group
(
hosts
_and_vars
.
get
(
"hosts"
),
groupname
,
hosts_and_vars
.
get
(
"vars"
)
)
class
Command
(
MyInventory
):
class
Command
(
MyInventory
):
...
@@ -120,7 +129,7 @@ class Command(MyInventory):
...
@@ -120,7 +129,7 @@ class Command(MyInventory):
subset
=
'my_group'
,
subset
=
'my_group'
,
forks
=
forks
forks
=
forks
)
)
self
.
results
=
hoc
.
run
()
self
.
results
=
hoc
.
run
()
return
self
.
stdout
return
self
.
stdout
...
@@ -197,7 +206,7 @@ class Tasks(Command):
...
@@ -197,7 +206,7 @@ class Tasks(Command):
subset
=
'my_group'
,
subset
=
'my_group'
,
forks
=
forks
forks
=
forks
)
)
self
.
results
=
hoc
.
run
()
self
.
results
=
hoc
.
run
()
@property
@property
...
@@ -235,7 +244,7 @@ class Tasks(Command):
...
@@ -235,7 +244,7 @@ class Tasks(Command):
"""
"""
add a host user.
add a host user.
"""
"""
encrypt_pass
=
sha512_crypt
.
encrypt
(
password
)
encrypt_pass
=
sha512_crypt
.
encrypt
(
password
)
module_args
=
'name=
%
s shell=/bin/bash password=
%
s'
%
(
username
,
encrypt_pass
)
module_args
=
'name=
%
s shell=/bin/bash password=
%
s'
%
(
username
,
encrypt_pass
)
self
.
__run
(
module_args
,
"user"
)
self
.
__run
(
module_args
,
"user"
)
...
@@ -263,7 +272,7 @@ class Tasks(Command):
...
@@ -263,7 +272,7 @@ class Tasks(Command):
results
[
"user_info"
]
=
users
results
[
"user_info"
]
=
users
return
results
return
results
def
del_init_users
(
self
):
def
del_init_users
(
self
):
"""
"""
delete initail users: SA, DBA, DEV
delete initail users: SA, DBA, DEV
...
@@ -284,7 +293,7 @@ class CustomAggregateStats(callbacks.AggregateStats):
...
@@ -284,7 +293,7 @@ class CustomAggregateStats(callbacks.AggregateStats):
def
__init__
(
self
):
def
__init__
(
self
):
super
(
CustomAggregateStats
,
self
)
.
__init__
()
super
(
CustomAggregateStats
,
self
)
.
__init__
()
self
.
results
=
[]
self
.
results
=
[]
def
compute
(
self
,
runner_results
,
setup
=
False
,
poll
=
False
,
def
compute
(
self
,
runner_results
,
setup
=
False
,
poll
=
False
,
ignore_errors
=
False
):
ignore_errors
=
False
):
"""
"""
...
@@ -292,21 +301,21 @@ class CustomAggregateStats(callbacks.AggregateStats):
...
@@ -292,21 +301,21 @@ class CustomAggregateStats(callbacks.AggregateStats):
"""
"""
super
(
CustomAggregateStats
,
self
)
.
compute
(
runner_results
,
setup
,
poll
,
super
(
CustomAggregateStats
,
self
)
.
compute
(
runner_results
,
setup
,
poll
,
ignore_errors
)
ignore_errors
)
self
.
results
.
append
(
runner_results
)
self
.
results
.
append
(
runner_results
)
def
summarize
(
self
,
host
):
def
summarize
(
self
,
host
):
"""
"""
Return information about a particular host
Return information about a particular host
"""
"""
summarized_info
=
super
(
CustomAggregateStats
,
self
)
.
summarize
(
host
)
summarized_info
=
super
(
CustomAggregateStats
,
self
)
.
summarize
(
host
)
# Adding the info I need
# Adding the info I need
summarized_info
[
'result'
]
=
self
.
results
summarized_info
[
'result'
]
=
self
.
results
return
summarized_info
return
summarized_info
class
MyPlaybook
(
MyInventory
):
class
MyPlaybook
(
MyInventory
):
"""
"""
...
@@ -316,23 +325,24 @@ class MyPlaybook(MyInventory):
...
@@ -316,23 +325,24 @@ class MyPlaybook(MyInventory):
super
(
MyPlaybook
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
super
(
MyPlaybook
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
def
run
(
self
,
playbook_relational_path
):
def
run
(
self
,
playbook_relational_path
,
extra_vars
=
None
):
"""
"""
run ansible playbook,
run ansible playbook,
only surport relational path.
only surport relational path.
"""
"""
stats
=
CustomAggregateStats
()
stats
=
callbacks
.
AggregateStats
()
playbook_cb
=
callbacks
.
PlaybookCallbacks
(
verbose
=
utils
.
VERBOSITY
)
playbook_cb
=
callbacks
.
PlaybookCallbacks
(
verbose
=
utils
.
VERBOSITY
)
runner_cb
=
callbacks
.
PlaybookRunnerCallbacks
(
stats
,
verbose
=
utils
.
VERBOSITY
)
runner_cb
=
callbacks
.
PlaybookRunnerCallbacks
(
stats
,
verbose
=
utils
.
VERBOSITY
)
playbook_path
=
os
.
path
.
join
(
ANSIBLE_DIR
,
playbook_relational_path
)
playbook_path
=
os
.
path
.
join
(
ANSIBLE_DIR
,
playbook_relational_path
)
pb
=
PlayBook
(
pb
=
PlayBook
(
playbook
=
playbook_path
,
playbook
=
playbook_path
,
stats
=
stats
,
stats
=
stats
,
callbacks
=
playbook_cb
,
callbacks
=
playbook_cb
,
runner_callbacks
=
runner_cb
,
runner_callbacks
=
runner_cb
,
inventory
=
self
.
inventory
,
inventory
=
self
.
inventory
,
check
=
True
)
extra_vars
=
extra_vars
,
check
=
False
)
self
.
results
=
pb
.
run
()
self
.
results
=
pb
.
run
()
...
@@ -351,16 +361,17 @@ class App(MyPlaybook):
...
@@ -351,16 +361,17 @@ class App(MyPlaybook):
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
App
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
super
(
App
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
resource
=
[{
"hostname"
:
"192.168.10.128"
,
"port"
:
"22"
,
"username"
:
"root"
,
"password"
:
"xxx"
}]
pass
# resource = [{"hostname": "192.168.10.128", "port": "22", "username": "root", "password": "yusky0902"}]
# playbook = MyPlaybook(resource)
# playbook = MyPlaybook(resource)
# playbook.run('test.yml')
# playbook.run('test.yml')
# print playbook.raw_results
# print playbook.raw_results
command
=
Command
(
resource
)
#
command = Command(resource)
command
.
run
(
"who"
)
#
command.run("who")
print
command
.
stdout
# print command.raw_results
# task = Tasks(resource)
# task = Tasks(resource)
...
@@ -375,7 +386,3 @@ if __name__ == "__main__":
...
@@ -375,7 +386,3 @@ if __name__ == "__main__":
# print task.del_init_users()
# print task.del_init_users()
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