I’ll be using the post to journal as I learn ansible. I hope to reduce my need to ssh into remote machines and this is my journey

As I start, Im using ansible version This is mainly because of a project I’m contributing to that requires this version of ansible. Therefore some features that I use are deprecated in later version of ansible so please take note.

Beginning (2018-11-10)

$ ansible --version
  config file = 
  configured module search path = Default w/o overrides
  • You can use ssh-agent to avoid entering passwords to unlock the ssh keys
imela@whiscardz /etc/ansible $ ssh-agent bash
imela@whiscardz /etc/ansible $ ssh-add ~/.ssh/id_rsa
Enter passphrase for /home/imela/.ssh/id_rsa:
Identity added: /home/imela/.ssh/id_rsa (/home/imela/.ssh/id_rsa)
  • To run an adhoc command then add the -u to specify the user you are using to ssh
$ ansible all -a "/bin/echo hello" -u imela
  • You can define hosts in the /etc/ansible/hosts file. You can also define it somewhere else and then use the -i parameter to specify the location
Copying files
  • After defining the host above, you can then copy files from the host to the destination:
$ ansible biz1 -m copy -a "src=/home/imela/Documents/mans/comparing-ossim-to-usm.pdf dest=/tmp/copied.pdf" -u imela
  • You can also define the permissions of the destination file use mode:
$ ansible biz1 -m copy -a "src=/tmp/copyfile.txt dest=/tmp/ mode=644" -u imela
  • If you need sudo in order to perform a function, for instance changing the owner and group then add the following options –sudo –ask-sudo-pass and then you will be asked to enter the sudo password.
$ ansible biz1 -m copy -a "src=/tmp/copyfile.txt dest=/tmp/ mode=644 owner=admin group=admin" -u imela --sudo  --ask-sudo-pass
  • Check if a service has been started
$ ansible biz1 -m service -a "name=apache2 state=started" -u imela --sudo --ask-sudo-pass
  • Stop a service
$ ansible biz1 -m service -a "name=apache2 state=stopped" -u imela --sudo --ask-sudo-pass
  • Restart a service
$ ansible biz1 -m service -a "name=apache2 state=restarted" -u imela --sudo --ask-sudo-pass


How ansible works

Getting started

Working with Inventory

Ansible Configuration settings

AdHoc Commands

Install previous ansible (2.2.3) release on ubuntu 16.04


I have some ansible playbooks that only work on ansible 2.2.3 which is not on the list releases on the ubuntu repository or the ansible repo.

Here’s how to check the versions of ansible that are available on the ubuntu 16.04 repo:

imela@curtsey ~ $ apt-cache policy ansible
 Installed: (none)
 Version table: 500
 500 xenial-backports/universe amd64 Packages
 500 xenial-backports/universe i386 Packages 500
 500 xenial-updates/universe amd64 Packages
 500 xenial-updates/universe i386 Packages 500
 500 xenial/universe amd64 Packages
 500 xenial/universe i386 Packages

If you want to install the latest release of ansible then add the following repo:

sudo apt-add-repository ppa:ansible/ansible


Im going to use the python pi pypi repository

  • Install the required dependencies

imela@curtsey ~ $ sudo apt install libffi-dev python-dev libssl-dev
[sudo] password for imela:
Reading package lists... Done
Building dependency tree
Reading state information... Done
libssl-dev is already the newest version (1.0.2g-1ubuntu4.10).
The following additional packages will be installed:
libexpat1-dev libpython-dev libpython2.7-dev python2.7-dev
The following NEW packages will be installed:
libexpat1-dev libffi-dev libpython-dev libpython2.7-dev python-dev python2.7-dev

sudo apt-get install sshpass -y

  • Upgrade pip and install ansible pre-requisites :

imela@curtsey ~ $ sudo -H pip install pip --upgrade

imela@curtsey ~ $ sudo -H pip install setuptools --upgrade

sudo -H pip install pyopenssl ndg-httpsclient pyasn1

sudo -H pip install passlib

  • Install ansible

imela@curtsey ~ $ sudo -H pip install ansible==


Installing Ansible 2.2 on Ubuntu 16.04

Ansible: Installing Ansible on Ubuntu 16.04

Setting Up Ansible for Configuration Management on Ubuntu 14.04: Installing Ansible

How to Setup Ansible on Ubuntu 16.04