Welcome to the Ansible Developer Guide!
Who should use this guide?
This development board is based on the MT3620 module, which greatly simplifies the difficulty of hardware design. The MT3620 module is developed by our partner AI-Link. Developers can easily reuse this design into their own projects. The development board uses two single-row pin headers for easy plug-in on other motherboards or expansion boards. C Language Tutorial (PDF 124P) This note covers the following topics: getting started, getting started in c, program control, assignment logical compares, functions and variables, defines and macros, strings and arrays, pointers, standard inputoutput, file inputoutput, structures and unions, dynamic allocation, character and bit manipulation.
If you want to extend Ansible by using a custom module or plugin locally, creating a module or plugin, adding functionality to an existing module, or expanding test coverage, this guide is for you. We’ve included detailed information for developers on how to test and document modules, as well as the prerequisites for getting your module or plugin accepted into the main Ansible repository.
Find the task that best describes what you want to do:
I’m looking for a way to address a use case:
I want to add a custom plugin or module locally.
I want to figure out if developing a module is the right approach for my use case.
I want to develop a collection.
I want to contribute to an Ansible-maintained collection.
I want to contribute to a community-maintained collection.
I want to migrate a role to a collection.
I’ve read the info above, and I’m sure I want to develop a module:
What do I need to know before I start coding?
I want to set up my Python development environment.
I want to get started writing a module.
- I want to write a specific kind of module:
a network module
a Windows module.
an Amazon module.
an OpenStack module.
an oVirt/RHV module.
a VMware module.
I want to write a series of related modules that integrate Ansible with a new product (for example, a database, cloud provider, network platform, and so on).
I want to refine my code:
I want to debug my module code.
I want to add tests.
I want to document my module.
I want to document my set of modules for a network platform.
I want to follow conventions and tips for clean, usable module code.
I want to make sure my code runs on Python 2 and Python 3.
I want to work on other development projects:
I want to write a plugin.
I want to connect Ansible to a new source of inventory.
I want to deprecate an outdated module.
I want to contribute back to the Ansible project:
I want to understand how to contribute to Ansible.
I want to contribute my module or plugin.
I want to understand the license agreement for contributions to Ansible.
If you prefer to read the entire guide, here’s a list of the pages in order.
- Adding modules and plugins locally
- Ansible module development: getting started
- Contributing your module to an existing Ansible collection
- Conventions, tips, and pitfalls
- Ansible and Python 3
- Debugging modules
- Module format and documentation
- Windows module development walkthrough
- Developing Cisco ACI modules
- Guidelines for Ansible Amazon AWS module development
- OpenStack Ansible Modules
- oVirt Ansible Modules
- Guidelines for VMware module development
- Creating a new collection
- Testing Ansible
- The lifecycle of an Ansible module
- Developing plugins
- Developing dynamic inventory
- Ansible module architecture
- Python API
- Rebasing a pull request
- Using and developing module utilities
- Developing collections
- Migrating Roles to Roles in Collections on Galaxy
- Collection Galaxy metadata structure
- Migrating Roles to Roles in Collections on Galaxy
- Ansible architecture
If you quit from the Python interpreter and enter it again, the definitions youhave made (functions and variables) are lost. Therefore, if you want to write asomewhat longer program, you are better off using a text editor to prepare theinput for the interpreter and running it with that file as input instead. Thisis known as creating a script. As your program gets longer, you may want tosplit it into several files for easier maintenance. You may also want to use ahandy function that you’ve written in several programs without copying itsdefinition into each program.
To support this, Python has a way to put definitions in a file and use them in ascript or in an interactive instance of the interpreter. Such a file is called amodule; definitions from a module can be imported into other modules or intothe main module (the collection of variables that you have access to in ascript executed at the top level and in calculator mode).
A module is a file containing Python definitions and statements. The file nameis the module name with the suffix
.py appended. Within a module, themodule’s name (as a string) is available as the value of the global variable
__name__. For instance, use your favorite text editor to create a filecalled
fibo.py in the current directory with the following contents:
Now enter the Python interpreter and import this module with the followingcommand:
This does not enter the names of the functions defined in
fibo directly inthe current symbol table; it only enters the module name
fibo there. Usingthe module name you can access the functions:
If you intend to use a function often you can assign it to a local name:
6.1. More on Modules¶
A module can contain executable statements as well as function definitions.These statements are intended to initialize the module. They are executed onlythe first time the module name is encountered in an import statement. 1(They are also run if the file is executed as a script.)
Each module has its own private symbol table, which is used as the global symboltable by all functions defined in the module. Thus, the author of a module canuse global variables in the module without worrying about accidental clasheswith a user’s global variables. On the other hand, if you know what you aredoing you can touch a module’s global variables with the same notation used torefer to its functions,
Modules can import other modules. It is customary but not required to place all
import statements at the beginning of a module (or script, for thatmatter). The imported module names are placed in the importing module’s globalsymbol table.
There is a variant of the
import statement that imports names from amodule directly into the importing module’s symbol table. For example:
This does not introduce the module name from which the imports are taken in thelocal symbol table (so in the example,
fibo is not defined).
There is even a variant to import all names that a module defines:
This imports all names except those beginning with an underscore (
_).In most cases Python programmers do not use this facility since it introducesan unknown set of names into the interpreter, possibly hiding some thingsyou have already defined.
Note that in general the practice of importing
* from a module or package isfrowned upon, since it often causes poorly readable code. However, it is okay touse it to save typing in interactive sessions.
If the module name is followed by
as, then the namefollowing
as is bound directly to the imported module.
This is effectively importing the module in the same way that
importfibowill do, with the only difference of it being available as
It can also be used when utilising
from with similar effects:
For efficiency reasons, each module is only imported once per interpretersession. Therefore, if you change your modules, you must restart theinterpreter – or, if it’s just one module you want to test interactively,use
6.1.1. Executing modules as scripts¶
When you run a Python module with
the code in the module will be executed, just as if you imported it, but withthe
__name__ set to
'__main__'. That means that by adding this code atthe end of your module:
you can make the file usable as a script as well as an importable module,because the code that parses the command line only runs if the module isexecuted as the “main” file:
If the module is imported, the code is not run:
This is often used either to provide a convenient user interface to a module, orfor testing purposes (running the module as a script executes a test suite).
6.1.2. The Module Search Path¶
When a module named
spam is imported, the interpreter first searches fora built-in module with that name. If not found, it then searches for a filenamed
spam.py in a list of directories given by the variable
sys.path is initialized from these locations:
The directory containing the input script (or the current directory when nofile is specified).
PYTHONPATH(a list of directory names, with the same syntax as theshell variable
The installation-dependent default.
On file systems which support symlinks, the directory containing the inputscript is calculated after the symlink is followed. In other words thedirectory containing the symlink is not added to the module search path.
After initialization, Python programs can modify
sys.path. Thedirectory containing the script being run is placed at the beginning of thesearch path, ahead of the standard library path. This means that scripts in thatdirectory will be loaded instead of modules of the same name in the librarydirectory. This is an error unless the replacement is intended. See sectionStandard Modules for more information.
6.1.3. “Compiled” Python files¶
To speed up loading modules, Python caches the compiled version of each modulein the
__pycache__ directory under the name
module.version.pyc,where the version encodes the format of the compiled file; it generally containsthe Python version number. For example, in CPython release 3.3 the compiledversion of spam.py would be cached as
__pycache__/spam.cpython-33.pyc. Thisnaming convention allows compiled modules from different releases and differentversions of Python to coexist.
Python checks the modification date of the source against the compiled versionto see if it’s out of date and needs to be recompiled. This is a completelyautomatic process. Also, the compiled modules are platform-independent, so thesame library can be shared among systems with different architectures.
Python does not check the cache in two circumstances. First, it alwaysrecompiles and does not store the result for the module that’s loaded directlyfrom the command line. Second, it does not check the cache if there is nosource module. To support a non-source (compiled only) distribution, thecompiled module must be in the source directory, and there must not be a sourcemodule.
Some tips for experts:
You can use the
-OOswitches on the Python commandto reduce the size of a compiled module. The
-Oswitch removes assertstatements, the
-OOswitch removes both assert statements and __doc__strings. Since some programs may rely on having these available, you shouldonly use this option if you know what you’re doing. “Optimized” modules havean
opt-tag and are usually smaller. Future releases maychange the effects of optimization.
A program doesn’t run any faster when it is read from a
.pycfile than when it is read from a
.pyfile; the only thing that’s fasterabout
.pycfiles is the speed with which they are loaded.
compileallcan create .pyc files for all modules in adirectory.
There is more detail on this process, including a flow chart of thedecisions, in PEP 3147.
6.2. Standard Modules¶
Python comes with a library of standard modules, described in a separatedocument, the Python Library Reference (“Library Reference” hereafter). Somemodules are built into the interpreter; these provide access to operations thatare not part of the core of the language but are nevertheless built in, eitherfor efficiency or to provide access to operating system primitives such assystem calls. The set of such modules is a configuration option which alsodepends on the underlying platform. For example, the
winreg module is onlyprovided on Windows systems. One particular module deserves some attention:
sys, which is built into every Python interpreter. The variables
sys.ps2 define the strings used as primary and secondaryprompts:
These two variables are only defined if the interpreter is in interactive mode.
sys.path is a list of strings that determines the interpreter’ssearch path for modules. It is initialized to a default path taken from theenvironment variable
PYTHONPATH, or from a built-in default if
PYTHONPATH is not set. You can modify it using standard listoperations:
The built-in function
dir() is used to find out which names a moduledefines. It returns a sorted list of strings:
dir() lists the names you have defined currently:
Note that it lists all types of names: variables, modules, functions, etc.
dir() does not list the names of built-in functions and variables. If youwant a list of those, they are defined in the standard module
Packages are a way of structuring Python’s module namespace by using “dottedmodule names”. For example, the module name
A.B designates a submodulenamed
B in a package named
A. Just like the use of modules saves theauthors of different modules from having to worry about each other’s globalvariable names, the use of dotted module names saves the authors of multi-modulepackages like NumPy or Pillow from having to worry abouteach other’s module names.
Suppose you want to design a collection of modules (a “package”) for the uniformhandling of sound files and sound data. There are many different sound fileformats (usually recognized by their extension, for example:
.au), so you may need to create and maintain a growingcollection of modules for the conversion between the various file formats.There are also many different operations you might want to perform on sound data(such as mixing, adding echo, applying an equalizer function, creating anartificial stereo effect), so in addition you will be writing a never-endingstream of modules to perform these operations. Here’s a possible structure foryour package (expressed in terms of a hierarchical filesystem):
When importing the package, Python searches through the directories on
sys.path looking for the package subdirectory.
__init__.py files are required to make Python treat directoriescontaining the file as packages. This prevents directories with a common name,such as
string, unintentionally hiding valid modules that occur lateron the module search path. In the simplest case,
__init__.py can just bean empty file, but it can also execute initialization code for the package orset the
__all__ variable, described later.
Users of the package can import individual modules from the package, forexample:
This loads the submodule
sound.effects.echo. It must be referenced withits full name.
An alternative way of importing the submodule is:
This also loads the submodule
echo, and makes it available without itspackage prefix, so it can be used as follows:
Yet another variation is to import the desired function or variable directly:
Again, this loads the submodule
echo, but this makes its function
echofilter() directly available:
Note that when using
frompackageimportitem, the item can be either asubmodule (or subpackage) of the package, or some other name defined in thepackage, like a function, class or variable. The
import statement firsttests whether the item is defined in the package; if not, it assumes it is amodule and attempts to load it. If it fails to find it, an
ImportErrorexception is raised.
Contrarily, when using syntax like
importitem.subitem.subsubitem, each itemexcept for the last must be a package; the last item can be a module or apackage but can’t be a class or function or variable defined in the previousitem.
6.4.1. Importing * From a Package¶
Now what happens when the user writes
fromsound.effectsimport*? Ideally,one would hope that this somehow goes out to the filesystem, finds whichsubmodules are present in the package, and imports them all. This could take along time and importing sub-modules might have unwanted side-effects that shouldonly happen when the sub-module is explicitly imported.
The only solution is for the package author to provide an explicit index of thepackage. The
import statement uses the following convention: if a package’s
__init__.py code defines a list named
__all__, it is taken to be thelist of module names that should be imported when
frompackageimport* isencountered. It is up to the package author to keep this list up-to-date when anew version of the package is released. Package authors may also decide not tosupport it, if they don’t see a use for importing * from their package. Forexample, the file
sound/effects/__init__.py could contain the followingcode:
This would mean that
fromsound.effectsimport* would import the threenamed submodules of the
__all__ is not defined, the statement
fromsound.effectsimport*does not import all submodules from the package
sound.effects into thecurrent namespace; it only ensures that the package
sound.effects hasbeen imported (possibly running any initialization code in
__init__.py)and then imports whatever names are defined in the package. This includes anynames defined (and submodules explicitly loaded) by
__init__.py. Italso includes any submodules of the package that were explicitly loaded byprevious
import statements. Consider this code:
In this example, the
surround modules are imported in thecurrent namespace because they are defined in the
sound.effects packagewhen the
from...import statement is executed. (This also works when
__all__ is defined.)
Although certain modules are designed to export only names that follow certainpatterns when you use
import*, it is still considered bad practice inproduction code.
Remember, there is nothing wrong with using
frompackageimportspecific_submodule! In fact, this is the recommended notation unless theimporting module needs to use submodules with the same name from differentpackages.
6.4.2. Intra-package References¶
When packages are structured into subpackages (as with the
sound packagein the example), you can use absolute imports to refer to submodules of siblingspackages. For example, if the module
sound.filters.vocoder needs to usethe
echo module in the
sound.effects package, it can use
You can also write relative imports, with the
frommoduleimportname formof import statement. These imports use leading dots to indicate the current andparent packages involved in the relative import. From the
surroundmodule for example, you might use:
Modul Dev C 2b 2b Pdf Free
Note that relative imports are based on the name of the current module. Sincethe name of the main module is always
'__main__', modules intended for useas the main module of a Python application must always use absolute imports.
6.4.3. Packages in Multiple Directories¶
Packages support one more special attribute,
__path__. This isinitialized to be a list containing the name of the directory holding thepackage’s
__init__.py before the code in that file is executed. Thisvariable can be modified; doing so affects future searches for modules andsubpackages contained in the package.
While this feature is not often needed, it can be used to extend the set ofmodules found in a package.
Modul Dev C 2b 2b Pdf File
In fact function definitions are also ‘statements’ that are ‘executed’; theexecution of a module-level function definition enters the function name inthe module’s global symbol table.