最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ansible作為python模塊庫使用的方法實(shí)例

 更新時(shí)間:2017年01月17日 16:11:42   作者:mindg.cn  
ansible是一個(gè)python package,是個(gè)完全的unpack and play軟件,對(duì)客戶端唯一的要求是有ssh有python,并且裝了python-simplejson包,部署上簡單到發(fā)指。下面這篇文章就給大家主要介紹了ansible作為python模塊庫使用的方法實(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è)庫組件寫入到自己的腳本中,今天的腳本腳本就將展示下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ì)接。

簡單的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í)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

相關(guān)文章

  • 解決pip install中UnicodeDecodeError問題的處理

    解決pip install中UnicodeDecodeError問題的處理

    這篇文章主要介紹了解決pip install中UnicodeDecodeError問題的處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • python實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲(pygame版)

    python實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲(pygame版)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)pygame版的飛機(jī)大戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • numpy.random.shuffle打亂順序函數(shù)的實(shí)現(xiàn)

    numpy.random.shuffle打亂順序函數(shù)的實(shí)現(xiàn)

    這篇文章主要介紹了numpy.random.shuffle打亂順序函數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 使用Python批量修改文件名的代碼實(shí)例

    使用Python批量修改文件名的代碼實(shí)例

    今天小編就為大家分享一篇關(guān)于使用Python批量修改文件名的代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • keras.layers.Conv2D()函數(shù)參數(shù)用法及說明

    keras.layers.Conv2D()函數(shù)參數(shù)用法及說明

    這篇文章主要介紹了keras.layers.Conv2D()函數(shù)參數(shù)用法及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • python實(shí)現(xiàn)對(duì)輸入的密文加密

    python實(shí)現(xiàn)對(duì)輸入的密文加密

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)對(duì)輸入的密文加密,分析python求解簡單加密問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • Python如何通過ip2region解析IP獲得地域信息

    Python如何通過ip2region解析IP獲得地域信息

    這篇文章主要介紹了Python如何通過ip2region解析IP獲得地域信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 解決windows下Sublime Text 2 運(yùn)行 PyQt 不顯示的方法分享

    解決windows下Sublime Text 2 運(yùn)行 PyQt 不顯示的方法分享

    問題描述:PyQt 環(huán)境正常,可以使用 Windows 的 虛擬 DOS 正常運(yùn)行,但在 Sublime Text 2 下使用 Ctrl + B 運(yùn)行后,界面不顯示,但查看任務(wù)管理器,有 python.exe 進(jìn)程。
    2014-06-06
  • 利用Python實(shí)現(xiàn)給圖像添加標(biāo)簽

    利用Python實(shí)現(xiàn)給圖像添加標(biāo)簽

    這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)給指定的圖片添加標(biāo)簽,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下
    2023-07-07
  • Python工廠模式實(shí)現(xiàn)封裝Webhook群聊機(jī)器人詳解

    Python工廠模式實(shí)現(xiàn)封裝Webhook群聊機(jī)器人詳解

    企業(yè)存在給 特定群組 自動(dòng)推送消息的需求,你可以在群聊中添加一個(gè)自定義機(jī)器人,通過服務(wù)端調(diào)用 webhook 地址,即可將外部系統(tǒng)的通知消息即時(shí)推送到群聊中。本文就來和大家聊聊具體實(shí)現(xiàn)方法
    2023-02-02

最新評(píng)論

张家港市| 桦川县| 贵州省| 宁城县| 乾安县| 江川县| 潮安县| 叙永县| 霍山县| 什邡市| 澄城县| 景宁| 铜山县| 敦煌市| 贵州省| 文山县| 错那县| 宝清县| 金阳县| 吕梁市| 盐源县| 盐亭县| 高阳县| 黄平县| 大方县| 洛宁县| 邓州市| 胶州市| 普兰店市| 崇义县| 阳东县| 嘉黎县| 鹤岗市| 酉阳| 平遥县| 湘西| 鲁甸县| 富源县| 城口县| 绥阳县| 奉新县|