* [Topics](/en/topics "Topics")
* [Automation and management](/en/topics/automation "Automation and management")
* What is an Ansible module—and how does it work?
What is an Ansible module—and how does it work?
===============================================
Published  April 30, 2024•*5*-minute read
Copy URL
Jump to section
---------------
OverviewHow do modules work?Creating and sharing Ansible modulesEvent-driven automationWhy Red Hat?
Overview
--------
An Ansible® module is a small program that performs actions on a local machine, application programming interface (API), or remote host. Modules are expressed as code, usually in Python, and contain metadata that defines when and where a specific automation task is executed and which users can execute it. There are thousands of Ansible modules available for download in repositories like [Ansible automation hub](/en/technologies/management/ansible/automation-hub) and [Ansible Galaxy](https://www.ansible.com/community/galaxy), but you can also create custom modules yourself and share them with other Ansible users within your organization or in the larger Ansible community.
How do modules work?
--------------------
Modules can perform IT functions for a variety of use cases including networking, provisioning, security, cloud management, user management, configuration management, and communications. To understand how Ansible modules help you automate these use cases and more, it’s important to understand the relationship between modules and other key components of [Ansible automation](/en/technologies/management/ansible/ansible-vs-red-hat-ansible-automation-platform)—including tasks, plays, playbooks, and roles.
* **Task:** Defines the action to be applied to a managed host, but does not specify the host it will be applied to. You can invoke a module within a task to be executed on a host, but that host will be defined outside of the task, at the play level.
* **Play:** The core unit of Ansible execution, a play includes a list of tasks and maps that list to specific managed hosts. A play also contains variables and, in more complex use cases, may contain roles.
* [**Ansible Playbook**](/en/topics/automation/what-is-an-ansible-playbook)**:** Includes 1 or more plays. Playbooks are written in YAML, are human-readable, and easy to share.
* [**Ansible Role**](/en/topics/automation/what-is-an-ansible-role)**:** Packages Ansible content—including tasks, handlers, variables, plugins, templates, and files—for use inside of a play. Ansible Roles can be imported into a play, reused in other plays or playbooks, and shared for use by other automation users.
* **Collections:** Bundles of Ansible content designed to help automation developers work faster and improve productivity. Collections are the preferred way to package and share modules.
* [**Plugins**](https://docs.ansible.com/ansible/latest/dev_guide/developing_locally.html#modules-and-plugins-what-is-the-difference)**:** Pieces of code that build on Ansible’s core functionality and usually execute on the control node. While modules are a type of plugin, they generally execute tasks on a target, not the control node.
Think of it like this: If tasks are the jobs to be completed, then modules are the tools you need to actually complete those jobs. A task defines an action that needs to be done, and the module executes on a managed host to accomplish that action, collecting return values in JSON format when finished.
Modules are typically stored in a playbook’s library and run when the playbook executes the associated task, but they may also be included within a role or collection. When an Ansible Role is imported into a playbook, the modules in the role directory execute the task or tasks contained within that role. While a module written in a single play will only be used that 1 time, modules contained inside of a role will execute each time the role is called within the playbook.
The following 3 example modules are included with Ansible Core and a part of all Ansible installations:
* **dnf module** ([ansible.builtin.dnf](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/dnf_module.html#ansible-collections-ansible-builtin-dnf-module))- Installs, upgrades, and removes packages and groups with the dnf package manager—the default software package manager for Fedora.
* **service module** ([ansible.builtin.service](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html#ansible-collections-ansible-builtin-service-module))- Manages services on remote hosts. Example actions include, but are not limited to, starting, stopping, and restarting a service.
* **command module** ([ansible.builtin.command](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html#ansible-collections-ansible-builtin-command-module))- Executes commands on targets.
Recommended for you
Automate CyberArk activities with the Ansible Automation Platform’s Event-Driven Ansible
----------------------------------------------------------------------------------------
[Watch the webinar](https://www.redhat.com/en/events/webinar/automate-cyberaark-activities-ansible-automation-platforms-event-driven-ansible?percmp=RHCTG0250000455235)
Creating and sharing Ansible modules
------------------------------------
When existing Ansible modules are insufficient for your automation use case, you can create your own module to accomplish a specific job. Although the process for creating a module may vary depending on the complexity of the desired automation tasks, you can [develop your own module](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html) by:
1. Creating a library directory in your repository.
2. Creating a module file in Python or your preferred programming language within the library directory.
3. Developing a test playbook in your library directory.
4. Executing the playbook to test the module.
Modules should be concise and aim to solve a very specific problem; this makes them easy to test, use, reuse, and share. Modules should also provide a defined interface that accepts arguments and be idempotent—meaning they shouldn’t make any changes if they detect that the current state of the node matches the desired final state.
Users who are just getting started with Ansible content development or those who want to streamline the module creation process can use a community tool like [Content Builder](https://github.com/ansible-community/ansible.content_builder), which generates modules and other Ansible plugins for use cases like networking, security, and cloud content.
Like other types of Ansible content, Ansible modules are designed to be shareable. With a [Red Hat® Ansible Automation Platform](/en/technologies/management/ansible) subscription, you can package your modules in collections and upload them to Ansible automation hub or share them across your enterprise with a private automation hub. You can also distribute your modules on Ansible Galaxy, a free repository for sharing Ansible content with community users.
[Learn how to create Ansible modules with Content Builder](https://www.ansible.com/blog/streamline-your-ansible-module-development-with-content-builder)
Using modules with event-driven automation
------------------------------------------
While modules play an essential role in playbook execution, they also hold value for organizations looking to take a more advanced, proactive approach to IT management with event-driven automation.
Event-driven automation connects data from IT events to automated actions, reducing the need for teams to take manual steps to respond or remediate. An event can be any occurrence that impacts the delivery of a service or the management of IT infrastructure.
A feature of Red Hat Ansible Automation Platform and a comprehensive solution for automation teams looking to benefit from event-driven automation, [Event-Driven Ansible](/en/technologies/management/ansible/event-driven-ansible) uses [Ansible Rulebooks](/en/topics/automation/what-is-an-ansible-rulebook) to respond to events with the appropriate actions you’ve defined—this includes executing playbooks, roles, templates, and modules. A rulebook uses conditional “if-this-then-that” instructions to help you automate routine tasks—such as running a remediation playbook in the case of an outage.
When data from an event source meets the conditions defined in a rulebook, Ansible Automation Platform will trigger the actions that you’ve defined, which may include running a whole playbook or running a single module to execute a very specific action—the response fully depends on what you want to happen when an event occurs and what you’ve defined in a rulebook.
The example below demonstrates how a debug module is triggered when an event from a webhook source meets the conditions defined in a rulebook.
The **rulebook.yml**:
```
---
- name: Listen for events on a webhook
  hosts: all
  ## Define our source for events
  sources:
    - ansible.eda.webhook:
        host: 0.0.0.0
        port: 5000
  ## Define the conditions we are looking for
  rules:
    - name: Say Hello
      condition: event.payload.message == "Ansible is super cool"
  ## Define the action we should take should the condition be met
      action:
        run_playbook:
          name: say-what.yml
```
The playbook **say-what.yml**:
```
- hosts: localhost
  connection: local
  tasks:
    - debug:
        msg: "Thank you, my friend!"
```
In this case, the rulebook specifies that the say-what.yml playbook executes when the defined conditions have been met. Since the debug module is included in the say-what.yml playbook, it is run each time the playbook is executed.
Why Red Hat?
------------
As a part of Ansible Automation Platform, Event-Driven Ansible offers the event-handling capabilities you need to enable quick and efficient IT service delivery while allowing you to complete tasks without manual intervention—giving your organization time to focus on higher-value projects.
Since event-driven automation often relies on third-party monitoring tools to identify when an event occurs, making use of Ansible content that integrates with the platforms and tools you already use can make the transition to more efficient, automated workflows easier. With a Red Hat Ansible Automation Platform subscription, you gain access to thousands of bundled modules, roles, plugins, and documentation in the form of [Ansible Content Collections](/en/technologies/management/ansible/content-collections). These include both Red Hat Ansible Certified Content and Ansible validated content from Red Hat and our partners—all available for download via Ansible automation hub.
[Learn more about event-driven automation with a self-paced lab](/en/engage/event-driven-ansible-20220907 "SOLP | Get started with event-driven automation")
Recommended for you
E-book
The automation architect's handbook
-----------------------------------
Learn about how to lead your company's end-to-end automation journey.
[Read the e-book](https://www.redhat.com/en/engage/automation-architect-handbook-ebook?percmp=RHCTG0250000455234)
Recommended for you
Developing Advanced Automation with Red Hat Ansible Automation Platform and exam
--------------------------------------------------------------------------------
This course teaches you advanced Ansible skills, helping you design, operate, and scale your automation.
[View course with exam](https://www.redhat.com/en/services/training/developing-advanced-automation-red-hat-ansible-automation-platform-and-exam-au375?percmp=RHCTG0250000455236)
Keep reading
------------
### What is IT service management (ITSM)?
IT service management (ITSM) refers to the activities IT teams perform to ensure that IT services meet customer needs and contribute to business goals.
[Read the article](/en/topics/automation/what-is-it-service-management-itsm "article | what is IT service management (ITSM)?")
### What is Infrastructure as Code (IaC)?
Infrastructure as Code (IaC) is the managing and provisioning of infrastructure through code instead of through manual processes.
[Read the article](/en/topics/automation/what-is-infrastructure-as-code-iac "article | What is Infrastructure as Code?")
### What is a configuration management database (CMDB)?
A configuration management database (CMDB) is used to manage information about IT components, including their attributes and relationships between them.
[Read the article](/en/topics/automation/what-is-a-configuration-management-database-cmdb "article | what is a configuration management database (CMDB)?")
Automation and management resources
-----------------------------------
### Featured product
* #### [Red Hat Ansible Automation Platform](/en/technologies/management/ansible)
  A platform for implementing enterprise-wide automation, no matter where you are in your automation journey.
[See all products](/en/technologies/all-products "See all products")
### Related content
* Blog post
  [Introducing the Red Hat Ansible Lightspeed intelligent assistant](/en/blog/evolution-red-hat-ansible-lightspeed)
* Blog post
  [Slash VM provisioning time on Red Hat Openshift Virtualization using Red Hat Ansible Automation Platform](/en/blog/accelerate-it-service-delivery-red-hats-integrated-toolset)
* Datasheet
  [Red Hat Ansible Automation Platform](/en/resources/ansible-automation-platform-datasheet)
* Blog post
  [Red Hat Ansible Automation Platform: Measuring Business Impact with Dashboard and Analytics](/en/blog/red-hat-ansible-automation-platform-measuring-business-impact-dashboard-and-analytics)
### Related articles
* [What is DevOps automation?](/en/topics/automation/what-is-devops-automation)
* [What is IT service management (ITSM)?](/en/topics/automation/what-is-it-service-management-itsm)
* [What is Infrastructure as Code (IaC)?](/en/topics/automation/what-is-infrastructure-as-code-iac)
* [Ansible vs. Kubernetes: how they work together](/en/topics/automation/Ansible-vs-Kubernetes)
* [What is a configuration management database (CMDB)?](/en/topics/automation/what-is-a-configuration-management-database-cmdb)
* [What is cloud migration? And how can automation help?](/en/topics/automation/what-is-cloud-migration)
* [What is a software-defined data center (SDDC)?](/en/topics/automation/what-is-a-sddc)
* [What is IT automation?](/en/topics/automation/what-is-it-automation)
* [Why choose Red Hat Ansible Automation Platform as your AI foundation?](/en/topics/automation/automation-and-ai)
* [How to build an IT automation strategy](/en/topics/automation/build-an-automation-strategy)
* [What is access control?](/en/topics/security/what-is-access-control)
* [What is virtual infrastructure management? And how can automation help?](/en/topics/automation/virtual-infrastructure-management)
* [What is IT migration?](/en/topics/automation/what-is-it-migration)
* [How to automate migrations with Red Hat Ansible Automation Platform](/en/technologies/management/ansible/automate-migrations-with-red-hat-ansible-automation-platform)
* [Why use Red Hat Ansible Automation Platform with Red Hat OpenShift?](/en/technologies/cloud-computing/openshift/ansible-on-openshift)
* [What is CloudOps?](/en/topics/automation/what-is-cloudops)
* [Red Hat Satellite on Red Hat Enterprise Linux](/en/technologies/management/satellite/satellite-for-rhel)
* [What is role-based access control (RBAC)?](/en/topics/security/what-is-role-based-access-control)
* [Which Red Hat Ansible Automation Platform deployment option is right for you?](/en/technologies/management/ansible/ansible-deployment-options)
* [How to manage and automate applications at the edge](/en/topics/edge-computing/how-to-manage-automate-applications-edge)
* [How to build an automation Center of Excellence](/en/topics/automation/how-to-build-automation-center-of-excellence)
* [What is orchestration?](/en/topics/automation/what-is-orchestration)
* [How to adopt Automation as Code: Extending Infrastructure as Code into Policy as Code](/en/topics/automation/how-to-adopt-automation-as-code)
* [What is a webhook?](/en/topics/automation/what-is-a-webhook)
* [Learning Ansible basics](/en/topics/automation/learning-ansible-tutorial)
* [Red Hat Lightspeed data and application security](/en/topics/management/data-application-security)
* [What is an Ansible Role—and how is it used?](/en/topics/automation/what-is-an-ansible-role)
* [What is data management?](/en/topics/data-services/what-is-data-management)
* [Gain security with Red Hat Ansible Automation Platform](/en/technologies/management/ansible/gain-security-with-red-hat-ansible-automation-platform)
* [What is NetOps?](/en/topics/automation/what-is-netops)
* [What is an Ansible Rulebook?](/en/topics/automation/what-is-an-ansible-rulebook)
* [What is an Ansible Playbook?](/en/topics/automation/what-is-an-ansible-playbook)
* [What is configuration management](/en/topics/automation/what-is-configuration-management)
* [What is event-driven automation?](/en/topics/automation/what-is-event-driven-automation)
* [Zero-Touch Provisioning and telco automation with Red Hat](/en/topics/telecommunications/zero-touch-provisioning-and-telco-automation-at-red-hat)
* [What is infrastructure automation?](/en/topics/automation/what-is-infrastructure-automation)
* [What is YAML?](/en/topics/automation/what-is-yaml)
* [What is provisioning?](/en/topics/automation/what-is-provisioning)
* [What is compliance management?](/en/topics/management/what-is-compliance-management)
* [Understanding Ansible, Terraform, Puppet, Chef, and Salt](/en/topics/automation/understanding-ansible-vs-terraform-puppet-chef-and-salt)
* [What is cloud orchestration?](/en/topics/automation/what-is-cloud-orchestration)
* [Ansible vs. Chef: What you need to know](/en/topics/automation/ansible-vs-chef)
* [What is security automation?](/en/topics/automation/what-is-security-automation)
* [Ansible vs. Salt: What you need to know](/en/topics/automation/ansible-vs-salt)
* [What is a configuration file?](/en/topics/linux/what-configuration-file)
* [Ansible vs. Puppet: What you need to know](/en/topics/automation/ansible-vs-puppet)
* [Ansible vs. Terraform, clarified](/en/topics/automation/ansible-vs-terraform)
* [Why choose Red Hat for automation?](/en/topics/automation/why-choose-red-hat-for-automation)
* [Ansible vs. Red Hat Ansible Automation Platform](/en/technologies/management/ansible/ansible-vs-red-hat-ansible-automation-platform)
* [What is cloud automation?](/en/topics/automation/what-is-cloud-automation)
* [What is network automation?](/en/topics/automation/what-is-network-automation)
* [What are managed IT services?](/en/topics/cloud-computing/what-are-managed-it-services)
* [What is business process management?](/en/topics/automation/what-is-business-process-management)
* [What is patch management (and automation)?](/en/topics/management/what-patch-management-and-automation)
* [What is the Red Hat Ansible Automation Platform automation controller?](/en/technologies/management/ansible/automation-controller-product-feature)
* [What is SOAR?](/en/topics/security/what-is-soar)
* [What is business process automation?](/en/topics/automation/what-is-business-process-automation)
* [What is IT process automation?](/en/topics/automation/what-is-it-process-automation)
* [Automating Microsoft Windows with Red Hat Ansible Automation Platform](/en/technologies/management/ansible/automate-microsoft-windows-with-ansible)
* [What is deployment automation?](/en/topics/automation/what-is-deployment-automation)
* [What is business optimization?](/en/topics/automation/business-optimization)
* [What is Kubernetes cluster management?](/en/topics/containers/what-is-kubernetes-cluster-management)
* [What is SRE?](/en/topics/devops/what-is-sre)
* [What is risk management?](/en/topics/management/what-is-risk-management)
* [What is an SOE?](/en/topics/management/what-is-an-soe)
* [What is IT system life-cycle management?](/en/topics/management/it-system-life-cycle-management)
* [What is robotic process automation (RPA?)](/en/topics/automation/what-is-robotic-process-automation)
* [What is network management?](/en/topics/management/what-is-network-management)
* [What is cloud management?](/en/topics/cloud-computing/what-is-cloud-management)
* [What's business automation?](/en/topics/automation/whats-business-automation)
[More about this topic](/en/topics/automation "More about this topic")