Ansible 開發(fā)模塊 之【模塊說明】

在開發(fā)模塊之前宴凉,現(xiàn)問下自己幾個問題?

  1. 官方是否有提供的類似功能模塊亮蛔?
    可從下面兩個連接確定官方提供的模塊,以免重復造輪子
    官方已發(fā)布的模塊 http://docs.ansible.com/ansible/modules.html
    官方正在開發(fā)的模塊 https://github.com/ansible/ansible/labels/module
  2. 你需要開發(fā)一個action 插件么味榛?
    action插件是在ansible主機上運行,而不是在目標主機上運行的予跌。對于類似file/copy/template功能的模塊搏色,在模塊執(zhí)行前需要在ansible主機上做一些操作的。有關(guān)action插件的開發(fā)請移步到

明確幾點

- 模塊是傳送到目標主機上運行的匕得。
- 模塊的返回值必須是json dumps的字符串继榆。

執(zhí)行模塊的過程

image.png

首先巾表,將模塊文件讀入內(nèi)存汁掠,然后添加傳遞給模塊的參數(shù),最后將模塊中所需要的類添加到內(nèi)存集币,由zipfile壓縮后考阱,再由base64進行編碼,寫入到模版文件內(nèi)鞠苟。

通過默認的連接方式乞榨,一般是ssh。ansible通過ssh連接到遠程主機当娱,創(chuàng)建臨時目錄吃既,并關(guān)閉連接。然后將打開另外一個ssh連接跨细,將模版文件以sftp方式傳送到剛剛創(chuàng)建的臨時目錄中鹦倚,寫完后關(guān)閉連接。然后打開一個ssh連接將任務對象賦予可執(zhí)行權(quán)限冀惭,執(zhí)行成功后關(guān)閉連接震叙。

最后,ansible將打開第三個連接來執(zhí)行模塊散休,并刪除臨時目錄及其所有內(nèi)容媒楼。模塊的結(jié)果是從標準輸出stdout中獲取json格式的字符串。ansible將解析和處理此字符串戚丸。如果有任務是異步控制執(zhí)行的划址,ansible將在模塊完成之前關(guān)閉第三個連接,并且返回主機后限府,在規(guī)定的時間內(nèi)檢查任務狀態(tài)夺颤,直到模塊完成或規(guī)定的時間超時。

使用了管道連接后谣殊,與遠程主機只有一個連接拂共,命令通過數(shù)據(jù)流的方式發(fā)送執(zhí)行。

配置方式

vim /etc/ansible/ansible.cfg
pipelining = True

執(zhí)行過程


image.png

模塊工具


Ansible提供了許多模塊實用程序姻几,它們提供了在開發(fā)自己的模塊時可以使用的輔助功能宜狐。 basic.py模塊為程序提供訪問Ansible庫的主要入口點势告,所有Ansible模塊必須至少從basic.py導入:
from ansible.module_utils.basic import *

其他模塊工具
a10.py - Utilities used by the a10_server module to manage A10 Networks devices.
api.py - Adds shared support for generic API modules.
aos.py - Module support utilities for managing Apstra AOS Server.
asa.py - Module support utilities for managing Cisco ASA network devices.
azure_rm_common.py - Definitions and utilities for Microsoft Azure Resource Manager template deployments.
basic.py - General definitions and helper utilities for Ansible modules.
cloudstack.py - Utilities for CloudStack modules.
database.py - Miscellaneous helper functions for PostGRES and MySQL
docker_common.py - Definitions and helper utilities for modules working with Docker.
ec2.py - Definitions and utilities for modules working with Amazon EC2
eos.py - Helper functions for modules working with EOS networking devices.
f5.py - Helper functions for modules working with F5 networking devices.
facts.py - Helper functions for modules that return facts.
gce.py - Definitions and helper functions for modules that work with Google Compute Engine resources.
ios.py - Definitions and helper functions for modules that manage Cisco IOS networking devices
iosxr.py - Definitions and helper functions for modules that manage Cisco IOS-XR networking devices
ismount.py - Contains single helper function that fixes os.path.ismount
junos.py - Definitions and helper functions for modules that manage Junos networking devices
known_hosts.py - utilities for working with known_hosts file
mysql.py - Allows modules to connect to a MySQL instance
netapp.py - Functions and utilities for modules that work with the NetApp storage platforms.
netcfg.py - Configuration utility functions for use by networking modules
netcmd.py - Defines commands and comparison operators for use in networking modules
network.py - Functions for running commands on networking devices
nxos.py - Contains definitions and helper functions specific to Cisco NXOS networking devices
openstack.py - Utilities for modules that work with Openstack instances.
openswitch.py - Definitions and helper functions for modules that manage OpenSwitch devices
powershell.ps1 - Utilities for working with Microsoft Windows clients
pycompat24.py - Exception workaround for Python 2.4.
rax.py - Definitions and helper functions for modules that work with Rackspace resources.
redhat.py - Functions for modules that manage Red Hat Network registration and subscriptions
service.py - Contains utilities to enable modules to work with Linux services (placeholder, not in use).
shell.py - Functions to allow modules to create shells and work with shell commands
six/init.py - Bundled copy of the Six Python library to aid in writing code compatible with both Python 2 and Python 3.
splitter.py - String splitting and manipulation utilities for working with Jinja2 templates
urls.py - Utilities for working with http and https requests
vca.py - Contains utilities for modules that work with VMware vCloud Air
vmware.py - Contains utilities for modules that work with VMware vSphere VMs
vyos.py - Definitions and functions for working with VyOS networking

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市抚恒,隨后出現(xiàn)的幾起案子咱台,更是在濱河造成了極大的恐慌,老刑警劉巖俭驮,帶你破解...
    沈念sama閱讀 222,865評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件回溺,死亡現(xiàn)場離奇詭異,居然都是意外死亡混萝,警方通過查閱死者的電腦和手機遗遵,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,296評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來逸嘀,“玉大人车要,你說我怎么就攤上這事≌柑龋” “怎么了翼岁?”我有些...
    開封第一講書人閱讀 169,631評論 0 364
  • 文/不壞的土叔 我叫張陵,是天一觀的道長司光。 經(jīng)常有香客問我琅坡,道長,這世上最難降的妖魔是什么残家? 我笑而不...
    開封第一講書人閱讀 60,199評論 1 300
  • 正文 為了忘掉前任榆俺,我火速辦了婚禮,結(jié)果婚禮上跪削,老公的妹妹穿的比我還像新娘谴仙。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 69,196評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著宏娄,像睡著了一般。 火紅的嫁衣襯著肌膚如雪掀虎。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,793評論 1 314
  • 那天付枫,我揣著相機與錄音烹玉,去河邊找鬼。 笑死阐滩,一個胖子當著我的面吹牛二打,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播掂榔,決...
    沈念sama閱讀 41,221評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼继效,長吁一口氣:“原來是場噩夢啊……” “哼症杏!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起瑞信,我...
    開封第一講書人閱讀 40,174評論 0 277
  • 序言:老撾萬榮一對情侶失蹤厉颤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后凡简,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體逼友,經(jīng)...
    沈念sama閱讀 46,699評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,770評論 3 343
  • 正文 我和宋清朗相戀三年秤涩,在試婚紗的時候發(fā)現(xiàn)自己被綠了帜乞。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,918評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡溉仑,死狀恐怖挖函,靈堂內(nèi)的尸體忽然破棺而出状植,到底是詐尸還是另有隱情浊竟,我是刑警寧澤,帶...
    沈念sama閱讀 36,573評論 5 351
  • 正文 年R本政府宣布津畸,位于F島的核電站振定,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏肉拓。R本人自食惡果不足惜后频,卻給世界環(huán)境...
    茶點故事閱讀 42,255評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望暖途。 院中可真熱鬧卑惜,春花似錦、人聲如沸驻售。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,749評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽欺栗。三九已至毫痕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間迟几,已是汗流浹背消请。 一陣腳步聲響...
    開封第一講書人閱讀 33,862評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留类腮,地道東北人臊泰。 一個月前我還...
    沈念sama閱讀 49,364評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像蚜枢,于是被迫代替她去往敵國和親缸逃。 傳聞我的和親對象是個殘疾皇子七婴,可洞房花燭夜當晚...
    茶點故事閱讀 45,926評論 2 361

推薦閱讀更多精彩內(nèi)容