// Tutorial //

How to Install and Configure Ansible on Ubuntu 18.04 [Quickstart]

Published on April 16, 2020 · Updated on April 16, 2020
Default avatar
By Erika Heidi
Developer Advocate
English
How to Install and Configure Ansible on Ubuntu 18.04 [Quickstart]

Introduction

In this guide, we will discuss how to install and configure Ansible on an Ubuntu 18.04 server. For a more detailed version of this tutorial, with more explanations of each step, please refer to How to Install and Configure Ansible on Ubuntu 18.04.

Prerequisites

To follow this tutorial, you will need:

  • One Ansible Control Node: an Ubuntu 18.04 system where Ansible will be installed. This can be a remote server or a local machine.
  • One or more Ansible Hosts: one or more Ubuntu 18.04 servers that are accessible from your Control Node via SSH.

Step 1 — Install Ansible

From your control node, run the following command to include the official project’s PPA (personal package archive) in your system’s list of sources:

  1. sudo apt-add-repository ppa:ansible/ansible

Refresh your system’s package index with:

  1. sudo apt update

Following this update, you can install the Ansible software with:

  1. sudo apt install ansible

Step 2 — Set Up the Inventory File

To edit the contents of your default Ansible inventory, open the /etc/ansible/hosts file using your text editor of choice:

  1. sudo nano /etc/ansible/hosts

The default inventory file provided by the Ansible installation contains a number of examples that you can use as references for setting up your inventory. The following example defines a group named [servers] with three different servers in it, each identified by a custom alias: server1, server2, and server3. Be sure to replace the highlighted IPs with the IP addresses of your Ansible hosts.

/etc/ansible/hosts
[servers]
server1 ansible_host=203.0.113.111
server2 ansible_host=203.0.113.112
server3 ansible_host=203.0.113.113

[all:vars]
ansible_python_interpreter=/usr/bin/python3

The all:vars subgroup sets the ansible_python_interpreter host parameter that will be valid for all hosts in this inventory. This parameter makes sure the remote server uses the /usr/bin/python3 Python 3 executable instead of /usr/bin/python (Python 2.7), which is not present on recent Ubuntu versions.

Don’t forget to save and close the file when you’re finished.

Step 3 — Test Connection

You can use the -u argument to specify the remote system user. When not provided, Ansible will try to connect as your current system user on the control node.

From your Ansible control node, run:

  1. ansible all -m ping -u root

You should get output similar to this:

Output
server1 | SUCCESS => { "changed": false, "ping": "pong" } server2 | SUCCESS => { "changed": false, "ping": "pong" } server3 | SUCCESS => { "changed": false, "ping": "pong" }

If this is the first time you’re connecting to these servers via SSH, you’ll be asked to confirm the authenticity of the hosts you’re connecting to via Ansible. When prompted, type yes and then hit ENTER to confirm.

Once you get a "pong" reply back from a host, it means you’re ready to run Ansible commands and playbooks on that server.

Here are links to more detailed guides related to this tutorial:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Developer Advocate

Dev/Ops passionate about open source, PHP, and Linux.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment
Leave a comment...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click here to Sign up and get $200 of credit to try our products over 60 days!