Nowadays, when businesses or individuals use multiple devices or servers, manually installing the environment on each device via CLI takes a lot of time and manpower. So can we install the environment at the same time on many devices, servers? Or with a command can we do that? Then in this article I introduce to you about Ansible
What is Ansible?
- Ansible is a popular
automation
andorchestration
tool that makes it simple to automate application deployment. It can configure the system, deploy software - Ansible can automate installing, updating multiple systems, or deploying an application remotely
Advantages of Ansible
Here are some advantages of Ansible:
- Clear: Ansible uses a simple syntax (YAML) and easy to understand for anyone (developers, sysadmins, managers).
- Fast: learn quickly, install quickly and don’t have to install any other software or daemon on our server
- Complete:
batteries included
approach by Ansible, we have everything we need in a complete package - Efficient: not having additional software on our servers saves us resources and dedicates more resources to our applications. Also, since Ansible’s modules operate via JSON, Ansible is extensible with modules written in the programming language we already know.
- Secure: Ansible uses SSH and does not require additional opening port or daemon so it avoids accessing your server via port or daemon.
- Ansible is lightweight and consistent, without any constraints related to the underlying operating system or hardware
Key terms in Ansible
Here are the terms in Ansible that I have learned:
- Ansible server: is where ansible is installed and from there all tasks and playbooks will be run
- Module: A command or set of similar commands executed on the client-side. When we communicate with Ansible it will be through modules
- Task: A task that identifies a single task to be done, which are trivial tasks in playbooks
- Role: A collection of Playbooks, related files organized in predefined ways to facilitate reuse and sharing
- Fact: global variables contain information about the system
- Playbook: A YAML file that contains a set of automated tasks
- Inventory: an INI file that contains information about the remote servers you manage.
- Play: Once execute a Playbook
- Handler: used to enable state change of services
- Tag: a name given to a task, which can be used later for a specific task or a group of tasks.
How Ansible works
Below is a picture of Ansible’s activities:
As shown in the picture, the Management Node
is the Ansible server
I mentioned above, which is the place to manage the nodes that control the entire execution of the playbook.
The Playbook details everything we want to do with the servers we want to manage and how to do them.
Here, the Inventory
files give us a list of the servers the module Ansible
needs to run
After reading the hosts that we need to run in Inventory
, the Management Node
will connect to these hosts via SSH connection and execute modules.
Ansible installation
In this section, I will introduce installing Ansible on ubuntu:
- Implement update packages:
1 2 | sudo apt update |
- Install software-properties-common package
1 2 | sudo apt install software-properties-common |
- Make the installation:
1 2 3 4 | sudo apt-add-repository ppa:ansible/ansible sudo apt update sudo apt install ansible |
- Once installed we can test it out by executing the following command in the terminal:
1 2 | ansible --version |
Create a inventory file
Ansible uses a inventory
file (list of servers) to connect to our server. As with the hosts
file (/ etc / hosts) to point the IP address to the domain name, an Ansible inventory
file points servers (IP addresses or domain names) to groups. This Inventory file will probably be made available after we finish installing Ansible. In the absence of this file, we can do the following:
1 2 3 | sudo mkdir /etc/ansible sudo touch /etc/ansible/hosts |
Then we edit this file like so:
1 2 3 4 | [example] www.example.com 192.168.100.9 |
Here, the example
is the group of servers that we manage. www.example.com
and 192.168.100.9
are the domain and the IP address of the server in the group. If we use port 22 for SSH to the server, we may need to add :2222
to the address, for example www.example.com:2222
, since Ansible defaults to port 22 and will not get the value. this from our config file
Run Ad-Hoc Ansible command
Ad-Hoc commands are commands that can be run individually to quickly execute functions. They can be used when we want to issue a command on one server or multiple servers.
As above, we have created a inventory
file containing the hosts that we have defined.
We can check the access to hosts from ansible server
, using the following command:
1 2 | ansible -i hosts all -m ping |
After executing this command, on our terminal will display the status of the command, hosts in the inventory
file
We can also specify which host
we want to run the command on:
1 2 | ansible -i hosts all -m ping --limit example |
In this example, I just run the command on the host example
. In this command we have param –limit to specify which hosts in the host file
Conclude
In this part I introduce to you the basic knowledge, the frequently used terminology in the ansible and how it works. In the next section we learn about the playbook
. Thank you for watching the article
References:
- https://www.guru99.com/ansible-tutorial.html
- https://www.tutorialspoint.com/ansible
- Book: ansible-for-devops