How to Setup Ansible on Ubuntu 16.04

WebsiteInternetWeb Services

In this article, we will learn how to configure Ansible – an automation configuration management system. Anisble is a configuration management system which can control a large number of client machines with an easy administration, which can be automated from a central location.

Anisble communicates over SSH tunnels and it doesn’t need to install any software on the client machine and it retrieves the information from the remote ansible machines which issues the commands and copies the files.

The Anisble configuration files mainly uses the YAML data formation as it can be due to expressive which has similarity of popular languages. The clients can be communicated using the command line or with playbooks.

Prerequisites

  • We needed Ubuntu 16.04 and a non-root user with sudo permissions.
  • Needed SSH keys for the users.

Installing the Ansible onf Ubuntu 16.04

As we needed to install the Anisble software on one machine, Ansible is not available in the Ubuntu default repository, we will add the Anisble personal package archive to the system.

Below is the command to add the repository –

$ 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: keyring `/tmp/tmp8ffrbkin/secring.gpg' created
gpg: keyring `/tmp/tmp8ffrbkin/pubring.gpg' created
gpg: requesting key 7BB9C367 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmp8ffrbkin/trustdb.gpg: trustdb created
gpg: key 7BB9C367: public key "Launchpad PPA for Ansible, Inc." imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
OK

After we add the Ansible PPA to the local machine, we needed to update the Linux so that the package repository will be added to the machine.

$ sudo apt-get update –y
Hit:1 http://security.ubuntu.com/ubuntu xenial-security InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Get:3 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial InRelease [18.1 kB]
Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease
Hit:5 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease
Get:6 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial/main amd64 Packages [552 B]
Get:7 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial/main i386 Packages [552 B]
Get:8 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial/main Translation-en [340 B]
Fetched 19.5 kB in 1s (18.2 kB/s)
Reading package lists... Done

Once the package repository is updated, we will install the Ansible using the below command.

$ 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-minimal libpython2.7-stdlib libyaml-0-2 python python-crypto
python-ecdsa python-httplib2 python-jinja2 python-markupsafe python-minimal python-paramiko
python-pkg-resources python-setuptools python-six python-ya
l python2.7 python2.7-minimal
sshpass
Suggested packages:
python-doc python-tk python-crypto-dbg python-crypto-doc python-jinja2-doc
python-setuptools-doc python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
ansible libpython-stdlib libpython2.7-minimal libpython2.7-stdlib libyaml-0-2 python
python-crypto python-ecdsa python-httplib2 python-jinja2 python-markupsafe python-minimal
python-paramiko python-pkg-resources python-setuptools python-six python-yaml python2.7
python2.7-minimal sshpass
0 upgraded, 20 newly installed, 0 to remove and 12 not upgraded.
Need to get 6,145 kB of archives.
After this operation, 30.7 MB of additional disk space will be used.
…
…
…
…
Get:19 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 python-setuptools all 20.7.0-1 [169 kB]
Get:20 http://in.archive.ubuntu.com/ubuntu xenial/universe amd64 sshpass amd64 1.05-1 [10.5 kB]
Fetched 6,145 kB in 8s (761 kB/s)
Selecting previously unselected package libyaml-0-2:amd64.
(Reading database ... 91811 files and directories currently installed.)
Preparing to unpack .../libyaml-0-2_0.1.6-3_amd64.deb ...
Unpacking libyaml-0-2:amd64 (0.1.6-3) ...
Selecting previously unselected package libpython2.7-minimal:amd64.
Preparing to unpack .../libpython2.7-minimal_2.7.12-1~16.04_amd64.deb ...
Unpacking libpython2.7-minimal:amd64 (2.7.12-1~16.04) ...
Selecting previously unselected package python2.7-minimal.
Preparing to unpack .../python2.7-minimal_2.7.12-1~16.04_amd64.deb ...
Selecting previously unselected package python-setuptools.
Preparing to unpack .../python-setuptools_20.7.0-1_all.deb ...
Unpacking python-setuptools (20.7.0-1) ...
Setting up python-httplib2 (0.9.1+dfsg-1) ...
Setting up python-pkg-resources (20.7.0-1) ...
Setting up python-setuptools (20.7.0-1) ...
Setting up sshpass (1.05-1) ...
Setting up ansible (2.1.1.0-1ppa~xenial) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...

Ansible Hosts Configuring

We need to keep track of all the servers and clients from ‘hosts’ file, which we need to create the hosts file so that we can start the communication with the other client or server machines.

$ sudo nano /etc/ansible/hosts

When we open the configuration file we will see all the commented lines and none of the configuration in the files works we needed to add the below demo machines.

General Syntax for adding the host or client in the group adds.

[group_name]
Alias anisible_ssh_host=your_ansible_server_ip_address

Demo configuration file

[Ansible_server]
Client1 ansible_ssh_host=192.168.0.10
Client2 ansible_ssh_host=192.168.0.11

We needed to setup so that SSH keys are copied to all the client machines so that they are authorized without any password.

In our scenario we are using two client machines and all the client machines are accessible using the SSH keys without prompting for the password.

With the current configuration if we try to connect to the host with Ansible the command fails because the SSh keys are connected with the root user and we will see the below error

Client1 | UNREACHABLE! => {
   "changed": false,
   "msg": "Failed to connect to the host via ssh.",
   "unreachable": true
}

We will create a file which tells that the servers will connect using the root user of the client machines.

$ sudo mkdir /etc/ansible/group_vars
$ sudo vi /etc/ansible/group_vars/servers
---
Ansible_ssh_user: root

We have to put the “—“ in the starts of the YAML file.

If you want to specify all the servers at a place we needed to provide those details at /etc/ansible/group_vars/all.

Executing the Simple Ansible Commands

To test our configuration we run the below command which will ping all the clients in the configuration file.

$ ansible –m ping all
Client1 | SUCCESS => {
   "changed": false,
   "ping": "pong"
   }
   Client2 | SUCCESS => {
      "changed": false,
      "ping": "pong"
   }

We can also ping the individual clients with the below example.

If we want to ping the whole group we will use the below command

$ ansible –m ping servers

If we want to ping a single client or machine we will use the below command

$ ansible –m ping clien1

We can use the shell module to run a terminal command from the Anisble to the client.

$ ansible –m shell –a ‘df –h’ client1
Client1 | SUCCESS | rc=0 >>
df -h
Filesystem Size Used Avail Use% Mounted on
udev 973M 0 973M 0% /dev
tmpfs 199M 5.9M 193M 3% /run
/dev/mapper/ubuntu16--vg-root 18G 1.7G 15G 11% /
tmpfs 992M 0 992M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 992M 0 992M 0% /sys/fs/cgroup
/dev/sda1 472M 101M 347M 23% /boot
tmpfs 100K 0 100K 0% /run/lxcfs/controllers
tmpfs 199M 0 199M 0% /run/user/1000

In the above article we have learned how to install and configure the ansible, and configure the clients, communicate with the client or servers and run simple commands and tasks so that we can test the configuration and execute the simple tasks remotely. We will cover the Playbooks in the further articles

raja
Published on 23-Jan-2020 14:22:21
Advertisements