Ansible

Intro

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 2.2.3.0 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
ansible 2.2.3.0
  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
Hosts
  • 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
Services
  • 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

References

How ansible works

Getting started

Working with Inventory

Ansible Configuration settings

AdHoc Commands

Install previous ansible (2.2.3) release on ubuntu 16.04

Problem

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
ansible:
 Installed: (none)
 Candidate: 2.1.1.0-1~ubuntu16.04.1
 Version table:
 2.1.1.0-1~ubuntu16.04.1 500
 500 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages
 500 http://archive.ubuntu.com/ubuntu xenial-backports/universe i386 Packages
 2.0.0.2-2ubuntu1 500
 500 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages
 500 http://archive.ubuntu.com/ubuntu xenial-updates/universe i386 Packages
 2.0.0.2-2 500
 500 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages
 500 http://archive.ubuntu.com/ubuntu 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

Solution

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 2.2.3.0

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

References

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