How To Install and Configure Ansible on Ubuntu

Andrei Maksimov

Andrei Maksimov

0
(0)

Ansible is an open-source software provisioning, configuration management, and application-deployment tool which allows you to manage your infrastructure in infrastructure as code (IaC) way. In this article, we’ll cover the installation and initial configuration for Ansible on Ubuntu Linux.

Ansible Logo

Why Ansible

Operation teams and administrators use configuration management tools to simplify the automation process of managing configurations of a large number of servers. By using Ansible you have control over numerous systems from a central location.  

The most popular configuration management tools available for you are:

  • Puppet
  • Chef
  • Ansible

Here’s a chart from Google Trends, which shows the search volume for all of them: 

Ansible is very simple, which makes it a favorite tool of choice for many people worldwide. It uses SSH for executing automation tasks described in YAML files.

Ansible also supports containerized workloads and infrastructure deployment automation to private and public clouds.

It does not matter what requirements are for your application; Ansible will take the responsibility of creating, managing, and maintaining everything.

Here are primary use-cases for Ansible:

  • Private Infrastructure And Cloud Automation: Ansible is a powerful and handy tool that permits users to handle various physical devices, such as storage devices, bare-metal servers, and even network devices. It is simple to use Ansible’s Playbook-based automation and integrating it with your existing provisioning infrastructure.
  • IT Service Management: You can modernize service processes faster and more effectively by automating service requests and offering a single truth source regarding IT asset configurations.
  • Virtualization Automation: Ansible supports many leading virtualization industry frameworks, including Vagrant, VMware, Red Hat Virtualization (RHV), vSphere. Ansible can provision virtual worlds, orchestrate and deploy them into any virtual environment.
  • Container Automation: Ansible supports the Red Hat OpenShift Container Platform and other container technologies and handles native Kubernetes and Docker container management.  Ansible wraps native commands on all of these frameworks, enabling you to automate almost anything.

In the next part of this guide, we will check out installing and configuring Ansible on the Ubuntu system.

Installing Ansible

To install Ansible on Ubuntu Linux, use the following command:

sudo apt install ansible
1. How To Install and Configure Ansible on Ubuntu - apt-get install ansible

Let’s verify successful Ansible installation:

ansible --version
2. How To Install and Configure Ansible on Ubuntu - ansible version

If you’re installing Ansible on a brand new server or Docker container, execute the command to generate the SSH key:

ssh-keygen

You can press Enter to answer the questions to proceed with the default SSH key configuration, or you can make the required changes.

3. How To Install and Configure Ansible on Ubuntu - ssh-keygen

If you’d like to learn more about SSH, its configuration, and usage examples, I recommend you our article Top 10 SSH features you must know to be more productive.

Ansible host configuration 

OpenSSH setup

To unlock the capability to use Ansible for a server or container automation, we need to install an SSH package on it.

sudo apt install openssh-server -y
4. How To Install and Configure Ansible on Ubuntu - apt-get install openssh-server

Check that our server SSH service status is active:

sudo systemctl status sshd
5. How To Install and Configure Ansible on Ubuntu - systemctl status sshd

You can also enable the sshd service if its status is not Active.

sudo systemctl start sshd

If you enabled the UFW firewall, make sure that you did not forget to allow SSH traffic to the host:

sudo ufw allow ssh
6. How To Install and Configure Ansible on Ubuntu - ufw allow ssh

Ansible user

Now, we need to add a new user named ansible.

sudo adduser ansible

Fill out all the required credentials, including password, full name, etc.

Also, type ‘y‘ in the terminal if you provided the correct information.

7. How To Install and Configure Ansible on Ubuntu - adduser ansible

Next, we need to set the passwordless access for the ansible user and him the ability to execute administrative commands:

echo "ansible ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/ansible
8. How To Install and Configure Ansible on Ubuntu - sudoers

Look out for the host IP address where you created the ansible user:

hostname -I
9. How To Install and Configure Ansible on Ubuntu - hostname

Let’s copy the public SSH key to the host where we installed Ansible to allow passwordless authentication:

ssh-copy-id [email protected]
10. How To Install and Configure Ansible on Ubuntu - ssh-copy-id

Use the ‘-L‘ option in the usermod command for disabling the password-based login:

sudo usermod -L ansible
11. How To Install and Configure Ansible on Ubuntu - usermod ansible

Let’s try to SSH to the host using an ansible user.

You should not use a password:

ssh [email protected]
12. How To Install and Configure Ansible on Ubuntu - ssh host

Testing Ansible connection to the host

Now, let’s test that we’re ready for Ansible automation.

We need to create a new directory for Ansible.

mkdir ~/ansible-demo
13. How To Install and Configure Ansible on Ubuntu - mkdir ansible-demo

Jump to the ansible-demo directory:

cd ~/ansible-demo/
14. How To Install and Configure Ansible on Ubuntu - cd ansible-demo

Add the IP address of the host you want to manage the hosts file:

nano hosts
15. How To Install and Configure Ansible on Ubuntu - nano hosts

Enter the Ansible host IP and saved this file:

16. How To Install and Configure Ansible on Ubuntu - edit hosts content

Finally, we can ping the host by using Ansible:

ansible all -i ./hosts -u ansible -m ping

The SUCCESS refers to the successfully established connection to the Ansible host. In another case, UNREACHABLE will display if the system found any issue in testing Ansible automation.

17. How To Install and Configure Ansible on Ubuntu - ansible ping

Now, you can start developing your Ansible automation playbooks to automate host configuration. 

Summary

This article has provided you a step-by-step process for installing and setting up the Ansible on Ubuntu. Starting from now, you can work on automation playbooks.

We hope this article was helpful for you. If so, please, help us to spread it to the world.

Credits

We’ve obtained the Ansible logo from Wikimedia Commons.

How useful was this post?

Click on a star to rate it!

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Top rated Udemy Courses to improve you career

Subscribe to our updates

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Want to be an author of another post?

We’re looking for skilled technical authors for our blog!

Leave a comment

If you’d like to ask a question about the code or piece of configuration, feel free to use https://codeshare.io/ or a similar tool as Facebook comments are breaking code formatting.