Monday, 23 April 2018

Ansible– 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 modules
  • 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.
E.g: 
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
The below snapshot shows my playbook to install Nginx written in YAML format
The tasks of a playbook are defined in YAML as a list of dictionaries and executed 
from top to bottom. If we have several hosts, then each task is tried for each host before 
moving on to the next one. Each task is defined as a dictionary that can have several keys, 
such as “name” or “sudo” which signify the name of the task and whether it 
requires sudo privileges. 
A variable server_port is set that listens on TCP port 8080 for incoming requests.
Here, the first task is to get the necessary package for installation of Nginx and then install it. Internally, Ansible will check if the directory exists and create it if it’s not, otherwise it will do nothing.
The next task is to configure Nginx. In Nginx, contexts contain configuration details.
Here, the template is a file you can deploy on hosts. However, 
template files also include some reference variables which are pulled from variables defined as part of an Ansible playbook or facts gathered from the hosts. 
Facts containing the configuration details are being pulled from a source directory and being copied to a destination directory.
Handlers here define the action to be performed only upon notification of tasks or state changes. In this playbook, we defined, notify: restart Nginx handler which
 will restart Nginx once the files and templates are copied to hosts.
Now, save the file and exit.
Step 6: Now let’s run this playbook, using the command below:
ansible-playbook <name of your file>.yml
Run Playbook - Ansible Tutorial - Edureka
We can see in the screenshot above that our task is getting executed; Nginx being installed.
Step 7: Let’s check if Nginx is installed on my host machine. Use the command below:
ps waux | grep nginx
Run Nginx - Ansible Tutorial - Edureka
now the screenshot above, that different process ids 3555 and 103316 are running which ensures that Nginx is running on your host machines.
deployed Nginx on your host using Ansible playbooks. 

No comments:

Post a Comment