Ansible Modules

Learn how to perform different actions using Ansible modules.

What are Ansible modules?

Ansible modules are reusable components designed for specific actions such as managing packages, starting or stopping services, managing users or groups, executing commands, configuring files, and so on. Ansible modules can be categorized into the following two categories:

  • Core modules: Modules that are created and managed by Ansible and available for use by anyone who runs Ansible. An example of this is the apt module.
  • Custom modules: Modules that are created and managed by users or organizations to extend the functionality of Ansible Core modules and to meet their own specific needs.
Press + to interact
Ansible modules
Ansible modules

Why we need Ansible modules

Without Ansible modules, it’ll be difficult to write tasks for Ansible plays or playbooks. Here are some of the benefits of Ansible modules:

  • Automation: Ansible modules are one of the primary ingredients for automation via Ansible. With modules, we can define specific tasks and actions to be executed on managed nodes, reducing manual intervention as well as repetition.
  • Consistency: Ansible modules help us create a uniform configuration that we can use across multiple systems.
  • Standardization: Modules provide a standardized and structured way of managing system components as well as applications on managed nodes.
  • Idempotence: Modules empower us to run tasks without causing unintended changes or disruptions on target systems.

We can interact with a module by supplying the following components:

  • Module name: This is the name of the specific module that we would like to use to perform a task. For example, we can specify the user module if we wish to manage user accounts on a managed node.
  • Parameters: The module name will determine the set of parameters that we have to supply. For instance, if we want to use the user module to manage user accounts, we’ll have to specify parameters like name, state, groups, uid, shell, and password.
  • Variable substitution: Ansible modules allow us to substitute variables, making it possible for modules to pick up dynamic values. As an example, we can create a variable named new_user with a corresponding value of devopsguy and then pass this to the name parameter of our user module. This ensures that we can create a user named devopsguy whose value can be changed as needed, thereby making our configuration very flexible.
  • Conditionals: We can use conditionals to tailor our module execution to a particular condition. An example is when we want to ensure that the user creation only happens if the group name is a specific value, as shown below. The following code snippet will create
...