• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How To Install and Configure Ansible on Ubuntu 20.04

Written by Rahul, Updated on October 1, 2020

Ansible is an automation tool for managing multiple remote hosts from the single machine. It provides you with an easy to configure for a large number of remote hosts. For example, you are performing the same tasks over multiple machines, Ansible provides you the option to automate these tasks.

Ansible is a better alternatives of the other popular infrastructure automation tools available like Chef and Puppet. You don’t need to install any client software on nodes to manage through Ansible server. It uses SSH connection to execute tasks on nodes.

This tutorial will help you to install and configure Ansible on Ubuntu 20.04 LTS Linux systems.

Prerequisites

We have one control node to configure Ansible server and three node servers to be managed. Here control node is running with Ubuntu 20.04 Linux system. First and third node is running with Ubuntu 18.04 server and Second node is running with centos 7 server.

Here is list of nodes with IP address and hostnames:

  • Control node – 10.0.1.10 (control-node)
  • First node – 10.0.1.101 (web-host1)
  • Second node – 10.0.1.102 (web-host2)
  • Third node – 10.0.1.103 (db-host1)
  • Step 1 – Setup SSH Keys

    You can configure key based ssh for the remote Linux Ansible hosts. So password will not be required for SSH. Ansible also allows you to use a password for ssh, but key-based ssh is more secure.

    Login to the control node (10.0.1.10) and generate ssh key pair:

    ssh-keygen -t rsa 
    

    Just press “Enter” to all the input asked by the command.

    Copy the public key to all your remote nodes you need to connect via with SSH protocols.

    ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] 
    ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] 
    ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] 
    

    Step 2 – Installing Ansible on Ubuntu

    You can install Ansible server from official packages repositories on Ubuntu system. Which has the latest debian packages. Execute the following command to setup Ansible PPA on your Ubuntu system.

    sudo apt-add-repository ppa:ansible/ansible 
    

    Software Updater utility will update the packages cache on your system. So you have to run the following command to install or update Ansible on your Ubuntu system

    sudo apt update 
    sudo apt install ansible 
    

    Enter ‘Y’ for all the installation confirmation to complete install process. Next, you need to configure Ansible server

    Step 3 – Configure Inventory File

    Your server is ready with Ansible for remote host management and automation. You can have a number of hosts you need and manage them with single Ansible server.

    Here you need to define your remote systems in Ansible hosts file (/etc/ansible/hosts). You can also make groups of hosts with similar types. Here you need to properly organize your hosts into groups. Groups are used for performing one task on all remote hosts defined under it.

    Edit Ansible hosts configuration file. For exmaple:

    sudo nano /etc/ansible/hosts 
    

    Add your hosts and organize them with groups. A host can be added under multiple groups.

    [webservers]
    web-host1
    web-host2
    
    [dbservers]
    db-host1
    

    The below image will help you to understand group and hosts under a group.

    Ansible Group and Hosts

    Single Host Vars Configuration

    You need to define settings for your hosts. The host-specific file must be with the same name as host (eg: web-host1) under the host_vars directory.

    sudo mkdir /etc/ansible/host_vars/ 
    sudo vi /etc/ansible/host_vars/web-host1 
    

    Add the SSH settings to this file for the web-host1.

    ansible_ssh_host: 10.0.1.101
    ansible_ssh_port: 22
    ansible_ssh_user: root
    

    Ansible Host Settings

    In case you don’t have used Step 1 for the ssh connection for this host. You can also add one of the below methods to web-hosts1 configuration file for the authentication.

    ansible_ssh_pass: secret_password
    ansible_ssh_private_key_file: /home/rahul/.ssh/aws.pem
    

    Group Vars Configuration

    You can configure common variable settings of a Group under group configurations. The group file name must be same as the group name (eg: webservers) under group_vars directory.

    sudo mkdir /etc/ansible/group_vars 
    sudo vi /etc/ansible/group_vars/webservers 
    

    Add the common variables to this file used by all the hosts added under this group.

    ansible_ssh_port: 22
    ansible_ssh_user: root
    

    Step 4 – Testing Ansible Connection

    Your Ansible is ready to use. To test all nodes connectivity use ping module. Login to your Ansible server and execute following command:

    ansible -m ping all 
    

    You can also test connectivity for the specific host or groups.

    ansible -m ping web-host1        ## Specific host 
    ansible -m ping webservers        ## Specific group 
    

    You can also run any Linux command using the Ansible shell module. For example, execute the below command to test the free memory on web-host1.

    ansible -m shell -a 'free -m' web-host1 
    

    Install Ansible on Ubuntu

    You can also perform the same task for a group. Just use group name instead of hostname.

    Conclusion

    In this tutorial, you have learned to install Ansible server on Ubuntu 20.04 system. Also configured remote hosts to be managed with Ansible server.

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy