Introduction
In the world of DevOps where automation is necessary, ansible is one tool used to automate tedious and manual tasks.
What is Ansible?
Ansible is a configuration management tool used to automate repetitive tasks like cloud provisioning, and application deployment.
Ansible connects to nodes and uses the concept of control and managed nodes and push modules to them from a centralized place. This will then execute the modules and automatically remove them when the action is complete.
Ansible is agentless in the sense that no additional software is required to be installed on the target machines, ansible is just executing bash commands or actions over SSH or Windows Remote Management connections.
Basic Ansible Terms
- Hosts: a machine (either physical hardware or remote) hosted by ansible.
- Inventory: a collection of all the hosts and groups that Ansible manages.
- Group: Several hosts grouped together that share a common attribute.
- Module: Units of code that Ansible sends to the nodes for execution or actions run by tasks.
- Tasks: Units of action that combine a module and its arguments along with some other parameters.
- Playbooks: An ordered list of tasks along with their necessary parameters that define a recipe to configure a system.
- YAML: A popular and simple data format that is very clean and understandable by humans (Ansible playbooks are written in YAML format).
- Roles: Redistributable units of organization that allow users to share automation code easier.
Demo: In this article, I will demo how to use ad hoc commands (a quick way to run a task on one or more managed nodes) and how to use ansible playbook to install an application on a virtual machine.
Prerequisites
- A cloud provider (Azure with an active subscription) - to create virtual machines.
- An SSH client (I will be using Mobaxterm) to SSH into my VMs.
Steps
- Login to your Azure account and create a virtual machine (for now I will create 1 VM which will be my control node).
- Using an SSH client, I will SSH into my control node
- To enable me ssh into other machines, I will need to create an ssh key on my control node using
ssh-keygen -t rsa
command
- After which I will navigate to the directory and get the public key.
- Go to the Azure portal, search for ssh keys and create a key with the public key gotten from the step above.
- I will create 2 VMs that will serve as my managed nodes with the public key so as to enable swift ssh connection between the control node and managed nodes.
- SSH into VM1 using
ssh <ip address of vm>
on the control node.
- And use
exit
to logout out of the machine (I repeated the same process on my second VM). I will install ansible on the control node (no installation will be done on the managed node because ansible is agentless).
Firstly, I will run
sudo apt-get update
command to download information for all packages listed in the sources file.
Checking to see if the installation was successful by using
ansible --version
command.
I will create an inventory file to store a list of my host and group (I grouped my hosts in a test group); note that the default inventory file is located in
/etc/ansible/hosts
Using ad hoc command:
ansible test -i inventory -m ping
to execute a ping command on all hosts.
Using ansible playbook to deploy a website on virtual machines
Ansible playbooks are the simplest way to automate repeating tasks in the form of reusable and consistent configuration files. They are written in YAML and contain any ordered set of steps to be executed on our managed nodes.
- My playbook which will deploy a website on my target machines is in the main.yml file
- Before executing the play book command, notice that the VMs are empty (not accessible).
- Using
ansible-playbook -i inventory main.yml
command to run playbook; notice that it runs each task for the 2 managed nodes/target machines I specified in my inventory file.
- After the playbook is run, I can now access the website on both target machines (notice the different IP addresses for both machines on the images below)
- Ansible is agentless meaning that no additional software installation is required on the managed nodes.
- Ansible is just executing commands over SSH.
- The default inventory file in ansible is located in /etc/ansible/hosts.
- Ansible modules are idempotent which means that changes are applied only if needed; the current state is checked and nothing is done unless the current state is different from the specified final state.
- Ansible playbooks are written in yaml format
Challenge Encountered: the challenge I encountered was the inability to establish an ssh connection between my VMs, but this was resolved by creating an ssh key and passing the public key to the managed nodes while the control node had the private key.
Conclusion: through this tutorial, we have learned:
- About ansible and its basic terms.
- How to use a simple ad hoc command to ping host machines.
- How to use a playbook to deploy a website on a virtual machine.
Kindly visit the project repo and thank you for reading.
Top comments (0)