In this blog, we will learn how to run ansible in an ubuntu virtual machine. We will also learn some tips and tricks to make our experience with ansible more enjoyable.
Ansible is a configuration management, deployment, and orchestration tool. It uses SSH for communication between nodes.
So there is no need to install any agent in the nodes. We can use playbooks to write automation processes in Ansible.
This guide will help you to install Ansible on Ubuntu 18.04 LTS & 16.04 LTS systems.
Setting up Ansible on Ubuntu
Ansible is a configuration management system that automates server configuration, application deployment, and task management.
It’s a simple but powerful tool that can greatly simplify configuration management tasks.
In this guide, we will show you how to install Ansible on an Ubuntu 18.04 server and manage server configurations with it.
Installing Ansible on Ubuntu 18.04 Server#
- To install Ansible on our Ubuntu server, we will first need to install some prerequisite packages. We can do this with the apt command:
sudo apt update
sudo apt install software-properties-common
- Next, we’ll add the Ansible repository to our sources list:
sudo apt-add-repository ppa:ansible/ansible
- Hit ENTER to continue when prompted. Finally, we can install the Ansible package itself with apt:
sudo apt install ansible -y
ansible --version2.7 should be displayed indicating a successful installation
Configuring Ansible User Accounts
By default in Ubuntu, only privileged users (users with sudo access) can run Ansibl e playbooks.
However, for ease of use, you may want to run playbooks as a regular user account instead of using sudo all the time.
There are two ways to accomplish this – you can either create a new user account specifically for running playbooks or you can use your existing user account if it has sudo privileges.
In this guide, we will show you how to setup both methods so that you can choose which one is right for your needs
Running Ansible on Ubuntu
Ansible is a configuration management tool used for managing server configurations, deploying applications, and automating tasks.
We will also create a simple Playbook to deploy a static HTML website. Ansible playbooks are written in the YAML data serialization format and they define a set of tasks to be executed on a remote host or a group of remote hosts.
Before we get started, make sure you have Ansible installed on your local system. If not, you can follow our guide on How to Install Ansible on CentOS 7.
Ansible playbooks are a configuration and multinode deployment system. They can describe a policy you want your remote systems to enforce or a set of steps in a general IT process.
If Ansible modules are the tools in your toolbox, playbooks are your toolbox. And like your favorite power tools, Ansible playbooks can get pretty hairy when they grow beyond a few dozen lines.
The good news is, there are some solid best practices you can follow to keep your Ansible playbooks clean, dry, and reusable. In this post we’ll cover 9 tips for building better Ansible playbooks.
- Keep Your Playbooks Short and Sweet
This one might seem obvious, but it bears repeating: the shorter and simpler your playbook, the easier it is to read and maintain. shoter single-purpose playbooks are easier to troubleshoot than long monolithic ones. When in doubt, break out related tasks into separate playbooks and include them as roles or as imports in your main playbook.
- Write Idempotent Tasks
An idempotent task is one that has the same effect whether you run it once or a hundred times. That might sound like a strange thing to aim for, but idempotent tasks are important because they can be run over and over again without any adverse side effects.
For example, say you have a task that installs a particular software package on all of your servers. If the task is idempotent, then running it multiple times on the same server will have the same effect as running it once: the package will be installed (or, if it was already installed, nothing will happen). On the other hand, if the task is not idempotent, then running it Again will install two copies of the software package (or worse).
- Use Includes wisely
Include files let you organize related tasks into logical units that can be reused in other playbooks or included directly in plays. You can think of them as building blocks for larger playbooks
While includes can be very useful, they should be used sparingly — too many included files can make a playbook hard to follow. A good rule of thumb is to use includes only when you need to reuse tasks in more than one place; if you’re only including tasks in one playbook, it’s probably cleaner to just embed them directly in the plays where they’re used
Ansible roles are consists of many playbooks, which is similar to modules in Puppet. The biggest advantage with Ansible roles is, it is much easier to reuse the code and schedule the tasks. In this chapter, we will see how to create and use Ansible roles with examples.
Roles are the way of bundling all the related playbooks and other files in one place so that it can be reused.
One can put all the plays of MySQL installation in one role and all the plays of Apache installation in another role.
All you need is to call these roles in site.yml to install both MySQL and Apache. As these two roles are independent, they can be run parallelly without any issue as they do not have dependency on each other.
If you want to install an Ansible role on your Ubuntu virtual machine, you can use the Galaxy CLI tool. This tool will allow you to search for roles, install them, and even create your own.
First, you’ll need to make sure that you have the latest version of Ansible installed. You can do this by running the following command:
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo apt-add-repository --yes ppa:ansible/ansible
$ sudo apt update
$ sudo apt install ansible
Once Ansible is installed, you can begin using the Galaxy CLI. The first thing you’ll need to do is create a file called “requirements.yml” in your current working directory. This file will contain a list of all the roles you want to install.
For example, if you wanted to install the “nginx” role, your requirements.yml file would look like this:
- src: geerlingguy.nginx version: v1.0.0
After requirements.yml has been created, you can use the following command to install all of the roles listed in the file:
$ ansible-galaxy install -r requirements.yml
Ansible Vault is a feature of Ansible that allows you to keep sensitive data, such as passwords or private keys, in encrypted files, rather than as plaintext in playbooks or roles. These vault files can then be distributed or placed in source control.
To use Ansible Vault, you need to create a vault file. This can be done with the ansible-vault create command:
$ ansible-vault create myvault.yml
You will be prompted for a password to encrypt the file with. Once the file is created, you can edit it like any other YAML file. When you are finished, you can encrypt the file again with the ansible-vault encrypt command:
$ ansible-vault encrypt myvault.yml
The file will be encrypted with the same password you used to create it. To edit an encrypted file, you must supply the password using the –ask-vault-pass or –vault-password-file options:
$ ansible-vault edit --ask-vault-pass myvault.yml
Now that you know how to run Ansible on an Ubuntu virtual machine, you can start automating your infrastructure tasks.
If you need more help, check out the Ansible Documentation.