You are currently viewing Modules In Ansible

Modules In Ansible

Modules in Ansible

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed encryption in Ansible.

https://cloudaffaire.com/encryption-in-ansible/

In this blog post, we will discuss modules in Ansible.

Ansible Modules:

Modules are discrete units of code that can be used from the command line or in a playbook task. Modules take some arguments, generally, in the form of key=value, perform some actions and return some result.

Whatever actions you perform in your play or command are defined in modules. Most of the core, community-supported modules and other modules essential for normal day to day usage are included in your ansible binary and available to use once you install Ansible in your system. You can also create your own custom module.

All modules technically return JSON format data, though if you are using the command line or playbooks, you don’t really need to know much about that. Ansible modules normally return a data structure that can be registered into a variable, or seen directly when output by the ansible program.

Module Return Values:

External use: Used by user

  • backup_file: For those modules that implement backup=no|yes when manipulating files, a path to the backup file created.
  • changed: A boolean indicating if the task had to make changes.
  • failed: A boolean that indicates if the task was failed or not.
  • invocation: Information on how the module was invoked.
  • msg: A string with a generic message relayed to the user.
  • rc: Some modules execute command-line utilities or are geared for executing commands directly (raw, shell, command, etc), this field contains ‘return code’ of these utilities.
  • results: If this key exists, it indicates that a loop was present for the task and that it contains a list of the normal module ‘result’ per item.
  • skipped: A boolean that indicates if the task was skipped or not
  • stderr: Some modules execute command-line utilities or are geared for executing commands directly (raw, shell, command, etc), this field contains the error output of these utilities.
  • stderr_lines: When stderr is returned we also always provide this field which is a list of strings, one item per line from the original.
  • stdout: Some modules execute command-line utilities or are geared for executing commands directly (raw, shell, command, etc). This field contains the normal output of these utilities.
  • stdout_lines: When stdout is returned, Ansible always provides a list of strings, each containing one item per line from the original output.

Internal use: Used by Ansible

  • ansible_facts: This key should contain a dictionary which will be appended to the facts assigned to the host. These will be directly accessible and don’t require using a registered variable.
  • exception: This key can contain traceback information caused by an exception in a module. It will only be displayed on high verbosity (-vvv).
  • warnings: This key contains a list of strings that will be presented to the user.
  • deprecations: This key contains a list of dictionaries that will be presented to the user. Keys of the dictionaries are msg and version, values are string, value for the version key can be an empty string.

Ansible Module Demo:

List all available modules in your system

Get module documentation

Execute a module through ansible command

See module execution under the hood

Execute a module through ansible playbook

Task: Observe the return values from the output and corelate them with module documentation.

Ansible provides a ton of modules and you and reference them using below links. Ansible module documentation is full of example and reference for ease of use.

Modules official documentation:

https://docs.ansible.com/ansible/latest/modules/modules_by_category.html

Modules source code:

https://github.com/ansible/ansible/tree/devel/lib/ansible/modules

Shell module (used in this demo) documentation:

https://docs.ansible.com/ansible/latest/modules/shell_module.html

https://github.com/ansible/ansible-modules-core/blob/devel/commands/shell.py

Hope you have enjoyed this article. In the next blog post, we will discuss roles in Ansible.

To get more details on Ansible, please refer below Ansible documentation.

https://docs.ansible.com/

 

Leave a Reply