【国际版棋牌源码】【mycli源码包】【教学商城源码】ad hoc安卓源码_android adb源码
1.å¦ä½ä½¿ç¨Ansible 2çAPIåpythonå¼å
å¦ä½ä½¿ç¨Ansible 2çAPIåpythonå¼å
Ansible å SaltStack é½æä¾äº Python ç´æ¥è°ç¨çAPI,卓源国际版棋牌源码 è¿æ¹ä¾¿äº Pythoner 对è¿äºè½¯ä»¶è¿è¡äºæ¬¡å¼ååæ´å, æ¤åè½çå®æ¹ä¾¿äºä¸å°, æ¯èµ· Python 代ç ä¸è°ç¨ shell ä¹ç¥æ¾ä¸ä¸!
ç¶è Ansible å¨2.0çæ¬åéæäºå¤§é¨åç代ç é»è¾, å¯ç¨äº2.0çæ¬ä¹åç Runner å Playbook ç±», 使å¾å¹¿å¤§åå¦ä¹åç代ç è¿è¡é误. æ©æ¥ä¸å¦ææ¥, ä»å¤©ä¸åå¯¹ç § å®æ¹çææ¡£ , ç»åæºä»£ç , 对2.0çæ¬ä¹åç Python API åäºä¸æ¢ç©¶
Adhoc
adhoc å ¶å®å°±æ¯æ§è¡ Ansible 模å, éè¿ adhoc æ们å¯ä»¥æ¹ä¾¿å¿«æ·çå®æä¸äºä¸´æ¶çè¿ç»´æä½.
2.0 ä¹åçè°ç¨
import ansible.runner
import json
runner = ansible.runner.Runner(
module_name='ping', # 模åå
module_args='', # 模ååæ°
pattern='all', # ç®æ æºå¨çpattern
forks=
)
datastructure = runner.run()
data = json.dumps(datastructure,indent=4)
å½ç¶è¿éä¼å»å è½½é»è®¤ç inventory
å¦æä¸æ³ä½¿ç¨ inventory æ件æè æ³ä½¿ç¨å¨æç inventory, åå¯ä»¥ä½¿ç¨ host_list åæ°ä»£æ¿
import ansible.runner
import json
runner = ansible.runner.Runner(
host_list=["..0.1"], # è¿éå¦ææç¡®æå®ä¸»æºéè¦ä¼ éä¸ä¸ªå表, æè æå®å¨æinventoryèæ¬
module_name='ping', # 模åå
module_args='', # 模ååæ°
extra_vars={ "ansible_ssh_user":"root","ansible_ssh_pass":"xx"},
forks=
)
datastructure = runner.run()
data = json.dumps(datastructure,indent=4)
2.0 ä¹åçè°ç¨
import json
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.executor.playbook_executor import PlaybookExecutor
loader = DataLoader() # ç¨æ¥å 载解æyamlæ件æJSONå 容,并ä¸æ¯ævaultç解å¯
variable_manager = VariableManager() # 管çåéçç±»,å æ¬ä¸»æº,ç»,æ©å±çåé,ä¹åçæ¬æ¯å¨ inventory ä¸ç
inventory = Inventory(loader=loader, variable_manager=variable_manager)
variable_manager.set_inventory(inventory) # æ ¹æ® inventory å 载对åºåé
class Options(object):
'''
è¿æ¯ä¸ä¸ªå ¬å ±çç±»,å 为ad-hocåplaybooké½éè¦ä¸ä¸ªoptionsåæ°
并ä¸æéè¦æ¥æä¸åçå±æ§,ä½æ¯å¤§é¨åå±æ§é½å¯ä»¥è¿åNoneæFalse
å æ¤ç¨è¿æ ·çä¸ä¸ªç±»æ¥çå»åå§å大ä¸å ç空å¼çå±æ§
'''
def __init__(self):
self.connection = "local"
self.forks = 1
self.check = False
def __getattr__(self, name):
return None
options = Options()
def run_adhoc():
variable_manager.extra_vars={ "ansible_ssh_user":"root" , "ansible_ssh_pass":"xxx"} # å¢å å¤é¨åé
# æ建pb, è¿éå¾æææ, æ°çæ¬è¿è¡ad-hocæplaybooké½éè¦æ建è¿æ ·çpb, åªæ¯æåè°ç¨playçç±»ä¸ä¸æ ·
# :param name: ä»»å¡å,类似playbookä¸tasksä¸çname
# :param hosts: playbookä¸çhosts
# :param tasks: playbookä¸çtasks, å ¶å®è¿å°±æ¯playbookçè¯æ³, å 为tasksçå¼æ¯ä¸ªå表,å æ¤å¯ä»¥åå ¥å¤ä¸ªtask
play_source = { "name":"Ansible Ad-Hoc","hosts":"..0.1","gather_facts":"no","tasks":[{ "action":{ "module":"shell","args":"w"}}]}
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
tqm = None
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=None,
stdout_callback='minimal',
run_tree=False,
)
result = tqm.run(play)
print result
finally:
if tqm is not None:
tqm.cleanup()
if __name__ == '__main__':
run_adhoc()
Playbook
playbook åç±»ä¼¼äº SaltStack ä¸ç state
2.0 ä¹åçè°ç¨
from ansible import callbacks
from ansible import utils
from ansible.playbook import PlayBook
stats = callbacks.AggregateStats()
callback = callbacks.PlaybookCallbacks()
runner_callbacks = callbacks.PlaybookRunnerCallbacks(stats)
pb = ansible.playbook.PlayBook(
playbook="tasks.yml",
stats=stats,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
check=True
)
pb.run()
2.0 ä¹åçè°ç¨
import json
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.executor.playbook_executor import PlaybookExecutor
loader = DataLoader() # ç¨æ¥å 载解æyamlæ件æJSONå 容,并ä¸æ¯ævaultç解å¯
variable_manager = VariableManager() # 管çåéçç±»,å æ¬ä¸»æº,ç»,æ©å±çåé,ä¹åçæ¬æ¯å¨ inventory ä¸ç
inventory = Inventory(loader=loader, variable_manager=variable_manager)
variable_manager.set_inventory(inventory) # æ ¹æ® inventory å 载对åºåé
class Options(object):
'''
è¿æ¯ä¸ä¸ªå ¬å ±çç±»,å 为ad-hocåplaybooké½éè¦ä¸ä¸ªoptionsåæ°
并ä¸æéè¦æ¥æä¸åçå±æ§,ä½æ¯å¤§é¨åå±æ§é½å¯ä»¥è¿åNoneæFalse
å æ¤ç¨è¿æ ·çä¸ä¸ªç±»æ¥çå»åå§å大ä¸å ç空å¼çå±æ§
'''
def __init__(self):
self.connection = "local"
self.forks = 1
self.check = False
def __getattr__(self, name):
return None
options = Options()
def run_playbook():
playbooks=['task.yaml'] # è¿éæ¯ä¸ä¸ªå表, å æ¤å¯ä»¥è¿è¡å¤ä¸ªplaybook
variable_manager.extra_vars={ "ansible_ssh_user":"root" , "ansible_ssh_pass":"xxx"} # å¢å å¤é¨åé
pb = PlaybookExecutor(playbooks=playbooks, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None)
result = pb.run()
print result
if __name__ == '__main__':
run_playbook()