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))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章