#author("2020-03-06T00:32:41+09:00","default:haikikyou","haikikyou")
[[Ansible]]
#contents
* Ansible Moduleの開発 [#nc7313c0]
Ansibleでは独自のモジュールを組み込み実行することができる。~
pythonでプログラムが書ければ、以下のようにtaskの中で作成したモジュールを使用することができる。他にもfilterやlookup pluginsなど、module以外にも独自の拡張ができる。
#geshi{{{
- my_module:
param_a: hello
paramb_b: world
}}}
module作成は、公式のドキュメントやansibleのmodulesディレクトリ下のデフォルトで用意されているmoduleを見ると良いだろう。
&label(warn){参考};
- https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html
- https://github.com/ansible/ansible/tree/devel/lib/ansible/modules
** moduleの作成 [#v1f1f382]
まずはシンプルなmoduleを見てみる。~
以下は、helloというモジュール名でechoで受け取ったメッセージを返すだけのモジュールである。
#geshi(python){{{
# AnsibleModuleの読み込み
from ansible.module_utils.basic import AnsibleModule
def main():
module = AnsibleModule(
# moduleのパラメータ
argument_spec=dict(
echo=dict(type='str', required=True),
),
supports_check_mode=True
)
if module.check_mode:
module.exit_json(**result)
result = dict(
changed=False,
message=module.params['echo'],
)
# moduleの出力
module.exit_json(**result)
if __name__ == '__main__':
main()
}}}
このmoduleは以下のような使用方法となる。
#geshi{{{
- hello:
echo: hello world
}}}
** AnsibleModule [#va2bce6c]
** moduleの引数 [#v4a0ca90]
|~head1|~head2|h
|body1|body2|
* moduleのロード [#i0799552]
Ansibleがデフォルトでmoduleを探索するパスに設置する、または、ansible.cfgでmoduleのパスを指定しすればよい。そうすればAnsibleがプレイブックを実行時にロードしてくれる。
- 環境変数&code(){ANSIBLE_LIBRARY};
- &code(){~/.ansible/plugins/modules/};
- &code(){/usr/share/ansible/plugins/modules/};
** ansible.cfgに指定 [#a2ac2850]
&code(){ansible.cfg};の&code(){library};というパラメタに自作したmoduleのパスを指定する。~
ディレクトリ構成は以下のとおり。~
#geshi{{{
$ tree
.
├── ansible.cfg
├── mod
│ └── hello.py
└── test.yaml
1 directory, 3 files
}}}
''ansible.cfg''
以下では、&code(){mod};というディレクトリを相対パスでしている。
#geshi(ini){{{
[defaults]
library = ./mod
}}}
以下では、&code(){ansible.cfg};があるディレクトリでplaybookを実行する。
#geshi{{{
$ ansible-playbook test.yaml -v
Using /test/ansible/module_test/ansible_cfg_module_test/ansible.cfg as config file
PLAY [localhost] **********************************************************************************
TASK [hello] **************************************************************************************
ok: [localhost] => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "message": "world"}
PLAY RECAP ****************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
}}}
&label(sample){サンプル}; &ref(./ansible_cfg_module_test.tar.gz,100%);
** libraryのローカルロード [#c15ad126]
playbook及び特定のroleに対しmoduleをロードさせることができる。~
playbookや特定のroleの下にlibraryフォルダを設定することで可能である。~
実際に自作moduleがロードされるか確認する。
テストで使用するソースは以下のとおり。
''hello.py''
#geshi(python){{{
from ansible.module_utils.basic import AnsibleModule
def main():
module = AnsibleModule(
argument_spec=dict(
echo=dict(type='str', required=True),
),
supports_check_mode=True
)
if module.check_mode:
module.exit_json(**result)
result = dict(
changed=False,
message=module.params['echo'],
)
module.exit_json(**result)
if __name__ == '__main__':
main()
}}}
*** &label(study){実験}; プレイブックに適用する [#b0469210]
playbookのあるパスに&code(){library};というディレクトリを配置する。
ディレクトリ構成は以下。
#geshi{{{
$ tree
.
├── library
│ └── hello.py
└── test.yaml
1 directory, 2 files
}}}
''test.yaml''
#geshi(yaml){{{
- hosts: localhost
connection: local
gather_facts: no
tasks:
- hello: echo=world
}}}
プレイブックを実行する。(簡単のためconnectionはlocal)
#geshi{{{
$ ls
library test.yaml
$ ansible-playbook test.yaml -v
Using /etc/ansible/ansible.cfg as config file
PLAY [localhost] **********************************************************************************
TASK [hello] **************************************************************************************
ok: [localhost] => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "message": "world"}
PLAY RECAP ****************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
}}}
&label(sample){サンプル};&ref(playbooks_test.tar.gz);
*** &label(study){実験}; 特定のroleに適用する [#sd1cad44]
roleの下に&code(){library};というディレクトリを配置する。
#geshi{{{
$ tree
.
├── db.yaml
├── roles
│ ├── db
│ │ └── tasks
│ │ └── main.yaml
│ └── web
│ ├── library
│ │ └── hello.py
│ └── tasks
│ └── main.yaml
└── web.yaml
6 directories, 5 files
}}}
''web.yaml''
#geshi(yaml){{{
- hosts: localhost
connection: local
gather_facts: no
roles:
- web
}}}
''roles/web/tasks/main.yaml''
#geshi(yaml){{{
- hello: echo=web
}}}
''db.yaml''
#geshi(yaml){{{
- hosts: localhost
connection: local
gather_facts: no
roles:
- db
}}}
''roles/db/tasks/main.yaml''
#geshi(yaml){{{
- hello: echo=db
}}}
プレイブックを実行する。
#geshi{{{
$ ansible-playbook web.yaml -v
Using /etc/ansible/ansible.cfg as config file
PLAY [localhost] **********************************************************************************
TASK [web : hello] ********************************************************************************
ok: [localhost] => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "message": "web"}
PLAY RECAP ****************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
}}}
roleがdbの場合はmoduleのロードに失敗しエラーとなる。
#geshi{{{
$ ansible-playbook db.yaml -v
Using /etc/ansible/ansible.cfg as config file
ERROR! couldn't resolve module/action 'hello'. This often indicates a misspelling, missing collection, or incorrect module path.
The error appears to be in '/test/ansible/module_test/roles_test/roles/db/tasks/main.yaml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- hello: echo=db
^ here
}}}
&label(sample){サンプル}; &ref(./roles_test.tar.gz,100%);
&label(warn){参考};
- https://docs.ansible.com/ansible/latest/dev_guide/developing_locally.html#developing-locally
- https://docs.ansible.com/ansible/latest/reference_appendices/config.html
* 参考リンク [#k3ccef05]
- moduleの開発
-- https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html
- moduleの追加とロード
-- https://docs.ansible.com/ansible/latest/dev_guide/developing_locally.html#developing-locally
- Ansible Config
-- https://docs.ansible.com/ansible/latest/reference_appendices/config.html