Skip to content

Commit 7ce35f1

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 7ce35f1

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

docs/docsite/rst/dev_guide/developing_modules_general.rst

+26-5
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 ``foo.bar``:
45+
46+
1. Create your new module file: ``$ touch <PATH_TO_COLLECTION>/ansible_collections/foo/bar/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,7 +103,13 @@ 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,
106+
For module developed in an existing collection called ``foo.bar`` (As mentioned above):
107+
108+
.. code:: shell
109+
110+
$ ansible localhost -m foo.bar.my_test -a 'name=hello new=true' --playbook-dir=$PWD
111+
112+
- If for any reason (``pdb``, using ``print()``, faster iteration and so on) you want to avoid going through Ansible,
99113
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.
100114
Name the arguments file ``/tmp/args.json`` and add the following content:
101115

@@ -114,6 +128,13 @@ If your module does not need to target a remote host, you can quickly and easily
114128
115129
$ python library/my_test.py /tmp/args.json
116130
131+
- For the module developed in an existing collection called ``foo.bar``, you may need to do:
132+
133+
.. code:: console
134+
135+
$ PYTHONPATH=PATH_TO_COLLECTIONS python -m ansible_collections.foo.bar.plugins.modules.my_test ./args.json
136+
137+
117138
It should return output like this:
118139

119140
.. code:: json

0 commit comments

Comments
 (0)