在開發(fā)模塊之前宴凉,現(xiàn)問下自己幾個問題?
- 官方是否有提供的類似功能模塊亮蛔?
可從下面兩個連接確定官方提供的模塊,以免重復造輪子
官方已發(fā)布的模塊 http://docs.ansible.com/ansible/modules.html
官方正在開發(fā)的模塊 https://github.com/ansible/ansible/labels/module - 你需要開發(fā)一個action 插件么味榛?
action插件是在ansible主機上運行,而不是在目標主機上運行的予跌。對于類似file/copy/template功能的模塊搏色,在模塊執(zhí)行前需要在ansible主機上做一些操作的。有關(guān)action插件的開發(fā)請移步到
明確幾點
- 模塊是傳送到目標主機上運行的匕得。
- 模塊的返回值必須是json dumps的字符串继榆。
執(zhí)行模塊的過程
首先巾表,將模塊文件讀入內(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í)行過程
模塊工具
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