如果不想使用清單文件或想使用動態清單,可以使用host_list參數。
導入Ansible。Runner導入JSON Runner = Ansible。跑步者。runner(host _ list =【“10.10 . 0 . 1“】,#此處,如果明確指定主機需要傳遞列表,或者指定動態清單腳本module _ name =‘ping‘,# module name module _ args =‘,# module parameter extra _ vars = {“ansi ble _ ssh _ user“:“root“,“ansi ble _ ssh _ pass“:“xx“},forks=10)ry from ansi ble . playbook . Play import Play from ansi ble . executor . task _ queue _ manager import TaskQueueManager from ansi ble . executor . playbook _ executor import playbook executor loader = data loader()#用於加載和分析yaml文件或JSON內容,並支持解密管理變量的vault variable _ manager = variable manager()#類,包括主機、組、擴展和其他變量。之前的版本是庫存=庫存(加載器=加載器,變量_管理器=變量_管理器)變量_管理器。set _ inventory(庫存)#根據庫存加載相應的變量類選項(對象):‘‘這是壹個公共* * *類。因為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 _ ad hoc():variable _ manager . extra _ vars = {“ansi ble _ ssh _ user“:“root“、“ansi ble _ ssh _ pass“:“XXX“} #添加外部變量#構建pb,這裏非常有趣。新版本需要構建這樣的pb來運行ad-hoc或playbook,但最後調用play的類是不同的# :param name: task name,類似於名稱#:param hosts in the tasks in the playbook:hosts #:param tasks in the playbook:tasks in the playbook,實際上這是playbook的語法,因為tasks的值是壹個列表。因此,您可以編寫多個task play _ source = {“name“:“ansi ble ad-hoc“、“hosts“:“10.10.1“、“gather _ facts“:“no“、“tasks“:【{“。“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:如果TQM
劇本類似於SaltStack中的狀態。