ansible作為python模塊庫(kù)使用的方法實(shí)例
前言
ansible是新出現(xiàn)的自動(dòng)化運(yùn)維工具,基于Python開發(fā),集合了眾多運(yùn)維工具(puppet、cfengine、chef、func、fabric)的優(yōu)點(diǎn),實(shí)現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運(yùn)行命令等功能。ansible是基于模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運(yùn)行的模塊,ansible只是提供一種框架。
主要包括:
(1)、連接插件connection plugins:負(fù)責(zé)和被監(jiān)控端實(shí)現(xiàn)通信;
(2)、host inventory:指定操作的主機(jī),是一個(gè)配置文件里面定義監(jiān)控的主機(jī);
(3)、各種模塊核心模塊、command模塊、自定義模塊;
(4)、借助于插件完成記錄日志郵件等功能;
(5)、playbook:劇本執(zhí)行多個(gè)任務(wù)時(shí),非必需可以讓節(jié)點(diǎn)一次性運(yùn)行多個(gè)任務(wù)。
Asible是運(yùn)維工具中算是非常好的利器,我個(gè)人比較喜歡,可以根據(jù)需求靈活配置yml文件來實(shí)現(xiàn)不同的業(yè)務(wù)需求,因?yàn)椴恍枰惭b客戶端,上手還是非常容易的,在某些情況下你可能需要將ansible作為python的一個(gè)庫(kù)組件寫入到自己的腳本中,今天的腳本腳本就將展示下ansible如何跟python腳本結(jié)合,也就是如何在python腳本中使用ansible,我們逐步展開。
先看第一個(gè)例子:
#!/usr/bin/python import ansible.runner import ansible.playbook import ansible.inventory from ansible import callbacks from ansible import utils import json # the fastest way to set up the inventory # hosts list hosts = ["10.11.12.66"] # set up the inventory, if no group is defined then 'all' group is used by default example_inventory = ansible.inventory.Inventory(hosts) pm = ansible.runner.Runner( module_name = 'command', module_args = 'uname -a', timeout = 5, inventory = example_inventory, subset = 'all' # name of the hosts group ) out = pm.run() print json.dumps(out, sort_keys=True, indent=4, separators=(',', ': '))
這個(gè)例子展示我們?nèi)绾卧趐ython腳本中運(yùn)行如何通過ansible運(yùn)行系統(tǒng)命令,我們接下來看第二個(gè)例子,跟我們的yml文件對(duì)接。
簡(jiǎn)單的yml文件內(nèi)容如下:
- hosts: sample_group_name tasks: - name: just an uname command: uname -a
調(diào)用playbook的python腳本如下:
#!/usr/bin/python import ansible.runner import ansible.playbook import ansible.inventory from ansible import callbacks from ansible import utils import json ### setting up the inventory ## first of all, set up a host (or more) example_host = ansible.inventory.host.Host( name = '10.11.12.66', port = 22 ) # with its variables to modify the playbook example_host.set_variable( 'var', 'foo') ## secondly set up the group where the host(s) has to be added example_group = ansible.inventory.group.Group( name = 'sample_group_name' ) example_group.add_host(example_host) ## the last step is set up the invetory itself example_inventory = ansible.inventory.Inventory() example_inventory.add_group(example_group) example_inventory.subset('sample_group_name') # setting callbacks stats = callbacks.AggregateStats() playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY) runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY) # creating the playbook instance to run, based on "test.yml" file pb = ansible.playbook.PlayBook( playbook = "test.yml", stats = stats, callbacks = playbook_cb, runner_callbacks = runner_cb, inventory = example_inventory, check=True ) # running the playbook pr = pb.run() # print the summary of results for each host print json.dumps(pr, sort_keys=True, indent=4, separators=(',', ': '))
總結(jié)
以上就是為大家展示的2個(gè)小例子希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
- python ansible自動(dòng)化運(yùn)維工具執(zhí)行流程
- python中Ansible模塊的Playbook的具體使用
- ansible-playbook實(shí)現(xiàn)自動(dòng)部署KVM及安裝python3的詳細(xì)教程
- Python自動(dòng)化運(yùn)維之Ansible定義主機(jī)與組規(guī)則操作詳解
- python自動(dòng)化之Ansible的安裝教程
- python ansible服務(wù)及劇本編寫
- python將ansible配置轉(zhuǎn)為json格式實(shí)例代碼
- python開發(fā)的自動(dòng)化運(yùn)維工具ansible詳解
相關(guān)文章
python生成器generator:深度學(xué)習(xí)讀取batch圖片的操作
這篇文章主要介紹了python生成器generator:深度學(xué)習(xí)讀取batch圖片的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05python中Scikit-learn庫(kù)的高級(jí)特性和實(shí)踐分享
Scikit-learn是一個(gè)廣受歡迎的Python庫(kù),它用于解決許多機(jī)器學(xué)習(xí)的問題,在本篇文章中,我們將進(jìn)一步探索Scikit-learn的高級(jí)特性和最佳實(shí)踐,需要的朋友可以參考下2023-07-07Python隨機(jī)生成身份證號(hào)碼及校驗(yàn)功能
這篇文章主要介紹了Python隨機(jī)生成身份證號(hào)碼及校驗(yàn)功能,文中給大家提到了校驗(yàn)碼計(jì)算方法,需要的朋友可以參考下2018-12-12Python猴子補(bǔ)丁知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是關(guān)于Python猴子補(bǔ)丁知識(shí)點(diǎn)總結(jié),需要的朋友們學(xué)習(xí)下。2020-01-01Python+Delorean實(shí)現(xiàn)時(shí)間格式智能轉(zhuǎn)換
DeLorean是一個(gè)Python的第三方模塊,基于?pytz?和?dateutil?開發(fā),用于處理Python中日期時(shí)間的格式轉(zhuǎn)換。本文將詳細(xì)講講DeLorean的使用,感興趣的可以了解一下2022-04-04Python 讀取 YUV(NV12) 視頻文件實(shí)例
今天小編就為大家分享一篇Python 讀取 YUV(NV12) 視頻文件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12