Plugins
Plugins in Ansible extend the core functionality of Ansible itself. They are used to customize or enhance various aspects of Ansible’s behavior, such as inventory sources, connection methods, authentication mechanisms, and more.
Ansible supports several types of plugins, including inventory plugins, callback plugins, lookup plugins, filter plugins, and more.
- Inventory Plugins: Inventory plugins allow Ansible to pull inventory information from different sources, such as cloud providers, databases, or dynamic inventories.
- Callback Plugins: Callback plugins enable customizing the output and behavior of Ansible runs, such as generating custom reports or notifications.
- Lookup Plugins: Lookup plugins allow Ansible to retrieve data from external sources during playbook execution.
- Filter Plugins: Filter plugins enable you to manipulate data within Ansible playbooks, such as transforming strings or lists.
- Connection Plugins: Connection plugins define how Ansible connects to target hosts. The default connection plugin is SSH, but you can use others like Paramiko, Docker, or WinRM.
- Authentication Plugins: Authentication plugins define how Ansible authenticates with target hosts. Common authentication methods include password, key-based authentication, and more.
- Examples: Some examples of plugins include the AWS EC2 dynamic inventory plugin, the Jinja2 template lookup plugin, and the various callback plugins like “json” or “yaml”.
Modules
Modules are a type of plugin, self-contained units of code that perform specific tasks on remote systems. They are the building blocks of Ansible automation. Modules can manage system resources, install software, manipulate files, manage services, and perform various other tasks on target hosts.
Modules are executed by Ansible on target hosts. Ansible runs the desired module with specific arguments, and the module carries out the task on the remote system. The module then returns results to Ansible for further processing.
Modules in Ansible can be written in any language, unlike other plugins which are required to be written in Python. However, it is important to note that Ansible currently offers modules in Python and Powershell only.
Some built-in modules include yum, apt, file, copy, service, shell, ping, and many more. Each module has its own parameters and usage.
Let’s understand them by writing a simple Ansible Playbook
---
- name: Install and Configure Nginx
hosts: web_servers
become: yes # Run tasks as root (sudo)
tasks:
- name: Install Nginx
apt:
name: nginx
state: present # Ensure Nginx is installed
- name: Start Nginx service
service:
name: nginx
state: started # Ensure Nginx service is running
- name: Copy Nginx configuration
copy:
src: /etc/nginx/nginx.conf
dest: /etc/nginx/nginx.conf.bak
notify:
- Restart Nginx
handlers:
- name: Restart Nginx
service:
name: nginx
state: restarted
In this Ansible playbook, we have a simple task: to install and configure the Nginx web server on a group of hosts defined in the “web_servers” group in Ansible’s inventory.
Now, let’s break down the components of this playbook and identify where modules and plugins are used:
Modules:
- apt Module: This module is used in the “Install Nginx” task to install the Nginx package. It’s responsible for package management. The state parameter specifies whether to ensure the package is “present” (installed) or not.
- service Module: The “Start Nginx service” task uses the service module to manage the Nginx service. It ensures that the Nginx service is in the “started” state.
- copy Module: In the “Copy Nginx configuration” task, the copy module is used to make a backup copy of the Nginx configuration file. It manages file operations.
Plugins:
- become Plugin: The become directive at the playbook level is not a module but a plugin-like feature. It enables privilege escalation, allowing Ansible to run tasks with elevated privileges (as root, using sudo). It’s part of Ansible’s core functionality.
- notify Plugin: In the “Copy Nginx configuration” task, the notify directive is used to trigger a handler called “Restart Nginx” when the file copy operation is successful. This is a built-in feature that uses a notification mechanism and is not a separate module or plugin.
Can we make our own custom module in Ansibe?
Well, the answer is Yes, we can.
Creating a custom module in Ansible requires writing a Python script that follows Ansible’s module development guidelines. Custom modules allow you to extend Ansible’s capabilities by defining your own tasks and actions. Here’s a simple example of a custom module that prints a message:
Example –
- Create a directory structure for your custom module:
custom_modules/
├── library/
│ ├── my_custom_module.py
└── playbook.yml
- Create the custom module Python script (my_custom_module.py) in the library directory:
# custom_modules/library/my_custom_module.py
from ansible.module_utils.basic import AnsibleModule
def main():
module = AnsibleModule(
argument_spec=dict(
message=dict(type='str', required=True)
)
)
message = module.params['message']
result = dict(message=message)
module.exit_json(changed=False, meta=result)
if __name__ == '__main__':
main()
In this example, we’ve created a custom module named my_custom_module that takes a single argument, message.
- Create a playbook (playbook.yml) that uses the custom module:
# custom_modules/playbook.yml
---
- name: Use Custom Module
hosts: localhost
gather_facts: no
tasks:
- name: Invoke Custom Module
my_custom_module:
message: "Hello, Custom Module!"
register: result
- name: Show Module Result
debug:
var: result.message
This playbook defines a task that uses the my_custom_module module to print the message “Hello, Custom Module!” and then registers the result.
- Run the playbook:
ansible-playbook -i localhost, -c local custom_modules/playbook.yml
This command runs the playbook and invokes the custom module. You should see the message “Hello, Custom Module!” printed in the output.
In summary, modules are task-specific units of code that perform actions on remote systems, while plugins extend Ansible’s core functionality by providing customization and flexibility in areas like inventory management, connection methods, authentication, and more.
If you are new to Ansible, Please check out this article first.
That’s all for the Difference between Ansible Plugins and Ansible Modules.
Thank you for reading!!
Stay tuned for more articles on Cloud computing and DevOps. Don’t forget to follow me for regular updates and insights.
This was really helpful man.