Ansible Modules

Introduction to modules

Modules are also referred to as “task plugins” or “library plugins”. These are discrete units of code that can be used from the command line or can be used as a task in a playbook. Ansible executes each module, usually on the remote target node/nodes.

You can execute modules from the command line:

[root@devopscheetah-server ~]# ansible webservers -m service -a “name=httpd state=started”
[root@devopscheetah-server ~]# ansible webservers -m ping
[root@devopscheetah-server ~]# ansible webservers -m command -a “/sbin/reboot -t now”

Each module supports taking arguments. Nearly all modules take key=value arguments, space delimited. Some modules take no arguments, and the command/shell modules simply take the string of the command you want to run.
From playbooks, Ansible modules are executed in a very similar way:

– name: reboot the servers
action: command /sbin/reboot -t now

  Which can be abbreviated to:

– name: reboot the servers
command: /sbin/reboot -t now

  Another way to pass arguments to a module is by using YAML syntax also called ‘complex args’

– name: restart webserver
service:
  name: httpd
state: restarted

  Ansible modules are categorized in various group based on their functionality. Below are example of some of the groups with their corresponding modules :

 

 

 

Let’s take some examples of different Modules’s syntax and how are they used :

  1. Copy Module : Used to copy a file from source to destination. We can also provide the owner and permissions to the file

 

2) Command Module : Use to execute any command on target machine.

 

3) Script Module : To execute a script on a target machine. You just need to run ansible command and it will not only copy the script on target machines but also execute them.

 

 

4) Service Module : It is used to manage service i.e. stop/start/restart service from controller node.

 

 

5) Lineinfile Module : This is to add a line in a particular file.

 

Leave a Reply

Your email address will not be published. Required fields are marked *