Monday 26 August 2019

Role of Ansible in DevOps


Ansible is an open source automation platform. Ansible can help you with configuration management, application deployment, task automation and also IT orchestration.
It is very simple to setup, efficient and powerful tool to make IT professional's life easy.

Features of Ansible:

  1. Ansible uses YAML syntax to define playbook configuration files which is having minimum syntax and easy to understand by humans. 
  2. Write few lines of code to manage and provision your infrastructure.
  3. The work velocity of developers were affected since sysadmins were taking time to configure servers.
  4. Roll-ups and Roll-backs are possible if we want to go back to Java 1.8 from Java 2.0.
  5. Grouping of Servers for partial deployment, let's say on 40 servers out of 100 servers.
  6. Uses push based configuration methodology, where no agent needs to be installed on slaves.
  7. Agent-less configuration tool unlike Puppet & Chef that uses Pull-based (agent-based) config methodology.
  8. Ansible Tower is a GUI based tool where deployment can be done from UI for Large scale enterprises.


Sample Ansible Playbook config file created to make understand the syntax:


Download the sample file - Click Here


Playbooks are simple files written in YAML code. Used to declare configurations and launching task synchronously and asynchronously.
A YAML file will always start with '---' three hyphens.
Indentation matters a lot in YAML files. Running an incorrectly indentated file will run into error and you'll have to spend a lot of time in checking for that extra space.


Hosts is simply a remote machine that Ansible manages. They can have individual variables assigned to them, and can also be organized in groups.


Tasks combine an action with a name. Playbooks exist to run tasks.


Action is a part of a task that specifies which of the modules to run and which arguments to pass to that module. Each task can have only one action, but it may also have other parameters.


Notify is the act of a task registering a change event and informing a handler task that another action needs to be run at the end of the play. If a handler is notified by multiple tasks, it will still be run only once.


Handlers are just like task but they will only run when notified by successful completion of that task.
Handlers are run in the order they are listed, not in the order that they are notified. They are placed at same level (identation) as hosts and tasks in YAML.

-----------------------------------------------------------------------------------------

We can execute commands on remote machines via 2 commands

1. Directly by issuing command
ansible all -m copy -a "src=/home/user1/test.html dest=/home/user2"

2. By creating a playbook file and running it.
ansible-playbook /opt/playbooks/copyfile.yml


##To check the syntax of playbook.yml file

ansible-playbook test-playbook.yml --syntax-check


##To check the list of modules that comes installed with Ansible
(You will be surprised to know that there are around 3000 modules installed by default)
ansible-doc -l


##To test a module named ping, for checking connectivity between controller and slave nodes
ansible -m ping web-servers


If you'll start exploring various modules in ansible, trust me you will fall in love with this tool. As there is nothing which can not be accomplished by it.

No comments:

Post a Comment