mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Mention the AnsibleModule common code on the docs site
This commit is contained in:
parent
962e6e5572
commit
dde17404c6
3 changed files with 148 additions and 58 deletions
|
@ -153,8 +153,9 @@ s.parentNode.insertBefore(ga, s);
|
||||||
<li><a class="reference internal" href="#testing-modules">Testing Modules</a></li>
|
<li><a class="reference internal" href="#testing-modules">Testing Modules</a></li>
|
||||||
<li><a class="reference internal" href="#reading-input">Reading Input</a></li>
|
<li><a class="reference internal" href="#reading-input">Reading Input</a></li>
|
||||||
<li><a class="reference internal" href="#module-provided-facts">Module Provided ‘Facts’</a></li>
|
<li><a class="reference internal" href="#module-provided-facts">Module Provided ‘Facts’</a></li>
|
||||||
|
<li><a class="reference internal" href="#common-module-boilerplate">Common Module Boilerplate</a></li>
|
||||||
<li><a class="reference internal" href="#common-pitfalls">Common Pitfalls</a></li>
|
<li><a class="reference internal" href="#common-pitfalls">Common Pitfalls</a></li>
|
||||||
<li><a class="reference internal" href="#conventions">Conventions</a></li>
|
<li><a class="reference internal" href="#conventions-recomendations">Conventions/Recomendations</a></li>
|
||||||
<li><a class="reference internal" href="#shorthand-vs-json">Shorthand Vs JSON</a></li>
|
<li><a class="reference internal" href="#shorthand-vs-json">Shorthand Vs JSON</a></li>
|
||||||
<li><a class="reference internal" href="#sharing-your-module">Sharing Your Module</a></li>
|
<li><a class="reference internal" href="#sharing-your-module">Sharing Your Module</a></li>
|
||||||
<li><a class="reference internal" href="#getting-your-module-into-core">Getting Your Module Into Core</a></li>
|
<li><a class="reference internal" href="#getting-your-module-into-core">Getting Your Module Into Core</a></li>
|
||||||
|
@ -198,6 +199,10 @@ a module that just outputs the current time.</p>
|
||||||
<p>We are going to use Python here but any language is possible. Only File I/O and outputing to standard
|
<p>We are going to use Python here but any language is possible. Only File I/O and outputing to standard
|
||||||
out are required. So, bash, C++, clojure, Python, Ruby, whatever you want
|
out are required. So, bash, C++, clojure, Python, Ruby, whatever you want
|
||||||
is fine.</p>
|
is fine.</p>
|
||||||
|
<p>Now Python Ansible modules contain some extremely powerful shortcuts (that all the core modules use)
|
||||||
|
but first we are going to build a module the very hard way. The reason we do this is because modules
|
||||||
|
written in any language OTHER than Python are going to have to do exactly this. We’ll show the easy
|
||||||
|
way later.</p>
|
||||||
<p>So, here’s an example. You would never really need to build a module to set the system time,
|
<p>So, here’s an example. You would never really need to build a module to set the system time,
|
||||||
the ‘command’ module could already be used to do this. Though we’re going to make one.</p>
|
the ‘command’ module could already be used to do this. Though we’re going to make one.</p>
|
||||||
<p>Reading the modules that come with ansible (linked above) is a great way to learn how to write
|
<p>Reading the modules that come with ansible (linked above) is a great way to learn how to write
|
||||||
|
@ -224,7 +229,7 @@ you’ll turn to stone. Nobody ever executes async_wrapper directly.</p>
|
||||||
chmod +x ansible/hacking/test-module</pre>
|
chmod +x ansible/hacking/test-module</pre>
|
||||||
</div>
|
</div>
|
||||||
<p>Let’s run the script you just wrote with that:</p>
|
<p>Let’s run the script you just wrote with that:</p>
|
||||||
<div class="highlight-python"><pre>ansible/hacking/test-module ./time</pre>
|
<div class="highlight-python"><pre>ansible/hacking/test-module -m ./time</pre>
|
||||||
</div>
|
</div>
|
||||||
<p>You should see output that looks something like this:</p>
|
<p>You should see output that looks something like this:</p>
|
||||||
<div class="highlight-python"><div class="highlight"><pre><span class="p">{</span><span class="s">u'time'</span><span class="p">:</span> <span class="s">u'2012-03-14 22:13:48.539183'</span><span class="p">}</span>
|
<div class="highlight-python"><div class="highlight"><pre><span class="p">{</span><span class="s">u'time'</span><span class="p">:</span> <span class="s">u'2012-03-14 22:13:48.539183'</span><span class="p">}</span>
|
||||||
|
@ -357,17 +362,50 @@ this, just have the module return a <cite>ansible_facts</cite> key, like so, alo
|
||||||
A good idea might be make a module called ‘site_facts’ and always call it at the top of each playbook, though
|
A good idea might be make a module called ‘site_facts’ and always call it at the top of each playbook, though
|
||||||
we’re always open to improving the selection of core facts in Ansible as well.</p>
|
we’re always open to improving the selection of core facts in Ansible as well.</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" id="common-pitfalls">
|
<div class="section" id="common-module-boilerplate">
|
||||||
<h2>Common Pitfalls<a class="headerlink" href="#common-pitfalls" title="Permalink to this headline">¶</a></h2>
|
<h2>Common Module Boilerplate<a class="headerlink" href="#common-module-boilerplate" title="Permalink to this headline">¶</a></h2>
|
||||||
<p>If writing a module in Python and you have managed nodes running
|
<p>As mentioned, if you are writing a module in Python, there are some very powerful shortcuts you can use.
|
||||||
Python 2.4 or lower, this is generally a good idea, because
|
Modules are still transferred as one file, but an arguments file is no longer needed, so these are not
|
||||||
json isn’t in the Python standard library until 2.5.:</p>
|
only shorter in terms of code, they are actually FASTER in terms of execution time.</p>
|
||||||
<div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
|
<p>Rather than mention these here, the best way to learn is to read some of the <a class="reference external" href="https://github.com/ansible/ansible/tree/devel/library">source of the modules</a> that come with Ansible.</p>
|
||||||
<span class="kn">import</span> <span class="nn">json</span>
|
<p>The ‘group’ and ‘user’ modules are reasonably non-trival and showcase what this looks like.</p>
|
||||||
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
|
<p>Key parts include always ending the module file with:</p>
|
||||||
<span class="kn">import</span> <span class="nn">simplejson</span> <span class="kn">as</span> <span class="nn">json</span>
|
<div class="highlight-python"><div class="highlight"><pre><span class="c"># include magic from lib/ansible/module_common.py</span>
|
||||||
|
<span class="c">#<<INCLUDE_ANSIBLE_MODULE_COMMON>></span>
|
||||||
|
<span class="n">main</span><span class="p">()</span>
|
||||||
</pre></div>
|
</pre></div>
|
||||||
</div>
|
</div>
|
||||||
|
<p>And instantiating the module class like:</p>
|
||||||
|
<div class="highlight-python"><div class="highlight"><pre><span class="n">module</span> <span class="o">=</span> <span class="n">AnsibleModule</span><span class="p">(</span>
|
||||||
|
<span class="n">argument_spec</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
|
||||||
|
<span class="n">state</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="s">'present'</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="p">[</span><span class="s">'present'</span><span class="p">,</span> <span class="s">'absent'</span><span class="p">]),</span>
|
||||||
|
<span class="n">name</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">required</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
|
||||||
|
<span class="n">enabled</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">required</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="n">BOOLEANS</span><span class="p">),</span>
|
||||||
|
<span class="n">something</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">aliases</span><span class="o">=</span><span class="p">[</span><span class="s">'whatever'</span><span class="p">])</span>
|
||||||
|
<span class="p">)</span>
|
||||||
|
<span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
</div>
|
||||||
|
<p>The AnsibleModule provides lots of common code for handling returns, parses your arguments
|
||||||
|
for you, and allows you to check inputs.</p>
|
||||||
|
<p>Successful returns are made like this:</p>
|
||||||
|
<div class="highlight-python"><div class="highlight"><pre><span class="n">module</span><span class="o">.</span><span class="n">exit_json</span><span class="p">(</span><span class="n">changed</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">something_else</span><span class="o">=</span><span class="mi">12345</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
</div>
|
||||||
|
<p>And failures are just as simple (where ‘msg’ is a required parameter to explain the error):</p>
|
||||||
|
<div class="highlight-python"><div class="highlight"><pre><span class="n">module</span><span class="o">.</span><span class="n">exit_json</span><span class="p">(</span><span class="n">msg</span><span class="o">=</span><span class="s">"Something fatal happened"</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
</div>
|
||||||
|
<p>There are also other useful functions in the module class, such as module.md5(path). See
|
||||||
|
lib/ansible/module_common.py in the source checkout for implementation details.</p>
|
||||||
|
<p>Again, modules developed this way are best tested with the hacking/test-module script in the git
|
||||||
|
source checkout. Because of the magic involved, this is really the only way the scripts
|
||||||
|
can function outside of Ansible.</p>
|
||||||
|
<p>If submitting a module to ansible’s core code, which we encourage, use of the AnsibleModule
|
||||||
|
class is required.</p>
|
||||||
|
</div>
|
||||||
|
<div class="section" id="common-pitfalls">
|
||||||
|
<h2>Common Pitfalls<a class="headerlink" href="#common-pitfalls" title="Permalink to this headline">¶</a></h2>
|
||||||
<p>You should also never do this in a module:</p>
|
<p>You should also never do this in a module:</p>
|
||||||
<div class="highlight-python"><div class="highlight"><pre><span class="k">print</span> <span class="s">"some status message"</span>
|
<div class="highlight-python"><div class="highlight"><pre><span class="k">print</span> <span class="s">"some status message"</span>
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
@ -383,17 +421,20 @@ will still be shown in Ansible, but the command will not succeed.</p>
|
||||||
<p>Always use the hacking/test-module script when developing modules and it will warn
|
<p>Always use the hacking/test-module script when developing modules and it will warn
|
||||||
you about these kind of things.</p>
|
you about these kind of things.</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" id="conventions">
|
<div class="section" id="conventions-recomendations">
|
||||||
<h2>Conventions<a class="headerlink" href="#conventions" title="Permalink to this headline">¶</a></h2>
|
<h2>Conventions/Recomendations<a class="headerlink" href="#conventions-recomendations" title="Permalink to this headline">¶</a></h2>
|
||||||
<p>As a reminder from the example code above, here are some basic conventions
|
<p>As a reminder from the example code above, here are some basic conventions
|
||||||
and guidelines:</p>
|
and guidelines:</p>
|
||||||
<ul class="simple">
|
<ul class="simple">
|
||||||
<li>Include a minimum of dependencies if possible. If there are dependencies, document them at the top of the module file.</li>
|
<li>If the module is addressing an object, the parameter for that object should be called ‘name’ whenever possible, or accept ‘name’ as an alias.</li>
|
||||||
<li>Modules must be self contained in one file to be auto-transferred by ansible</li>
|
<li>If you have a company module that returns facts specific to your installations, a good name for this module is <cite>site_facts</cite>.</li>
|
||||||
<li>If packaging modules in an RPM, they only need to be installed on the control machine and should be dropped into /usr/share/ansible. This is entirely optional.</li>
|
<li>Modules accepting boolean status should generally accept ‘yes’, ‘no’, ‘true’, ‘false’, or anything else a user may likely throw at them. The AnsibleModule common code supports this with “choices=BOOLEANS” and a module.boolean(value) casting function.</li>
|
||||||
<li>Modules should return JSON or key=value results all on one line. JSON is best if you can do JSON. All return types must be hashes (dictionaries) although they can be nested.</li>
|
<li>Include a minimum of dependencies if possible. If there are dependencies, document them at the top of the module file, and have the module raise JSON error messages when the import fails.</li>
|
||||||
<li>In the event of failure, a key of ‘failed’ should be included, along with a string explanation in ‘msg’. Modules that raise tracebacks (stacktraces) are generally considered ‘poor’ modules, though Ansible can deal with these returns and will automatically convert anything unparseable into a failed result.</li>
|
<li>Modules must be self contained in one file to be auto-transferred by ansible.</li>
|
||||||
<li>Return codes are actually not signficant, but continue on with 0=success and non-zero=failure for reasons of future proofing.</li>
|
<li>If packaging modules in an RPM, they only need to be installed on the control machine and should be dropped into /usr/share/ansible. This is entirely optional and up to you.</li>
|
||||||
|
<li>Modules should return JSON or key=value results all on one line. JSON is best if you can do JSON. All return types must be hashes (dictionaries) although they can be nested. Lists or simple scalar values are not supported, though they can be trivially contained inside a dictionary.</li>
|
||||||
|
<li>In the event of failure, a key of ‘failed’ should be included, along with a string explanation in ‘msg’. Modules that raise tracebacks (stacktraces) are generally considered ‘poor’ modules, though Ansible can deal with these returns and will automatically convert anything unparseable into a failed result. If you are using the AnsibleModule common Python code, the ‘failed’ element will be included for you automatically when you call ‘fail_json’.</li>
|
||||||
|
<li>Return codes from modules are not actually not signficant, but continue on with 0=success and non-zero=failure for reasons of future proofing.</li>
|
||||||
<li>As results from many hosts will be aggregrated at once, modules should return only relevant output. Returning the entire contents of a log file is generally bad form.</li>
|
<li>As results from many hosts will be aggregrated at once, modules should return only relevant output. Returning the entire contents of a log file is generally bad form.</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
@ -410,20 +451,20 @@ JSON is probably the simplest way to go.</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" id="sharing-your-module">
|
<div class="section" id="sharing-your-module">
|
||||||
<h2>Sharing Your Module<a class="headerlink" href="#sharing-your-module" title="Permalink to this headline">¶</a></h2>
|
<h2>Sharing Your Module<a class="headerlink" href="#sharing-your-module" title="Permalink to this headline">¶</a></h2>
|
||||||
<p>If you think your module is generally useful to others, Ansible is preparing
|
<p>If you think your module is generally useful to others, a good place to share it
|
||||||
an ‘ansible-contrib’ repo. Stop by the mailing list and we’ll help you to
|
is in <cite>Ansible Resources <https://github.com/ansible/ansible-resources></cite>. This is maintained
|
||||||
get your module included. Contrib modules can be implemented in a variety
|
as a simple repo with pointers to other github projects.</p>
|
||||||
of languages. Including a README with your module is a good idea so folks
|
<p>Contrib modules here can be implemented in a variety of languages.
|
||||||
can understand what arguments it takes and so on. We would like to build
|
We would like to build up as many of these as possible in as many languages as possible.</p>
|
||||||
up as many of these as possible in as many languages as possible.</p>
|
|
||||||
<p><a class="reference external" href="http://groups.google.com/group/ansible-project">Ansible Mailing List</a></p>
|
<p><a class="reference external" href="http://groups.google.com/group/ansible-project">Ansible Mailing List</a></p>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" id="getting-your-module-into-core">
|
<div class="section" id="getting-your-module-into-core">
|
||||||
<h2>Getting Your Module Into Core<a class="headerlink" href="#getting-your-module-into-core" title="Permalink to this headline">¶</a></h2>
|
<h2>Getting Your Module Into Core<a class="headerlink" href="#getting-your-module-into-core" title="Permalink to this headline">¶</a></h2>
|
||||||
<p>High-quality modules with minimal dependencies
|
<p>High-quality modules with minimal dependencies
|
||||||
can be included in the core, but core modules (just due to the programming
|
can be included in the core, but core modules (just due to the programming
|
||||||
preferences of the developers) will need to be implemented in Python.
|
preferences of the developers) will need to be implemented in Python and use
|
||||||
Stop by the mailing list to inquire about requirements.</p>
|
the AnsibleModule common code, and should generally use consistent arguments with the rest of
|
||||||
|
the program. Stop by the mailing list to inquire about requirements.</p>
|
||||||
<div class="admonition-see-also admonition seealso">
|
<div class="admonition-see-also admonition seealso">
|
||||||
<p class="first admonition-title">See also</p>
|
<p class="first admonition-title">See also</p>
|
||||||
<dl class="last docutils">
|
<dl class="last docutils">
|
||||||
|
|
|
@ -9,7 +9,6 @@ by `ANSIBLE_LIBRARY_PATH` or the ``--module-path`` command line option.
|
||||||
|
|
||||||
Tutorial
|
Tutorial
|
||||||
````````
|
````````
|
||||||
|
|
||||||
Let's build a module to get and set the system time. For starters, let's build
|
Let's build a module to get and set the system time. For starters, let's build
|
||||||
a module that just outputs the current time.
|
a module that just outputs the current time.
|
||||||
|
|
||||||
|
@ -17,6 +16,11 @@ We are going to use Python here but any language is possible. Only File I/O and
|
||||||
out are required. So, bash, C++, clojure, Python, Ruby, whatever you want
|
out are required. So, bash, C++, clojure, Python, Ruby, whatever you want
|
||||||
is fine.
|
is fine.
|
||||||
|
|
||||||
|
Now Python Ansible modules contain some extremely powerful shortcuts (that all the core modules use)
|
||||||
|
but first we are going to build a module the very hard way. The reason we do this is because modules
|
||||||
|
written in any language OTHER than Python are going to have to do exactly this. We'll show the easy
|
||||||
|
way later.
|
||||||
|
|
||||||
So, here's an example. You would never really need to build a module to set the system time,
|
So, here's an example. You would never really need to build a module to set the system time,
|
||||||
the 'command' module could already be used to do this. Though we're going to make one.
|
the 'command' module could already be used to do this. Though we're going to make one.
|
||||||
|
|
||||||
|
@ -47,7 +51,7 @@ There's a useful test script in the source checkout for ansible::
|
||||||
|
|
||||||
Let's run the script you just wrote with that::
|
Let's run the script you just wrote with that::
|
||||||
|
|
||||||
ansible/hacking/test-module ./time
|
ansible/hacking/test-module -m ./time
|
||||||
|
|
||||||
You should see output that looks something like this::
|
You should see output that looks something like this::
|
||||||
|
|
||||||
|
@ -168,7 +172,6 @@ This should return something like::
|
||||||
|
|
||||||
{"changed": True, "time": "2012-03-14 12:23:00.000307"}
|
{"changed": True, "time": "2012-03-14 12:23:00.000307"}
|
||||||
|
|
||||||
|
|
||||||
Module Provided 'Facts'
|
Module Provided 'Facts'
|
||||||
```````````````````````
|
```````````````````````
|
||||||
|
|
||||||
|
@ -192,18 +195,58 @@ These 'facts' will be available to all statements called after that module (but
|
||||||
A good idea might be make a module called 'site_facts' and always call it at the top of each playbook, though
|
A good idea might be make a module called 'site_facts' and always call it at the top of each playbook, though
|
||||||
we're always open to improving the selection of core facts in Ansible as well.
|
we're always open to improving the selection of core facts in Ansible as well.
|
||||||
|
|
||||||
|
Common Module Boilerplate
|
||||||
|
`````````````````````````
|
||||||
|
|
||||||
|
As mentioned, if you are writing a module in Python, there are some very powerful shortcuts you can use.
|
||||||
|
Modules are still transferred as one file, but an arguments file is no longer needed, so these are not
|
||||||
|
only shorter in terms of code, they are actually FASTER in terms of execution time.
|
||||||
|
|
||||||
|
Rather than mention these here, the best way to learn is to read some of the `source of the modules <https://github.com/ansible/ansible/tree/devel/library>`_ that come with Ansible.
|
||||||
|
|
||||||
|
The 'group' and 'user' modules are reasonably non-trival and showcase what this looks like.
|
||||||
|
|
||||||
|
Key parts include always ending the module file with::
|
||||||
|
|
||||||
|
# include magic from lib/ansible/module_common.py
|
||||||
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||||
|
main()
|
||||||
|
|
||||||
|
And instantiating the module class like::
|
||||||
|
|
||||||
|
module = AnsibleModule(
|
||||||
|
argument_spec = dict(
|
||||||
|
state = dict(default='present', choices=['present', 'absent']),
|
||||||
|
name = dict(required=True),
|
||||||
|
enabled = dict(required=True, choices=BOOLEANS),
|
||||||
|
something = dict(aliases=['whatever'])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
The AnsibleModule provides lots of common code for handling returns, parses your arguments
|
||||||
|
for you, and allows you to check inputs.
|
||||||
|
|
||||||
|
Successful returns are made like this::
|
||||||
|
|
||||||
|
module.exit_json(changed=True, something_else=12345)
|
||||||
|
|
||||||
|
And failures are just as simple (where 'msg' is a required parameter to explain the error)::
|
||||||
|
|
||||||
|
module.exit_json(msg="Something fatal happened")
|
||||||
|
|
||||||
|
There are also other useful functions in the module class, such as module.md5(path). See
|
||||||
|
lib/ansible/module_common.py in the source checkout for implementation details.
|
||||||
|
|
||||||
|
Again, modules developed this way are best tested with the hacking/test-module script in the git
|
||||||
|
source checkout. Because of the magic involved, this is really the only way the scripts
|
||||||
|
can function outside of Ansible.
|
||||||
|
|
||||||
|
If submitting a module to ansible's core code, which we encourage, use of the AnsibleModule
|
||||||
|
class is required.
|
||||||
|
|
||||||
Common Pitfalls
|
Common Pitfalls
|
||||||
```````````````
|
```````````````
|
||||||
|
|
||||||
If writing a module in Python and you have managed nodes running
|
|
||||||
Python 2.4 or lower, this is generally a good idea, because
|
|
||||||
json isn't in the Python standard library until 2.5.::
|
|
||||||
|
|
||||||
try:
|
|
||||||
import json
|
|
||||||
except ImportError:
|
|
||||||
import simplejson as json
|
|
||||||
|
|
||||||
You should also never do this in a module::
|
You should also never do this in a module::
|
||||||
|
|
||||||
print "some status message"
|
print "some status message"
|
||||||
|
@ -222,27 +265,32 @@ will still be shown in Ansible, but the command will not succeed.
|
||||||
Always use the hacking/test-module script when developing modules and it will warn
|
Always use the hacking/test-module script when developing modules and it will warn
|
||||||
you about these kind of things.
|
you about these kind of things.
|
||||||
|
|
||||||
Conventions
|
Conventions/Recomendations
|
||||||
```````````
|
``````````````````````````
|
||||||
|
|
||||||
As a reminder from the example code above, here are some basic conventions
|
As a reminder from the example code above, here are some basic conventions
|
||||||
and guidelines:
|
and guidelines:
|
||||||
|
|
||||||
* Include a minimum of dependencies if possible. If there are dependencies, document them at the top of the module file.
|
* If the module is addressing an object, the parameter for that object should be called 'name' whenever possible, or accept 'name' as an alias.
|
||||||
|
|
||||||
* Modules must be self contained in one file to be auto-transferred by ansible
|
* If you have a company module that returns facts specific to your installations, a good name for this module is `site_facts`.
|
||||||
|
|
||||||
* If packaging modules in an RPM, they only need to be installed on the control machine and should be dropped into /usr/share/ansible. This is entirely optional.
|
* Modules accepting boolean status should generally accept 'yes', 'no', 'true', 'false', or anything else a user may likely throw at them. The AnsibleModule common code supports this with "choices=BOOLEANS" and a module.boolean(value) casting function.
|
||||||
|
|
||||||
* Modules should return JSON or key=value results all on one line. JSON is best if you can do JSON. All return types must be hashes (dictionaries) although they can be nested.
|
* Include a minimum of dependencies if possible. If there are dependencies, document them at the top of the module file, and have the module raise JSON error messages when the import fails.
|
||||||
|
|
||||||
* In the event of failure, a key of 'failed' should be included, along with a string explanation in 'msg'. Modules that raise tracebacks (stacktraces) are generally considered 'poor' modules, though Ansible can deal with these returns and will automatically convert anything unparseable into a failed result.
|
* Modules must be self contained in one file to be auto-transferred by ansible.
|
||||||
|
|
||||||
* Return codes are actually not signficant, but continue on with 0=success and non-zero=failure for reasons of future proofing.
|
* If packaging modules in an RPM, they only need to be installed on the control machine and should be dropped into /usr/share/ansible. This is entirely optional and up to you.
|
||||||
|
|
||||||
|
* Modules should return JSON or key=value results all on one line. JSON is best if you can do JSON. All return types must be hashes (dictionaries) although they can be nested. Lists or simple scalar values are not supported, though they can be trivially contained inside a dictionary.
|
||||||
|
|
||||||
|
* In the event of failure, a key of 'failed' should be included, along with a string explanation in 'msg'. Modules that raise tracebacks (stacktraces) are generally considered 'poor' modules, though Ansible can deal with these returns and will automatically convert anything unparseable into a failed result. If you are using the AnsibleModule common Python code, the 'failed' element will be included for you automatically when you call 'fail_json'.
|
||||||
|
|
||||||
|
* Return codes from modules are not actually not signficant, but continue on with 0=success and non-zero=failure for reasons of future proofing.
|
||||||
|
|
||||||
* As results from many hosts will be aggregrated at once, modules should return only relevant output. Returning the entire contents of a log file is generally bad form.
|
* As results from many hosts will be aggregrated at once, modules should return only relevant output. Returning the entire contents of a log file is generally bad form.
|
||||||
|
|
||||||
|
|
||||||
Shorthand Vs JSON
|
Shorthand Vs JSON
|
||||||
`````````````````
|
`````````````````
|
||||||
|
|
||||||
|
@ -260,12 +308,12 @@ JSON is probably the simplest way to go.
|
||||||
Sharing Your Module
|
Sharing Your Module
|
||||||
```````````````````
|
```````````````````
|
||||||
|
|
||||||
If you think your module is generally useful to others, Ansible is preparing
|
If you think your module is generally useful to others, a good place to share it
|
||||||
an 'ansible-contrib' repo. Stop by the mailing list and we'll help you to
|
is in `Ansible Resources <https://github.com/ansible/ansible-resources>`. This is maintained
|
||||||
get your module included. Contrib modules can be implemented in a variety
|
as a simple repo with pointers to other github projects.
|
||||||
of languages. Including a README with your module is a good idea so folks
|
|
||||||
can understand what arguments it takes and so on. We would like to build
|
Contrib modules here can be implemented in a variety of languages.
|
||||||
up as many of these as possible in as many languages as possible.
|
We would like to build up as many of these as possible in as many languages as possible.
|
||||||
|
|
||||||
`Ansible Mailing List <http://groups.google.com/group/ansible-project>`_
|
`Ansible Mailing List <http://groups.google.com/group/ansible-project>`_
|
||||||
|
|
||||||
|
@ -274,8 +322,9 @@ Getting Your Module Into Core
|
||||||
|
|
||||||
High-quality modules with minimal dependencies
|
High-quality modules with minimal dependencies
|
||||||
can be included in the core, but core modules (just due to the programming
|
can be included in the core, but core modules (just due to the programming
|
||||||
preferences of the developers) will need to be implemented in Python.
|
preferences of the developers) will need to be implemented in Python and use
|
||||||
Stop by the mailing list to inquire about requirements.
|
the AnsibleModule common code, and should generally use consistent arguments with the rest of
|
||||||
|
the program. Stop by the mailing list to inquire about requirements.
|
||||||
|
|
||||||
.. seealso::
|
.. seealso::
|
||||||
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue