- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Install and Configure Ansible on Ubuntu 20.04?
Ansible is an open-source automation tool that is used to automate software provisioning, configuration management, and application deployment. It uses a simple syntax called YAML to describe tasks in a playbook. Ansible can be used to manage a large number of servers and can be run from a central location. In this article, we will discuss how to install and configure Ansible on Ubuntu 20.04.
Step 1: Update System Packages
Before installing Ansible, it is always a good practice to update system packages to latest version. Open your terminal and execute following commands −
sudo apt update sudo apt upgrade -y
The above command will update and upgrade all installed packages.
Step 2: Install Ansible
Once system packages are updated, we can install Ansible using apt package manager by executing following command −
sudo apt install ansible -y
This command will install Ansible on Ubuntu 20.04 system.
Step 3: Configure Ansible Hosts File
Ansible uses a hosts file to know which servers to manage. By default, hosts file is located at /etc/ansible/hosts. We need to add IP address or hostname of servers that we want Ansible to manage.
Open hosts file using following command −
sudo nano /etc/ansible/hosts
This will open hosts file in nano editor. In this file, we can add IP addresses or hostnames of servers that we want to manage. format of hosts file is as follows −
[group_name] server1 ansible_host=ip_address_or_hostname server2 ansible_host=ip_address_or_hostname
Here, group_name is name of group of servers that we want to manage, and server1, server2 are names of servers that we want to manage. ansible_host is IP address or hostname of server.
Save and close hosts file.
Step 4: Testing Ansible
To test if Ansible is properly installed and configured, we can execute a simple command to ping servers listed in hosts file.
ansible -m ping all
This command will ping all servers listed in hosts file. If Ansible is properly configured, it will return a success message for each server.
Step 5: Creating a Playbook
Now that Ansible is properly installed and configured, we can create a playbook to automate tasks on servers. A playbook is a YAML file that contains a list of tasks that Ansible should perform on servers.
Create a new file called playbook.yaml in your home directory using following command −
In this file, we can add list of tasks that Ansible should perform on servers. For example, following playbook will install Apache on servers −
--- - name: Install Apache hosts: group_name become: true tasks: - name: Install Apache apt: name: apache2 state: present
In this playbook, name is a description of playbook, hosts is name of group of servers that we want to manage, and become: true allows Ansible to run commands with elevated privileges. tasks section contains a list of tasks that Ansible should perform. In this example, Ansible will install Apache using apt module.
Save and close playbook.yaml file.
Step 6: Running Playbook
To run playbook, we can execute following command −
This command will execute playbook and install Apache on all servers listed in the hosts file under group_name section.
Step 7: Additional Ansible Configuration
There are some additional Ansible configurations that can be done to enhance its functionality. These configurations are stored in ansible.cfg file located in /etc/ansible/ directory.
One configuration that can be added is default user that Ansible should use to connect to servers. To do this, open ansible.cfg file using following command −
sudo nano /etc/ansible/ansible.cfg
In this file, uncomment line that says remote_user and add username that you want Ansible to use to connect to servers.
remote_user = your_username
Save and close file.
Another configuration that can be added is location of inventory file. To do this, open ansible.cfg file and uncomment line that says inventory and add location of your inventory file.
inventory = /path/to/your/inventory/file
Save and close file.
Here are some additional tips and tricks that you can use to further enhance your Ansible installation −
Use Roles − Ansible roles are a way to organize your playbook into reusable and modular components. By using roles, you can separate your tasks into logical units, making your playbook easier to manage and maintain.
Use Variables − Ansible allows you to define variables that can be used throughout your playbook. Variables can be used to store information such as IP addresses, usernames, and passwords, making your playbook more flexible and customizable.
Use Ansible Vault − Ansible Vault is a feature that allows you to encrypt sensitive data such as passwords, keys, and other secrets. Ansible Vault is a secure way to store your secrets, and it can be easily integrated into your playbook.
Use Ansible Tower − Ansible Tower is a web-based UI and automation platform that allows you to centrally manage your Ansible infrastructure. With Ansible Tower, you can automate your playbook runs, manage your inventory, and monitor your infrastructure.
Use Ansible Modules − Ansible comes with a wide range of built-in modules that can be used to automate a variety of tasks. Ansible modules can be used to install software, manage users, configure network settings, and more.
Use Ansible Collections − Ansible Collections are a way to package and distribute your Ansible content such as modules, plugins, and roles. Ansible Collections can be installed using ansible-galaxy command, and they can be used to extend functionality of your Ansible installation.
Use Ansible Dynamic Inventory − Ansible dynamic inventory is a feature that allows you to generate your inventory file dynamically. This is useful if you have a large number of servers or if your infrastructure is constantly changing. Ansible dynamic inventory can generate your inventory file based on cloud providers such as AWS, Azure, or GCP, or based on other sources such as a CMDB or a database.
Use Ansible Playbook Best Practices − There are some best practices that you can follow when writing your Ansible playbook to make it more readable and maintainable. Some of these best practices include using descriptive names for your tasks and roles, using comments to explain your playbook, and organizing your playbook into logical sections.
Use Ansible Ad-Hoc Commands − Ansible ad-hoc commands allow you to run a single task on a set of servers without having to create a playbook. Ad-hoc commands are useful for performing quick and simple tasks such as checking disk space on your servers or restarting a service.
Use Ansible Debugging Tools − Ansible comes with a number of debugging tools that can help you troubleshoot issues in your playbook. Some of these tools include ansible-playbook --check command, which allows you to check your playbook for syntax errors, and ansible-playbook --start-at-task command, which allows you to start your playbook at a specific task.
Use Ansible Templates − Ansible templates are a way to create dynamic configuration files that can be used across multiple servers. Templates allow you to use variables and conditions to generate configuration files that are specific to each server. This makes your configuration management more efficient and flexible.
Use Ansible Callback Plugins − Ansible callback plugins are a way to extend functionality of Ansible and add custom output formats or actions. Callback plugins can be used to send notifications, log output to a database, or integrate Ansible with other tools such as Slack or PagerDuty.
Use Ansible Testing Frameworks − Ansible testing frameworks such as Molecule and Testinfra allow you to test your Ansible playbook in a reproducible and automated way. Testing your playbook ensures that it works as expected and prevents errors and issues from happening in production.
By using these additional tips and tricks, you can extend power and flexibility of Ansible beyond just server management and achieve a highly automated and efficient infrastructure that meets your specific needs and requirements. Ansible is a versatile and powerful automation tool that can help you manage your entire infrastructure, from servers to networks to containers and beyond.
In this article, we discussed how to install and configure Ansible on Ubuntu 20.04. We covered steps to install Ansible, configure hosts file, test Ansible, create a playbook, and run playbook. We also covered some additional Ansible configurations that can be added to enhance its functionality.
Ansible is a powerful automation tool that can save a lot of time and effort in managing a large number of servers. With Ansible, tasks can be automated, which makes whole process faster and more efficient.
Kickstart Your Career
Get certified by completing the courseGet Started