Ansible版本2.6.4,記錄下ansible API使用,合入到運營系統(tǒng),會使用到ansible批量操作服務(wù)器唉工,ansible本身就是python寫的,所以對于python來說可以直接調(diào)用汹忠,有興趣了解更多可以去官網(wǎng)看看https://docs.ansible.com/ansible/latest/index.html
# coding:utf-8
#!/usr/bin/env python
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.plugins.callback import CallbackBase
from ansible.errors import AnsibleError
import ansible.constants as C
class ResultCallback(CallbackBase):
"""結(jié)果回調(diào)"""
def __init__(self, *args, **kwargs):
super(ResultCallback, self).__init__(*args, **kwargs)
self.host_ok = {}
self.host_unreachable = {}
self.host_failed = {}
self.ret = {
'host_ok': '',
'host_unreachable': '',
'host_failed': ''
}
def v2_runner_on_unreachable(self, result):
self.host_unreachable[result._host.get_name()] = result
self.ret['host_unreachable'] = {result._host.name: result._result}
def v2_runner_on_ok(self, result, *args, **kwargs):
self.host_ok[result._host.get_name()] = result
self.ret['host_ok'] = {result._host.name: result._result}
def v2_runner_on_failed(self, result, *args, **kwargs):
self.host_failed[result._host.get_name()] = result
self.ret['host_failed'] = {result._host.name: result._result}
class AnsibleApi():
def __init__(self, sources='conf/ansible/hosts', name="ansible Play",hosts=None,actions=None):
'''
創(chuàng)建參數(shù)淋硝,為保證每個參數(shù)都被設(shè)置雹熬,ansible使用可命名元組
'''
self.Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff'])
self.options = self.Options(connection='ssh', module_path=['/to/mymodules'], forks=30, become=None, become_method=None, become_user=None, check=False, diff=False)
'''初始化loader類'''
self.loader = DataLoader() # 用于讀取與解析yaml和json文件
self.passwords = dict(vault_pass='secret')
'''初始化結(jié)果回調(diào)方法,用于接收返回的結(jié)果'''
self.results_callback = ResultCallback()
'''指定inventory谣膳,即我們的ansible hosts文件竿报,使用路徑指定一個host文件,或者一個逗號分割host的字符串'''
self.inventory = InventoryManager(loader=self.loader, sources=sources)
'''合并所有不同的資源成一個統(tǒng)一的變量管理視圖继谚,這些由變量管理器完成'''
self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory)
'''play_source 創(chuàng)建我們?nèi)蝿?wù)的數(shù)據(jù)結(jié)構(gòu)烈菌,tasks里面就是我們定義的yaml文件所做的步驟'''
if actions == None:
print "no actions"
raise AnsibleError
if hosts == None:
print "no hosts"
raise AnsibleError
self.play_source = dict(
name=name,
hosts=hosts,
gather_facts='no',
tasks=actions,
#dict(action=dict(module='shell', args='ls'), register='shell_out'),
#dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
)
def _run_task(self):
'''run task'''
play = Play().load(self.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.options,
passwords=self.passwords,
stdout_callback=self.results_callback,
# Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
)
result = tqm.run(play) # most interesting data for a play is actually sent to the callback's methods
finally:
# we always need to cleanup child procs and the structres we use to communicate with them
if tqm is not None:
tqm.cleanup()
# Remove ansible tmpdir
shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
if __name__ == '__main__':
'''
使用舉例, ip:,號分割
'''
ip = 'localhost,'
test_key = "key1\n \
key2"
actions = [
dict(action=dict(module='shell', args='ls', register='shell_out')),
dict(action=dict(module='authorized_key', args='user=brick key="%s"' % test_key))
]
ansible_actions = AnsibleApi(sources='../conf/ansible/hosts', name="ansible test", hosts=ip, actions=actions)
ansible_actions._run_task()
來自官網(wǎng)的python api example,簡單的封裝了一下花履,就可以用了僧界!其中每一步做了什么我都做了注釋,如果還需要了解更詳細的內(nèi)容臭挽,推薦讀下官方文檔,甚至可以讀下源碼咬腕,以后有空了再研究下源碼欢峰,現(xiàn)在業(yè)務(wù)忙也沒時間搞