开发者:支持的模块及工作原理
开发 ansible 模块主要在 later section of the documentation 介绍,专注于 Linux/Unix 平台. 如果你想编写 Windows 的 ansible 模块该怎么办呢?
Windows 平台主要通过 PowerShell 模块实现. 开始之前可以先略过 Linux/Unix 模块开发章节.
Windows 模块在 Ansible “library/” 子目录下的 “windows/” 子目录下. 例如,如果一个模块命名为 “library/windows/win_ping”,那将会在 “win_ping” 文件中嵌入一个文档,实际的 PowerShell 代码将存在 “win_ping.ps1” 文件. 看下源代码会有更深入的了解.
模块(ps1 files)文件应该以如下格式开头:
#!powershell
# <license>
# WANT_JSON
# POWERSHELL_COMMON
# code goes here, reading in stdin as JSON and outputting JSON如上代码是为了告诉 ansible 合入一些代码并且 The above magic is necessary to tell Ansible to mix in some common code and also know how to push modules out. 常规代码包括好包装例如哈希数据结构,jason格式标准输出,还有一些更有用的东西.常规 Ansible 有着重复利用 Python 代码的理念 - 这点 Windows 也是等同的. 你刚看到的 windows/ 模块只是一个开始. 附加模块已经被 git push 到 github上了.
学员评价