Ansible– Modules and different types of Modules

Ansible – Modules

Modules in Ansible are idempotent. From a RESTful service standpoint, for an operation (or service call) to be idempotent, clients can make that same call repeatedly while producing the same result. In other words, making multiple identical requests has the same effect as making a single request.
There are different types of modules in Ansible:
Core module

Extras modules

Core Modules

These are modules that the core Ansible team maintains and will always ship with Ansible itself. They will also receive slightly higher priority for all requests than those in the “extras” repos.
The source of these modules is hosted by Ansible on GitHub in the Ansible-modules-core.

Extras Modules

These modules are currently shipped with Ansible, but might be shipped separately in the future. They are also mostly maintained by the Ansible community. Non-core modules are still fully usable, but may receive slightly lower response rates for issues and pull requests.
Popular “extras” modules may be promoted to core modules over time.The source for these modules is hosted by Ansible on GitHub in the Ansible-modules-extras.
E.g: The one of the extras module in Remote Management Modules is ipmi_power module, which is a power manger for the remote machines. 
It requires python 2.6 or later and pyghmi to run.
You can use this module by writing an adhoc command like the one I have written below:

ipmi_power : name ="test.domain.com" user="localhost" password="xyz" state="on"

Ansible  – Return Values

Ansible modules normally return a data structure that can be registered into a variable, or seen directly when output by the Ansible program. Each module can optionally document its own unique return values.
Some examples of return values are:
changed: returns with a boolean value whenever the task makes any change.
failed: returns a boolean value, if the task is failed
msg: it returns a string with a generic message relayed to the user.

Ansible – AdHoc Commands

Adhoc commands are simple one line command to perform some action. 
Running modules with Ansible commands are adhoc commands.
Eg: ansible host -m netscaler -a "nsc_host=nsc.example.com user=apiuser password=apipass"
The above adhoc command uses the netscaler module to disable the server. There are hundreds of modules available in Ansible from where you can refer to and write adhoc commands.
Well, enough with all the theoretical explanations, let me explain you Ansible with some hands on.
Let’s begin :)
Step 1: Connect to your hosts using SSH. For that, you need to generate a public SSH key.
Use the command below:
ssh-keygen
As you can see in the snapshot above, the command ssh-keygen generated
 a public SSH key.
Step 2: Your next task is to copy the public SSH key on your hosts. In order to
 do that, use the command below:
ssh-copy-id -i root@<IP address of your host>
The snapshot above shows the SSH key being copied to the hosts.
Step 3: List the IP addresses of your hosts/nodes in your inventory. 
Use the following command:
vi /etc/ansible/hosts
This will open a vi editor where you can list down the IP addresses of your hosts. This is now your inventory.
Step 4: Let’s ping to ensure a connection has been established.
The snapshot above confirms that connection has been made between your control machine and host.
Step 5: Let us now write a playbook to install Nginx on the host machine. You can write your playbook in the vi editor. For that, simply create your playbook, using the command:
vi <name of your file>.yml

Comments

Post a Comment

Popular posts from this blog

Interview question for File and FTP Adapter

What is boot.properties file and how to create

SSL Exceptions in Admin Server and Node Manager.