ksaitoの日記

日々試したことの覚え書き

ansibleのコアモジュール

移転しました。

自動的にリダイレクトします。

Developing Modulesを参考にansibleのcoreモジュールを弄ってみました。

環境準備

オフシャルドキュメントのチュートリアルにテスト方法とかんたんなモジュールの作り方が書かれています。

下記でテスト環境を準備します。

$ git clone git://github.com/ansible/ansible.git --recursive
$ source ansible/hacking/env-setup
$ chmod +x ansible/hacking/test-module

pingモジュール

pingモジュールは、lib/ansible/modules/core/system/ping.pyにあります。

普通に使うとこんな感じ

$ ansible -m ping localhost
localhost | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
$ 

戻り値のpongpong!!に修正します。 gitのsubmoduleになっているようです。

$ git diff | cat
diff --git a/lib/ansible/modules/core b/lib/ansible/modules/core
--- a/lib/ansible/modules/core
+++ b/lib/ansible/modules/core
@@ -1 +1 @@
-Subproject commit 45367c3d090ccf4d649b103b50b6eec939b6ee14
+Subproject commit 45367c3d090ccf4d649b103b50b6eec939b6ee14-dirty
$ cd lib/ansible/modules/core/system
$ git --no-pager diff
diff --git a/system/ping.py b/system/ping.py
index ed93f7d..9f8ddf6 100644
--- a/system/ping.py
+++ b/system/ping.py
@@ -49,7 +49,7 @@ def main():
         ),
         supports_check_mode = True
     )
-    result = dict(ping='pong')
+    result = dict(ping='pong!!')
     if module.params['data']:
         if module.params['data'] == 'crash':
             raise exceptions.Exception("boom")
$ 

修正後に実行するとこんな感じ

$ ansible -m ping localhost
localhost | SUCCESS => {
    "changed": false, 
    "ping": "pong!!"
}
$ 

command/shellモジュール

command/shellモジュールは、lib/ansible/modules/core/commandsにあります。

実行するとこんな感じ

$ ansible localhost -m shell -a "/bin/ls"
localhost | SUCCESS | rc=0 >>
__init__.py
command.py
raw.py
script.py
shell.py

$ ansible localhost -m command -a "/bin/ls"
localhost | SUCCESS | rc=0 >>
__init__.py
command.py
raw.py
script.py
shell.py

$ 

test-moduleで実行するには、こんな感じで実行します。

$ $ANSIBLE_HOME/hacking/test-module -m command.py -a "/bin/ls"
* including generated source, if any, saving to: /home/vagrant/.ansible_module_generated
* this may offset any line numbers in tracebacks/debuggers!
***********************************
RAW OUTPUT
{"changed": true, "end": "2016-03-03 18:31:12.148437", "stdout": "__init__.py\ncommand.py\nraw.py\nscript.py\nshell.py", "cmd": ["/bin/ls"], "rc": 0, "start": "2016-03-03 18:31:12.145253", "stderr": "", "delta": "0:00:00.003184", "invocation": {"module_args": {"warn": true, "executable": null, "chdir": null, "_raw_params": "/bin/ls", "removes": null, "creates": null, "_uses_shell": false}}, "warnings": []}


***********************************
PARSED OUTPUT
{
    "changed": true, 
    "cmd": [
        "/bin/ls"
    ], 
    "delta": "0:00:00.003184", 
    "end": "2016-03-03 18:31:12.148437", 
    "invocation": {
        "module_args": {
            "_raw_params": "/bin/ls", 
            "_uses_shell": false, 
            "chdir": null, 
            "creates": null, 
            "executable": null, 
            "removes": null, 
            "warn": true
        }
    }, 
    "rc": 0, 
    "start": "2016-03-03 18:31:12.145253", 
    "stderr": "", 
    "stdout": "__init__.py\ncommand.py\nraw.py\nscript.py\nshell.py", 
    "warnings": []
}
$ 

デバッガを使うには、下記のようにします。

デバッガは、Python付属のpdbで開発用にクローンしてtest-moduleで生成・実行されたソースをデバッグすることができます。

$ pdb $ANSIBLE_HOME/hacking/test-module -m command.py -a "/bin/ls" --debugger /usr/bin/pdb
> /home/vagrant/git/module-dev/ansible/hacking/test-module(32)<module>()
-> import sys
(Pdb) q
$ $ANSIBLE_HOME/hacking/test-module -m command.py -a "/bin/ls" --debugger /usr/bin/pdb
* including generated source, if any, saving to: /home/vagrant/.ansible_module_generated
* this may offset any line numbers in tracebacks/debuggers!
> /home/vagrant/.ansible_module_generated(22)<module>()
-> import copy
(Pdb) list
 17     # GNU General Public License for more details.
 18     #
 19     # You should have received a copy of the GNU General Public License
 20     # along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
 21
 22  -> import copy
 23     import sys
 24     import datetime
 25     import glob
 26     import traceback
 27     import re
(Pdb)