Skip to content

Commit 4a55105

Browse files
committed
Update module developement
* Updated developer guide for module development to work with collection and PYTHONPATH Fixes: ansible#2374 Signed-off-by: Abhijeet Kasurde <[email protected]>
1 parent 1b91cd7 commit 4a55105

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

docs/docsite/rst/dev_guide/developing_modules_general.rst

+28-6
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,27 @@ Modules for inclusion in Ansible itself must be Python or Powershell.
2626
One advantage of using Python or Powershell for your custom modules is being able to use the ``module_utils`` common code that does a lot of the
2727
heavy lifting for argument processing, logging and response writing, among other things.
2828

29-
Creating a module
30-
=================
29+
Creating a standalone module
30+
============================
3131

3232
It is highly recommended that you use a ``venv`` or ``virtualenv`` for Python development.
3333

34-
To create a module:
34+
To create a standalone module:
3535

3636
1. Create a ``library`` directory in your workspace. Your test play should live in the same directory.
3737
2. Create your new module file: ``$ touch library/my_test.py``. Or just open/create it with your editor of choice.
3838
3. Paste the content below into your new module file. It includes the :ref:`required Ansible format and documentation <developing_modules_documenting>`, a simple :ref:`argument spec for declaring the module options <argument_spec>`, and some example code.
3939
4. Modify and extend the code to do what you want your new module to do. See the :ref:`programming tips <developing_modules_best_practices>` and :ref:`Python 3 compatibility <developing_python_3>` pages for pointers on writing clean and concise module code.
4040

41+
Creating a module in a collection
42+
=================================
43+
44+
To create a new module in an existing collection called ``my_namespace.my_collection``:
45+
46+
1. Create your new module file: ``$ touch <PATH_TO_COLLECTION>/ansible_collections/my_namespace/my_collection/plugins/modules/my_test.py``. Or just create it with your editor of choice.
47+
2. Paste the content below into your new module file. It includes the :ref:`required Ansible format and documentation <developing_modules_documenting>`, a simple :ref:`argument spec for declaring the module options <argument_spec>`, and some example code.
48+
3. Modify and extend the code to do what you want your new module to do. See the :ref:`programming tips <developing_modules_best_practices>` and :ref:`Python 3 compatibility <developing_python_3>` pages for pointers on writing clean and concise module code.
49+
4150
.. literalinclude:: ../../../../examples/scripts/my_test.py
4251
:language: python
4352

@@ -69,7 +78,6 @@ You can add your facts into ``ansible_facts`` field of the result as follows:
6978
.. code-block:: python
7079
7180
module.exit_json(changed=False, ansible_facts=dict(my_new_fact=value_of_fact))
72-
7381
7482
The rest is just like creating a normal module.
7583

@@ -95,8 +103,15 @@ If your module does not need to target a remote host, you can quickly and easily
95103
96104
ANSIBLE_LIBRARY=./library ansible -m my_test -a 'name=hello new=true' localhost
97105
98-
- If for any reason (pdb, using print(), faster iteration, etc) you want to avoid going through Ansible,
99-
another way is to create an arguments file, a basic JSON config file that passes parameters to your module so that you can run it.
106+
For a module developed in an existing collection called ``my_namespace.my_collection`` (as mentioned above):
107+
108+
.. code:: shell
109+
110+
$ ansible localhost -m my_namespace.my_collection.my_test -a 'name=hello new=true' --playbook-dir=$PWD
111+
112+
- If you use ``pdb``, ``print()``, or some other method of local debugging for faster iteration,
113+
you can avoid going through Ansible by creating an arguments file,
114+
a basic JSON config file that passes parameters to your module so that you can run it.
100115
Name the arguments file ``/tmp/args.json`` and add the following content:
101116

102117
.. code:: json
@@ -114,6 +129,13 @@ If your module does not need to target a remote host, you can quickly and easily
114129
115130
$ python library/my_test.py /tmp/args.json
116131
132+
- For a module developed in an existing collection called ``my_namespace.my_collection``, you may need to do:
133+
134+
.. code:: console
135+
136+
$ PYTHONPATH=PATH_TO_COLLECTIONS python -m ansible_collections.my_namespace.my_collection.plugins.modules.my_test ./args.json
137+
138+
117139
It should return output like this:
118140

119141
.. code:: json

0 commit comments

Comments
 (0)