#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : test2.py
# @Author: Anthony.waa
# @Date : 2018/12/6 0006
# @Desc : PyCharm
# ansible推荐版本:ansible-2.4.1.0
import json
import shutil
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.plugins.callback import CallbackBase
import ansible.constants as C
class ResultCallback(CallbackBase):
def __init__(self, *args, **kwargs):
# super(ResultsCollector, self).__init__(*args, **kwargs)
self.host_ok = {}
self.host_unreachable = {}
self.host_failed = {}
def v2_runner_on_unreachable(self, result):
self.host_unreachable[result._host.get_name()] = result
def v2_runner_on_ok(self, result, *args, **kwargs):
self.host_ok[result._host.get_name()] = result
def v2_runner_on_failed(self, result, *args, **kwargs):
self.host_failed[result._host.get_name()] = result
class Ansibleapi(object):
def __init__(self):
self.Options = namedtuple('Options',
['connection',
'remote_user',
'ask_sudo_pass',
'verbosity',
'ack_pass',
'module_path',
'forks',
'become',
'become_method',
'become_user',
'check',
'listhosts',
'listtasks',
'listtags',
'syntax',
'sudo_user',
'sudo',
'diff'])
self.ops = self.Options(connection='smart',
remote_user=None,
ack_pass=None,
sudo_user=None,
forks=5,
sudo=None,
ask_sudo_pass=False,
verbosity=5,
module_path=None,
become=None,
become_method=None,
become_user=None,
check=False,
diff=False,
listhosts=None,
listtasks=None,
listtags=None,
syntax=None)
self.loader = DataLoader()
self.passwords = dict()
self.results_callback = ResultCallback()
self.inventory = InventoryManager(loader=self.loader, sources=['/etc/ansible/hosts'])
self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory)
def runansible(self, host_list, task_list):
play_source = dict(
name="Ansible Play",
hosts=host_list,
gather_facts='no',
tasks=task_list
)
play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
tqm = None
try:
tqm = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.ops,
passwords=self.passwords,
stdout_callback=self.results_callback,
run_additional_callbacks=C.DEFAULT_LOAD_CALLBACK_PLUGINS,
run_tree=False,
)
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
results_raw = {}
results_raw['success'] = {}
results_raw['failed'] = {}
results_raw['unreachable'] = {}
for host, result in self.results_callback.host_ok.items():
results_raw['success'][host] = json.dumps(result._result)
for host, result in self.results_callback.host_failed.items():
results_raw['failed'][host] = result._result['msg']
for host, result in self.results_callback.host_unreachable.items():
results_raw['unreachable'][host] = result._result['msg']
print results_raw
def playbookrun(self, playbook_path):
self.variable_manager.extra_vars = {'customer': 'test', 'disabled': 'yes'}
playbook = PlaybookExecutor(playbooks=playbook_path,
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader, options=self.ops, passwords=self.passwords)
result = playbook.run()
return result
if __name__ == "__main__":
run_ansible = Ansibleapi()
host_list = ['192.168.2.213']
tasks_list = [
dict(action=dict(module='command', args='ls')),
# dict(action=dict(module='shell', args='python sleep.py')),
# dict(action=dict(module='synchronize', args='src=/home/op/test dest=/home/op/ delete=yes')),
]
run_ansible.runansible(host_list,tasks_list)
run_ansible.playbookrun(playbook_path=['/data/server/working/ping.yml'])
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : test2.py
# @Author: Anthony.waa
# @Date : 2018/12/6 0006
# @Desc : PyCharm
# ansible推荐版本:ansible-2.4.1.0
---
- hosts: web1
tasks:
- name: 检测主机是否存活
ping:
- name: 安装python3和python虚拟环境
yum: name={{ item }} state=latest
with_items:
- epel-release
- wget
- gcc
- git
- python36
- python36-devel
- name: 建立虚拟环境
shell: cd /opt && python3.6 -m venv py3 && source /opt/py3/bin/activate
- name: 下载并安装coco(jumpserver客户端注册服务)
shell: cd /opt && git clone https://github.com/jumpserver/coco.git && echo "source /opt/py3/bin/activate" > /opt/coco/.env
- name: 安装coco依赖
shell: cd /opt/coco/requirements && pip install --upgrade pip
- name: 安装python模块
shell: yum -y install $(cat rpm_requirements.txt) && pip install -r requirements.txt
- name: 创建keys logs目录
file: path={{ item }} state=directory
with_items:
- /opt/coco/keys
- /opt/coco/logs
- name: 传输本地coco配置文件至被控主句coco目录中
synchronize:
src: /opt/working/conf.py
dest: /opt/coco/conf.py
- name: 启动本地cocod client 服务
shell: cd /opt/coco && ./cocod start -d