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
9e0d1a72
Commit
9e0d1a72
authored
Oct 29, 2015
by
root
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add ansible_api module
parent
7d412e4c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
148 additions
and
0 deletions
+148
-0
README.md
jperm/README.md
+10
-0
ansible_api.py
jperm/ansible_api.py
+138
-0
No files found.
jperm/README.md
0 → 100644
View file @
9e0d1a72
# Jperm App
---
### 模块 ansible_api
> 使用说明
+
依赖安装包: ansible、 sshpass
+
关于ansible配置: 需要启用配置文件(/etc/ansible/ansible.cfg)的 host_key_checking = False
jperm/ansible_api.py
0 → 100644
View file @
9e0d1a72
# -*- coding: utf-8 -*-
from
ansible.inventory.group
import
Group
from
ansible.inventory.host
import
Host
from
ansible.inventory
import
Inventory
from
ansible.runner
import
Runner
from
ansible.playbook
import
PlayBook
class
MyAnsible
(
object
):
"""
this is my ansible object
"""
def
__init__
(
self
,
resource
):
"""
resource :
必须是一个字典列表,比如
[{"hostname": "10.10.10.10", "port": "22",
"username": "test", "password": "mypass"}, ...]
"""
self
.
resource
=
resource
self
.
_gen_inventory
()
def
_gen_inventory
(
self
):
"""
add hosts to inventory
"""
my_group
=
Group
(
name
=
'my_group'
)
for
host
in
self
.
resource
:
hostname
=
host
.
get
(
"hostname"
)
hostport
=
host
.
get
(
"hostport"
)
username
=
host
.
get
(
"username"
)
password
=
host
.
get
(
"password"
)
my_host
=
Host
(
name
=
hostname
,
port
=
hostport
)
my_host
.
set_variable
(
'ansible_ssh_host'
,
hostname
)
my_host
.
set_variable
(
'ansible_ssh_port'
,
hostport
)
my_host
.
set_variable
(
'ansible_ssh_user'
,
username
)
my_host
.
set_variable
(
'ansible_ssh_pass'
,
password
)
my_group
.
add_host
(
my_host
)
my_inventory
=
Inventory
()
my_inventory
.
add_group
(
my_group
)
my_inventory
.
subset
(
'my_group'
)
self
.
inventory
=
my_inventory
def
run_command
(
self
,
command
,
module_name
=
"command"
,
timeout
=
5
,
forks
=
10
):
"""
run command from andible ad-hoc
command : 必须是一个需要执行的命令字符串, 比如
'uname -a'
"""
hoc
=
Runner
(
module_name
=
module_name
,
module_args
=
command
,
timeout
=
timeout
,
inventory
=
self
.
inventory
,
subset
=
'my_group'
,
forks
=
forks
)
self
.
results
=
hoc
.
run
()
@property
def
raw_results
(
self
):
"""
get the ansible raw results
"""
return
self
.
results
@property
def
exec_time
(
self
):
"""
get the command execute time
"""
result
=
{}
all
=
self
.
results
.
get
(
"contacted"
)
for
key
,
value
in
all
.
iteritems
():
result
[
key
]
=
{
"start"
:
value
.
get
(
"start"
),
"end"
:
value
.
get
(
"end"
),
"delta"
:
value
.
get
(
"delta"
),}
return
result
@property
def
stdout
(
self
):
"""
get the comamnd standard output
"""
result
=
{}
all
=
self
.
results
.
get
(
"contacted"
)
for
key
,
value
in
all
.
iteritems
():
result
[
key
]
=
value
.
get
(
"stdout"
)
return
result
@property
def
stderr
(
self
):
"""
get the command standard error
"""
result
=
{}
all
=
self
.
results
.
get
(
"contacted"
)
for
key
,
value
in
all
.
iteritems
():
result
[
key
]
=
{
"stderr"
:
value
.
get
(
"stderr"
),
"warnings"
:
value
.
get
(
"warnings"
),}
return
result
@property
def
dark
(
self
):
"""
get the dark results
"""
return
self
.
results
.
get
(
"dark"
)
if
__name__
==
"__main__"
:
resource
=
[{
"hostname"
:
"127.0.0.1"
,
"port"
:
"22"
,
"username"
:
"root"
,
"password"
:
"xxx"
},
{
"hostname"
:
"192.168.10.128"
,
"port"
:
"22"
,
"username"
:
"root"
,
"password"
:
"xxx"
}]
myansible
=
MyAnsible
(
resource
)
myansible
.
run_command
(
"uname -a"
)
print
myansible
.
stdout
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