Ansible is an open-source IT automation tool used for tasks such as configuration management, application deployment, and orchestration. It allows users to automate repetitive tasks across multiple systems in a simple and agentless way, using human-readable YAML playbooks. Ansible communicates over SSH or PowerShell, making it lightweight and easy to set up without the need for additional software on client systems. Its simplicity, scalability, and flexibility make it a popular choice for automating cloud, network, and server environments.
In this tutorial we will demonstrate Ansible on Ubuntu. To install ansible, we first need to add ppa:ansible/ansible repository. Then we run apt-get update and apt-get install commands to install ansible.
sudo apt-add-repository -y ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible
Under /etc/ansible we see hosts file where we can add our repository i.e. collection of machines where we want to apply the configuration.
We can now configure these hosts using ansible, let’s try something basic e.g. running a ping command.
ansible -i ./hosts --connection=local local -m ping
Explanation of command: start with ansible (keyword), trigger hosts file, target is local host and we are running a ping command. The result is success as you can see from the above snapshot
Now we will install ngnix using ansible,
ansible -i ./hosts local --connection=local -b --become-user=root -m shell -a 'apt-get -y install nginx'
Explanation of command – start with ansible keyword, trigger hosts file, connect with local computer, assume the role of root using root shell install ngnix
This way we can use bash scripts, however, the power of ansible comes with modules. We can run commands with an assurance of the result. Ansible modules ensure indempotence – we can run the same Tasks over and over without affecting the final result.
For installing software on Debian/Ubuntu servers, the “apt” module
ansible -i ./hosts local --connection=local -b --become-user=root -m apt -a 'name=nginx state=installed update_cache=true'
Again we are running the same command, this time using apt module. The result is changed:false, this is because nginx is already installed on this machine.
Playbooks can run multiple Tasks and provide some more advanced functionality that we would miss out on using ad-hoc commands. Let’s move the above Task into a playbook.
Create YAML file and edit it
hosts: local
connection: local
become: yes
become_user: root
tasks:
- name: Install Nginx
apt:
name: nginx
state: installed
update_cache: true
Handler is another useful feature of ansible, A Handler is exactly the same as a Task (it can do anything a Task can), but it will only run when called by another Task. You can think of it as part of an Event system; A Handler will take an action when called by an event it listens for.
This is useful for “secondary” actions that might be required after running a Task, such as starting a new service after installation or reloading a service after a configuration change.
hosts: local
connection: local
become: yes
become_user: root
tasks:
- name: Install Nginx
apt:
name: nginx
state: installed
update_cache: true
notify: Start Nginx
handlers:
- name: Start Nginx
service:
name: nginx
state: started
Here we add a notify
directive to the installation Task. This notifies any Handler named “Start Nginx” after the Task is run. Then we can create the Handler called “Start Nginx”. This Handler is the Task called when “Start Nginx” is notified. This particular Handler uses the Service module, which can start, stop, restart, reload (and so on) system services. In this case, we tell Ansible that we want Nginx to be started.
Note: Remove nginx if it is already installed; handler – secondary task will fail if install nginx (primary task) fails (as it is already installed)
Command to remove nginx from ubuntu is: sudo apt-get purge nginx nginx-common
Now lets add some more tasks
- hosts: localhost
become: yes
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Install Nginx
apt:
name: nginx
state: present
notify:
- Restart Nginx
- name: Ensure Nginx is enabled and started
service:
name: nginx
state: started
enabled: yes
handlers:
- name: Restart Nginx
service:
name: nginx
state: restarted
In conclusion, Ansible is a powerful and flexible automation tool that simplifies IT operations by streamlining tasks like configuration management, application deployment, and orchestration. Its agentless architecture, ease of use, and scalability make it an ideal solution for businesses of all sizes looking to improve efficiency, reduce errors, and enhance system management. As IT environments continue to grow in complexity, Ansible’s ability to automate and manage processes across diverse systems positions it as an essential tool for modern DevOps and IT teams.
Enjoy Learning 🙂
Add a Comment