How to Install and Configure latest version of Ansible on Ubuntu Linux

Posted on in Categories , , last updated October 21, 2017

I am a new Ubuntu Linux user. How do I install Ansible on a Ubuntu Linux 16.04 LTS or 17.10 desktop control machine?

Ansible is a simple and easy to use IT automation tool. One can use Ansible to deploy applications and systems/VM/containers. Ansible configuration is written in plain English and works on the remote/local server using SSH. No agents needed on the remote server.
howto install ansible on ubuntu linux 16.04 17.10
In this guide, you will install Ansible on a Ubuntu Linux 16.04 LTS or 17.10 desktop system and learn some basics of how to use the software for automation purpose.

Installation

You must configure the PPA on your system to install the latest version of ansible. To manage the repositories that you install software from various PPA (Personal Package Archives). It allow you to upload Ubuntu source packages to be built and published as an apt repository by Launchpad. Type the following apt-get command or apt command:
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install software-properties-common

Next add ppa:ansible/ansible to your system’s Software Source:
$ sudo apt-add-repository ppa:ansible/ansible

 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.
 
gpg: keybox '/tmp/tmp6t9bsfxg/pubring.gpg' created
gpg: /tmp/tmp6t9bsfxg/trustdb.gpg: trustdb created
gpg: key 93C4A3FD7BB9C367: public key "Launchpad PPA for Ansible, Inc." imported
gpg: Total number processed: 1
gpg:               imported: 1
OK

Update your repos:
$ sudo apt update
Sample outputs:

Ign:1 http://dl.google.com/linux/chrome/deb stable InRelease
Hit:2 http://dl.google.com/linux/chrome/deb stable Release                                                                            
Get:4 http://in.archive.ubuntu.com/ubuntu artful InRelease [237 kB]                                                                                  
Hit:5 http://security.ubuntu.com/ubuntu artful-security InRelease             
Get:6 http://ppa.launchpad.net/ansible/ansible/ubuntu artful InRelease [15.9 kB]   
Get:7 http://ppa.launchpad.net/ansible/ansible/ubuntu artful/main amd64 Packages [560 B]
Get:8 http://in.archive.ubuntu.com/ubuntu artful-updates InRelease [65.4 kB]                    
Hit:9 http://in.archive.ubuntu.com/ubuntu artful-backports InRelease                 
Get:10 http://ppa.launchpad.net/ansible/ansible/ubuntu artful/main i386 Packages [560 B]
Get:11 http://ppa.launchpad.net/ansible/ansible/ubuntu artful/main Translation-en [340 B]             
Fetched 319 kB in 5s (62.3 kB/s)         
Reading package lists... Done

To install the latest version of ansible, enter:
$ sudo apt install ansible
Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  python-crypto python-httplib2 python-jinja2 python-markupsafe python-paramiko python-pyasn1 python-setuptools python-yaml sshpass
Suggested packages:
  python-crypto-dbg python-crypto-doc python-jinja2-doc python-gssapi python-setuptools-doc
The following NEW packages will be installed:
  ansible python-crypto python-httplib2 python-jinja2 python-markupsafe python-paramiko python-pyasn1 python-setuptools python-yaml sshpass
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 3,610 kB of archives.
After this operation, 27.3 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu artful/main amd64 python-markupsafe amd64 1.0-1build1 [13.0 kB]
Get:2 http://ppa.launchpad.net/ansible/ansible/ubuntu artful/main amd64 ansible all 2.4.0.0-1ppa~artful [2,669 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu artful/main amd64 python-jinja2 all 2.9.6-1 [107 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu artful/main amd64 python-yaml amd64 3.12-1build2 [115 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu artful/main amd64 python-pyasn1 all 0.1.9-2 [45.4 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu artful/main amd64 python-paramiko all 2.0.0-1 [109 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu artful/main amd64 python-httplib2 all 0.9.2+dfsg-1 [34.6 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu artful/main amd64 python-crypto amd64 2.6.1-7build2 [245 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu artful/main amd64 python-setuptools all 36.2.7-2 [260 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu artful/universe amd64 sshpass amd64 1.06-1 [10.5 kB]
Fetched 3,610 kB in 23s (154 kB/s)                                                                                                                   
Selecting previously unselected package python-markupsafe.
(Reading database ... 193191 files and directories currently installed.)
Preparing to unpack .../0-python-markupsafe_1.0-1build1_amd64.deb ...
Unpacking python-markupsafe (1.0-1build1) ...
Selecting previously unselected package python-jinja2.
Preparing to unpack .../1-python-jinja2_2.9.6-1_all.deb ...
Unpacking python-jinja2 (2.9.6-1) ...
Selecting previously unselected package python-yaml.
Preparing to unpack .../2-python-yaml_3.12-1build2_amd64.deb ...
Unpacking python-yaml (3.12-1build2) ...
Selecting previously unselected package python-pyasn1.
Preparing to unpack .../3-python-pyasn1_0.1.9-2_all.deb ...
Unpacking python-pyasn1 (0.1.9-2) ...
Selecting previously unselected package python-paramiko.
Preparing to unpack .../4-python-paramiko_2.0.0-1_all.deb ...
Unpacking python-paramiko (2.0.0-1) ...
Selecting previously unselected package python-httplib2.
Preparing to unpack .../5-python-httplib2_0.9.2+dfsg-1_all.deb ...
Unpacking python-httplib2 (0.9.2+dfsg-1) ...
Selecting previously unselected package python-crypto.
Preparing to unpack .../6-python-crypto_2.6.1-7build2_amd64.deb ...
Unpacking python-crypto (2.6.1-7build2) ...
Selecting previously unselected package python-setuptools.
Preparing to unpack .../7-python-setuptools_36.2.7-2_all.deb ...
Unpacking python-setuptools (36.2.7-2) ...
Selecting previously unselected package sshpass.
Preparing to unpack .../8-sshpass_1.06-1_amd64.deb ...
Unpacking sshpass (1.06-1) ...
Selecting previously unselected package ansible.
Preparing to unpack .../9-ansible_2.4.0.0-1ppa~artful_all.deb ...
Unpacking ansible (2.4.0.0-1ppa~artful) ...
Setting up python-setuptools (36.2.7-2) ...
Setting up python-yaml (3.12-1build2) ...
Setting up python-crypto (2.6.1-7build2) ...
Setting up python-pyasn1 (0.1.9-2) ...
Setting up python-markupsafe (1.0-1build1) ...
Setting up python-httplib2 (0.9.2+dfsg-1) ...
Setting up python-paramiko (2.0.0-1) ...
Setting up sshpass (1.06-1) ...
Processing triggers for doc-base (0.10.7) ...
Processing 1 added doc-base file...
Processing triggers for man-db (2.7.6.1-2) ...
Setting up python-jinja2 (2.9.6-1) ...
Setting up ansible (2.4.0.0-1ppa~artful) ...

Finding out Ansible version

Type the following command:
$ ansible --version
Sample outputs:

ansible 2.4.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/vivek/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.14 (default, Sep 23 2017, 22:06:14) [GCC 7.2.0]

Creating your hosts file

Ansible needs to know your remote server names or IP address. This information is stored in a file called hosts. The default is /etc/ansible/hosts. You can edit this one or create a new one in your $HOME directory:
$ sudo vi /etc/ansible/hosts
Or
$ vi $HOME/hosts
Append your server’s DNS or IP address:

[webservers]
server1.cyberciti.biz
104.20.187.5
104.20.186.5

[devservers]
10.98.222.4
10.98.222.7
10.98.222.8

I have two groups. The first one named as webserver and other is called devservers.

Setting up ssh keys

You must configure ssh keys between your machine and remote servers specified in ~/hosts file:
$ ssh-keygen -t rsa -b 4096 -C "My ansisble key"
Use scp or ssh-copy-id command to copy your public key file (e.g., $HOME/.ssh/id_rsa.pub) to your account on the remote server/host:
$ ssh-copy-id -i $HOME/.ssh/id_rsa.pub [email protected]
$ ssh-copy-id -i $HOME/.ssh/id_rsa.pub [email protected]
$ ssh-copy-id -i $HOME/.ssh/id_rsa.pub [email protected]
$ ssh-copy-id -i $HOME/.ssh/id_rsa.pub [email protected]
$ eval $(ssh-agent)
$ ssh-add

Now ansible can talk to all remote servers using ssh command.

Send ping server to all servers

Just type the following command:
$ ansible -i ~/hosts -m ping all
Sample outputs:

10.98.222.4 | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "ping": "pong"
}
10.98.222.7 | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "ping": "pong"
}
10.98.222.8 | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "ping": "pong"
}

Find out uptime for all hosts

$ ansible -i hosts -m shell -a 'uptime' all
Sample outputs:

do-de.public | SUCCESS | rc=0  
 10:37:02 up 1 day,  8:39,  1 user,  load average: 0.95, 0.27, 0.12
 
do-blr-vpn | SUCCESS | rc=0 
 16:07:11 up 1 day,  8:43,  1 user,  load average: 0.01, 0.01, 0.00
 
ln.gfs01 | SUCCESS | rc=0  
 10:37:17 up 22 days,  5:30,  1 user,  load average: 0.18, 0.12, 0.05

Where,

  1. -i ~/hosts : Specify inventory host path. You can setup shell variable and skip the -i option. For e.g.: export ANSIBLE_HOSTS=~/hosts
  2. -m shell : Module name to execute such as shell, apt, yum and so on
  3. -a 'uptime' : Module arguments. For example, shell module will accept Unix/Linux command names. The apt module will accept options to update remote boxes using apt-get/apt command and so on.
  4. all : The all means “all hosts.” You can speificy group name such as devservers (ansible -i hosts -m shell -a 'uptime' dbservers) or host names too.

Update all Debian/Ubuntu server using apt module

Run the following command:
$ ansible -i ~/hosts -m apt -a 'update_cache=yes upgrade=dist' dbservers

Writing your first playbook

You can combine all modules in a text file as follows in yml format i.e. create a file named update.yml:

---
- hosts: dbservers
  tasks:
          - name: Updating host using apt
            apt:
                    update_cache: yes
                    upgrade: dist

Now you can run it as follows:
$ ansible-playbook -i ~/hosts update.yml

Fig.01: Ansible playbook in action
Fig.01: Ansible playbook in action

That is all. Now you have a working ansible setup running on Ubuntu desktop/laptop and communication with remote servers. I suggest you learn how to write playbook and other stuff in the official documents.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 1 comments/add one below):

1 comment

  1. Nice tutorial. On your update section that uses apt, you need to pass the -b and -K switches to become root and ask for the SUDO password. Otherwise the command will fail unless you are sshing in as root, which I wouldn’t recommend.

Leave a Comment