2017-08-27 8 views
0

これは、現在のシステムからdatetimeを取得するために作成したカスタムモジュールです。私はモジュールを/ usr/share/my_modulesフォルダに入れました。可能性のあるモジュールでカスタムモジュールを作成する方法

#!/usr/bin/python 

import datetime 
import json 

date = str(datetime.datetime.now()) 
print(json.dumps({ 
    "time" : date 
})) 
def main(): 
    module = AnsibleModule(
     argument_spec = dict(
      state  = dict(default='present', choices=['present', 'absent']), 
      name  = dict(required=True), 
      enabled = dict(required=True, type='bool'), 
      something = dict(aliases=['whatever']) 
     ) 
    ) 
module.exit_json(changed=True, something_else=12345) 
module.fail_json(msg="Something fatal happened") 
from ansible.module_utils.basic import * 
from ansible.module_utils.basic import AnsibleModule 
if __name__ == '__main__': 
    main() 

そして、私は
timetestコマンドansibleローカル-mを使用して、それを実行しようとすると、それは私のカスタムモジュールを実行していない理由は、今、私はこのエラーに

127.0.0.1 | FAILED! => { 
    "failed": true, 
    "msg": "The module timetest was not found in configured module paths. Additionally, core modules are missing. If this is a checkout, run 'git submodule update --init --recursive' to correct this problem." 
} 

を取得していますか?この問題を解決するのを手伝ってください。あなたの脚本が存在するディレクトリ内libraryディレクトリを作成することができます

答えて

0

、あなたのファイル構造は次のようになります。

. 
|-- playbook.yml 
|-- library 
    `-- your-custom-module.py 

を希望します

+0

ありがとうArbab。私はこのパスをansible.cfgで言及するべきですか? –

+0

モジュールをライブラリに入れた後も、エラーを解決できません。 –

+0

これで、playbook内のライブラリフォルダに置かれたカスタム作成モジュールを実行できるようになりました。また、アドホックコマンドによっても実行できます。おかげさまでArbab。 –

0

あなたはAnsibleテストモジュールの指示に従ってしようとしたことがあり役立つかもしれませんhttp://ansible-docs.readthedocs.io/zh/stable-2.0/rst/developing_modules.html#testing-modulesに?

git clone git://github.com/ansible/ansible.git --recursive 
source ansible/hacking/env-setup 
chmod +x ansible/hacking/test-module 
ansible/hacking/test-module -m ./timetest.py 
+0

ya私はそのimmaを試しました。出力を得ることができますが、アドホックコマンドでモジュールを使用しようとすると、エラーが発生しました –

+0

ありがとうございます。今働いている。しかし、私はこのテストモジュールが何をしているのか分かりません。私は私のカスタムモジュールをテストすることができます、私がプレイブックやアドホックコマンドでそれを使用しようとしたら?なぜこれが特に必要なimmaですか? –

+0

開発中にモジュールをテストするのを助けることを目的としています。 https://github.com/ansible/ansible/blob/devel/hacking/README.mdをご覧ください。あなたのモジュールをビルドしたら、それをAnabilities内で使用するには、ANSIBLE_LIBRARY変数にあることを確認する必要があります。 – Imma

関連する問題