Edited 2 weeks ago by ExtremeHow Editorial Team
AnsibleAutomationUbuntuConfiguration ManagementLinuxInstallationOperating SystemsToolsSystemSetup
This content is available in 7 different language
Automating tasks in IT infrastructure management is crucial for efficiency and reliability. Ansible is a powerful open-source tool used for automation, configuration management, and application deployment. It allows you to easily manage a large number of servers while ensuring your environments are reproducible and consistent. In this guide, we will walk you through setting up and using Ansible on Ubuntu. This comprehensive tutorial will cover installation, setting up inventory files, creating and running playbooks, and understanding the basic concepts of Ansible.
Ansible is known for its simplicity and ease of use. It does not require any agent installation on target nodes, which means no software needs to be run on your managed servers. Ansible communicates with servers via SSH and can manage both Unix-like systems and Windows platforms. The primary advantage of using Ansible is that it provides simple, powerful, and flexible configuration management and orchestration capabilities.
Setting up Ansible on Ubuntu is a straightforward process. Before installation, make sure your system is up to date by running the following command in your terminal:
sudo apt update sudo apt upgrade
Next, install Ansible by executing the following command:
sudo apt install ansible
Once the installation is complete, verify it by checking the Ansible version:
ansible --version
You will see a printout of the currently installed version, confirming successful installation.
Ansible requires an inventory file containing a list of the servers you want to manage. By default, this file is located at /etc/ansible/hosts
. You can define hosts using either individual IP addresses or hostnames grouped under easy-to-remember names that can be referenced later in your playbook.
Here's an example of a simple inventory file setup:
[webservers] 192.168.1.10 192.168.1.11 [dbservers] dbserver.example.com
In this example, we have defined two groups of servers: webservers and dbservers. Each group contains the IPs or domain names of the associated machines. Groups can be used to execute tasks on multiple servers simultaneously.
Before creating complex playbooks, you can use Ansible to execute ad hoc commands on your server. This is especially useful for quick tasks. The syntax for running an ad hoc command is:
ansible <group> -m <module> -a "<command>"
For example, take the need to check disk space on all web servers:
ansible webservers -m shell -a "df -h"
This invokes the shell module on the group "webservers" to execute the "df -h" command. Make sure the user executing the Ansible command has SSH access to the host listed in your inventory file.
While ad hoc commands are useful, playbooks are where Ansible shines. A playbook is a YAML file that contains a series of serialized tasks to run on your specified host.
A simple example of an Ansible playbook might look like this:
- hosts: webservers become: yes tasks: - name: Install Apache apt: name: apache2 state: present
This playbook tells Ansible to connect to all hosts in the “webservers” group, elevate privileges using the default sudo method (make: yes), and install the Apache web server package on each of them.
Ansible allows dynamic configuration using variables. Variables can be defined in different scopes: playbook level, host level, or group level. You can declare variables directly within the playbook:
- hosts: webservers vars: http_port: 80 tasks: - name: Ensure Apache is installed apt: name: apache2 state: present - name: Ensure Apache is started service: name: apache2 state: started
Ansible also collects facts about the system by default. You can use these collected values to make decisions in your playbook. To view the collected facts from a specific host, run:
ansible <hostname> -m setup
In Ansible, some tasks are only useful if they trigger some action, such as restarting a service when a package is updated. Enter handlers and notifications.
- hosts: webservers tasks: - name: Install apache2 apt: name: apache2 state: latest notify: - restart apache handlers: - name: restart apache service: name: apache2 state: restarted
The above playbook will restart the Apache service only if the task "install apache2" changes anything, such as installing or upgrading Apache. Handlers always run at the end of the playbook run, assuming they have been notified.
Ansible's flexibility means it can make logical decisions. Using conditionals, you can prepare your tasks to run only under certain circumstances. Conditionals use Jinja2 templating to evaluate expressions:
- hosts: webservers tasks: - name: Install Apache 2 apt: name: apache2 state: latest when: ansible_os_family == "Debian"
In this example, the task to install Apache will only execute if the operating system family of the node is Debian. The loop allows you to run the task multiple times with different items:
- hosts: webservers tasks: - name: Install needed packages apt: name: "{{ item }}" state: present loop: - "apache2" - "vim" - "git"
This task will iterate over the list and install each listed package (Apache, Vim, and Git).
With its rich ecosystem of modules and functionality, Ansible provides a powerful framework for automating and managing IT infrastructure. By following this guide, you should have a basic understanding of how to setup Ansible on Ubuntu and execute basic playbooks and ad hoc commands. The tools and concepts discussed are only the foundation; Ansible's capabilities extend far beyond what is covered here. As you gain experience with Ansible, you'll find that its capabilities are robust enough to handle a wide range of infrastructure challenges, boosting efficiency through reliable automation.
If you find anything wrong with the article content, you can