Table of Contents
In this tutorial, we will go through the steps to install Ansible on Ubuntu Server 18.04. Ansible is an open-source software provisioning, configuration management, and application-deployment tool. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. We need to start with the prerequisites and make sure we have all the resources available to Install Ansible on Ubuntu.
How to Install Ansible on Ubuntu 18.04 with Best Practices
Also Read: How to Install Gazebo on Ubuntu 20.04 LTS (Focal Fossa)
In our example, we will use one Ansible server and one Node.
Server IP: 192.168.0.104
Node IP: 192.168.0.106
Step 1: Prerequisites
You need running Ubuntu 18.04 and python must be installed in your system before you follow the below steps to Install Ansible on Ubuntu. You can use below command to install Python if not already installed.
test@test-VirtualBox:~$ sudo apt-get install python -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
python is already the newest version (2.7.15~rc1-1).
python set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 230 not upgraded.
Step 2: Add the Repository
First you need to add the Ansible repository.
test@test-VirtualBox:~$ sudo apt-add-repository ppa:ansible/ansible
[sudo] password for test:
Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.
http://ansible.com/
More info: https://launchpad.net/~ansible/+archive/ubuntu/ansible
Press [ENTER] to continue or Ctrl-c to cancel adding it.
Hit:1 http://in.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu bionic-backports InRelease
Get:4 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic InRelease [15.9 kB]
Get:5 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:6 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic/main i386 Packages [692 B]
Get:7 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic/main amd64 Packages [692 B]
Get:8 http://security.ubuntu.com/ubuntu bionic-security/main amd64 DEP-11 Metadata [38.5 kB]
Get:9 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic/main Translation-en [472 B]
Get:10 http://security.ubuntu.com/ubuntu bionic-security/main DEP-11 48x48 Icons [17.6 kB]
Step 3: Update the System
Now update the system once using sudo apt-get update command as shown below.
test@test-VirtualBox:~$ sudo apt-get update
Hit:1 http://in.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:4 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic InRelease
Hit:5 http://security.ubuntu.com/ubuntu bionic-security InRelease
Reading package lists... Done
Step 4: Install Ansible
Once system is updated, you can run sudo apt-get install ansible command to install Ansible on ubuntu.
test@test-VirtualBox:~$ sudo apt-get install ansible Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libpython-stdlib libpython2.7 libpython2.7-minimal libpython2.7-stdlib python python-asn1crypto python-cffi-backend python-crypto python-cryptography python-enum34 python-httplib2 python-idna python-ipaddress python-jinja2 python-markupsafe python-minimal python-paramiko python-pkg-resources python-pyasn1 python-setuptools python-six python-yaml python2.7 python2.7-minimal sshpass Suggested packages: python-doc python-tk python-crypto-doc python-cryptography-doc python-cryptography-vectors python-enum34-doc python-jinja2-doc python-gssapi python-setuptools-doc python2.7-doc binfmt-support The following NEW packages will be installed: ansible libpython-stdlib python python-asn1crypto python-cffi-backend python-crypto python-cryptography python-enum34 python-httplib2 python-idna python-ipaddress python-jinja2 python-markupsafe python-minimal python-paramiko python-pkg-resources python-pyasn1 python-setuptools python-six python-yaml python2.7 python2.7-minimal sshpass The following packages will be upgraded: libpython2.7 libpython2.7-minimal libpython2.7-stdlib 3 upgraded, 23 newly installed, 0 to remove and 230 not upgraded. Need to get 12.4 MB of archives. After this operation, 71.4 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython2.7 amd64 2.7.15-4ubuntu4~18.04.2 [1,053 kB] Get:2 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic/main amd64 ansible all 2.9.2-1ppa~bionic [5,778 kB] Get:3 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython2.7-stdlib amd64 2.7.15-4ubuntu4~18.04.2 [1,916 kB] Get:4 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython2.7-minimal amd64 2.7.15-4ubuntu4~18.04.2 [336 kB] Get:5 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 python2.7-minimal amd64 2.7.15-4ubuntu4~18.04.2 [1,296 kB] ............................................
Step 5: Generate Keys
Once the Ansible installation completed, you need to generate the public/private key for authentication with the nodes that you want to add.
test@test-VirtualBox:~$ sudo ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:HyLBz4axoGvnJLp7nVy4DgW38hyTJmExhN9KYJXV4mg root@test-VirtualBox
The key's randomart image is:
+---[RSA 2048]----+
| o=.o.. |
|o. + o . |
|.o+.= = |
| .oE.= B |
| .=.B.+ S . |
| .O.o.o o . |
| =o=+ . |
| o.*= |
|++ .o |
+----[SHA256]-----+
Step 6: Copy the Key to the nodes
Once the public key is generated, you can copy the public key to the nodes either by manually copying or using ssh-copy-id <node ip> command.
root@test-VirtualBox:~# ssh-copy-id 192.168.0.106
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.0.106's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.0.106'"
and check to make sure that only the key(s) you wanted were added.
Step 7: Add the nodes
Once the public is copied to the remote host, you need to add those nodes in /etc/ansible/hosts
under one new section that you want to use. For this example, I am using [testservers] section.
#cat /etc/ansible/hosts
[testservers]
192.168.0.106
Step 8: Test the Configuration
Once the set up is done, it's time to test the set up by running a basic ping command. If you are able to see SUCCESS message then all is well. Congratulations Ansible is installed successfully. You can now write your playbooks.
root@test-VirtualBox:~# ansible -m ping all 192.168.0.106 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" }
Also Read: How to install and configure kubernetes
Reference: Ansible Documentation