本文目錄一覽:
- 1、如何使用Ansible 2的API做python開發
- 2、如何學習Python開源安全框架,並掌握其API
- 3、api介面和python庫的區別是什麼?
- 4、從哪能找到python示常式序或源碼
- 5、如何獲取python模塊的api
如何使用Ansible 2的API做python開發
在ansible1.9的時候,API是一個非常簡單的東西。官方說「it’s pretty simple」,真是又pretty又simple。
import ansible.runner
runner = ansible.runner.Runner(
module_name=’ping’,
module_args=”,
pattern=’web*’,
forks=10
)
datastructure = runner.run()
到了ansible2.0以後,是「a bit more complicated」,Oh my,簡直讓人難受。
簡潔和靈活是魚和熊掌。
ansible2.0 API怎麼用?
ansible2.0更貼近於ansible cli的常用命令執行方式,不同於上一版本只能發送單個命令或playbook;而更推薦用戶在調用ansibleAPI的時候,將playbook的每個task拆分出來,獲取每個task的結果。能夠跟靈活處理在執行批量作業過程中的各種反饋。
將執行操作的隊列模型,包含各類環境參數設置,歸結到「ansible.executor.task_queue_manager」類中
將執行過程中的各個task的設置,或者說playbook中的編排內容,歸結到「ansible.playbook.play」中
上述兩個東西,幾乎囊括了可以在執行過程中設置的所有參數,足夠靈活,也讓人抓狂,相當於需要自己寫一個1.9版本中的runner。
他們的確也都是原生類,並非專用於外部調用。
ansible.executor.task_queue_manager
這是ansible的一個內部模塊(ansible/executor/task_queue_manager.py)。初始化的源碼如下:
class TaskQueueManager:
”’
This class handles the multiprocessing requirements of Ansible by
creating a pool of worker forks, a result handler fork, and a
manager object with shared datastructures/queues for coordinating
work between all processes.
The queue manager is responsible for loading the play strategy plugin,
which dispatches the Play’s tasks to hosts.
”’
def __init__(self, inventory, variable_manager, loader, options, passwords, stdout_callback=None, run_additional_callbacks=True, run_tree=False):
self._inventory = inventory
self._variable_manager = variable_manager
self._loader = loader
self._options = options
self._stats = AggregateStats()
self.passwords = passwords
self._stdout_callback = stdout_callback
self._run_additional_callbacks = run_additional_callbacks
self._run_tree = run_tree
self._callbacks_loaded = False
self._callback_plugins = []
self._start_at_done = False
self._result_prc = None
……
創建時,需要的主要參數包括:
inventory — 由ansible.inventory模塊創建,用於導入inventory文件
variable_manager — 由ansible.vars模塊創建,用於存儲各類變數信息
loader — 由ansible.parsing.dataloader模塊創建,用於數據解析
options — 存放各類配置信息的數據字典
passwords — 登錄密碼,可設置加密信息
stdout_callback — 回調函數
ansible.playbook.play
ansible.playbook是一個原生模塊,既用於CLI也用於API。從源碼可以看出來:
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
ansible.playbook.play(ansible/playbook/play.py)。初始化源碼的介紹如下:
__all__ = [‘Play’]
class Play(Base, Taggable, Become):
“””
A play is a language feature that represents a list of roles and/or
task/handler blocks to execute on a given set of hosts.
Usage:
Play.load(datastructure) – Play
Play.something(…)
“””
最後,用task_queue_manager(play)來執行,老規矩,源碼的官方解釋。
def run(self, play):
”’
Iterates over the roles/tasks in a play, using the given (or default)
strategy for queueing tasks. The default is the linear strategy, which
operates like classic Ansible by keeping all hosts in lock-step with
a given task (meaning no hosts move on to the next task until all hosts
are done with the current task).
”’
一個完整的例子
# -*- coding:utf-8 -*-
# !/usr/bin/env python
#
# Author: Shawn.T
# Email: shawntai.ds@gmail.com
#
# this is the Interface package of Ansible2 API
#
from collections import namedtuple
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 tempfile import NamedTemporaryFile
import os
class AnsibleTask(object):
def __init__(self, targetHost):
Options = namedtuple(
‘Options’, [
‘listtags’, ‘listtasks’, ‘listhosts’, ‘syntax’, ‘connection’,’module_path’,
‘forks’, ‘remote_user’, ‘private_key_file’, ‘ssh_common_args’, ‘ssh_extra_args’,
‘sftp_extra_args’, ‘scp_extra_args’, ‘become’, ‘become_method’, ‘become_user’,
‘verbosity’, ‘check’
]
)
# initialize needed objects
self.variable_manager = VariableManager()
self.options = Options(
listtags=False, listtasks=False, listhosts=False, syntax=False, connection=’smart’,
module_path=’/usr/lib/python2.7/site-packages/ansible/modules’, forks=100,
remote_user=’root’, private_key_file=None, ssh_common_args=None, ssh_extra_args=None,
sftp_extra_args=None, scp_extra_args=None, become=False, become_method=None, become_user=’root’,
verbosity=None, check=False
)
self.passwords = dict(vault_pass=’secret’)
self.loader = DataLoader()
# create inventory and pass to var manager
self.hostsFile = NamedTemporaryFile(delete=False)
self.hostsFile.write(targetHost)
self.hostsFile.close()
self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=self.hostsFile.name)
self.variable_manager.set_inventory(self.inventory)
def ansiblePlay(self, action):
# create play with tasks
args = “ls /”
play_source = dict(
name = “Ansible Play”,
hosts = ‘all’,
gather_facts = ‘no’,
tasks = [
dict(action=dict(module=’shell’, args=args), register=’shell_out’),
dict(action=dict(module=’debug’, args=dict(msg='{{shell_out.stdout}}’)))
]
)
play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
# run it
tqm = None
try:
tqm = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords,
stdout_callback=’default’,
)
result = tqm.run(play)
finally:
# print result
if tqm is not None:
tqm.cleanup()
os.remove(self.hostsFile.name)
self.inventory.clear_pattern_cache()
return result
寫一個ansibleTask類,創建了上述的各類必要的配置信息對象,最後使用ansibleTask.ansiblePlay()函數執行。
inventory文件的動態生成
寫上面的代碼的過程中,碰到一個問題:inventory對象創建時需要一個實體的hosts文件,而文件需要動態生成。
生成的方法參考了這篇牛逼閃閃的文章。使用tempfile.NamedTemporaryFile這個方法來創建一個有名稱的臨時文件,可以選擇關閉後刪除或保留。上面的處理辦法是:不刪除,在執行完畢之後,通過os.remove(self.hostsFile.name)進行刪除。
ps.經YiChenWang指出,inventory的創建參數host_list可以使列表。使用以下方式創建inventory也是可以的:
self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=[‘xx.xx.xx.xx’, ‘xx.xx.xx.xx’])
不過,源碼中指出,採用list格式參數是無法載入inventory data的。如果需要載入,還是得使用臨時文件的辦法。
如何學習Python開源安全框架,並掌握其API
先打好py基礎!!!然後邊啃邊理解更深入且優秀的編程思想!只要哪天你感覺看代碼就跟看小說似的無壓力,那一切就容易啦。
我讀過py好多優秀開源,如sqlmap,wapti,一些優秀庫的實現。
然後,得有安全基本功,要不然也沒法理解安全類的源碼是什麼個思想。
最後,得有架構基本功,否則根本不知道這些API為什麼存在,框架與插件之間為何這樣通信,為什麼要這樣分模塊,有什麼地方可以優化的等等。
萬事開頭難,啃吧,調試吧!越到後面會越輕鬆,然後來給我點個贊
api介面和python庫的區別是什麼?
API
介面屬於一種操作系統或程序介面,而後兩者都屬於直接用戶介面。
有時公司會將
API
作為其公共開放系統。也就是說,公司制定自己的系統介面標準,當需要執行系統整合、自定義和程序應用等操作時,公司所有成員都可以通過該介面標準調用源代碼,該介面標準被稱之為開放式
API。
從哪能找到python示常式序或源碼
參考資源 1.Python網站,
2.諾基亞論壇,,「Python for Series 60 Platform API參考」
3.諾基亞論壇,,「用Python for Series 60 Platform編程」
4.Python for S60 Wiki
5.Python for S60開發夥伴討論區
6.更多信息,
遊戲演算法……這個就複雜了,一個遊戲可大可小,涉及的東西博大精深,你的意思應該是想看哈python s60上面的遊戲源代碼哇,呵呵我沒有弄過,其實只要你熟悉這個語言和平台了 就可以按照自己的想法寫
寫遊戲是種想法 python只是工具 s60隻是平台, 我記得有個外國超人說過 大概的意思”只要你能讀寫顯存和鍵盤輸入就能寫出DOOM來”
如何獲取python模塊的api
一:用C API為Python寫C語言函數,以方便Python中調用 1. 首先實現一個特定原型的函數,用Python C API來實現的話,所有函數必須是這種原型。必須是類似這樣的 PyObject *Fun(PyObject *self, PyObject *args) self應該是在用類的時候才會用到
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/293616.html