python代碼調用ansible


使用ansible_runner模塊,需要安裝2個模塊pip install ansible_runner ansible

extravars = {'ansible_user': 'test'} # 設置執行參數
inventory = "/PythonCode/test/inventory.hosts" #設置需要連接的遠程機器的文件,可以用openfile動態創建
private_data_dir = os.path.join("/PythonCode/test/", uuid.uuid4().hex) # 創建臨時文件夾用於存放ansible的臨時文件
os.mkdir(private_data_dir)
r = ansible_runner.run(inventory=inventory, extravars=extravars, private_data_dir=private_data_dir, host_pattern='all', module='shell', module_args='sh /tmp/test.sh', )
print("{}: {}".format(r.status, r.rc))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM