pyVision
A Machine Learning and Signal Processing toolbox

Download .zip Download.tar.gz View on GitHub


How to Install and Configure Ansible on Ubuntu 16.04

Introduction

Configuration management systems are designed to make controlling large numbers of servers in easy for administrators and operations teams. They allow you to control many systems from one central location using automated way.

Ansible works by configuring client machines from an computer with Ansible components installed and configured. Any server that has an SSH port exposed can configured by Ansible.Ansible can interact with clients through either command line tools or through its configuration scripts called Playbooks.

Step 1 — Installing Ansible

Run the following commands to run ansible

sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible

** Step 2 - Configuring Ansible Hosts**

ansible keeps track of the servers it controlls through a host/inventory files.The host/inventory file can be found at /etc/ansible/hosts

Syntax of the host file is as follows

[group_name]
alias ansible_ssh_host=your_server_ip

The group_name is an organizational tag that lets you refer to any servers listed under it with one word. The alias is just a name to refer to that server.

For example

[dev]
beta_server ansible_ssh_host=192.0.2.1

If the ssh is not running on the default port

[dev]
beta_server ansible_port=5555 ansible_host=192.0.2.50

We can also specify ssh user

[dev]
beta_server ansible_port=5555 ansible_host=192.0.2.50 ansible_user=root

To add a lot of hosts we can also specify patterns

[webservers]
www[01:50].example.com

Hosts can be in multiple groups and groups can configure parameters for all of their members.

There are a host of other options like ansible_ssh_private_key_file,ansible_ssh_pass,ansible_become which can be found at http://docs.ansible.com/ansible/intro_inventory.html#hosts-and-groups

Host Variables

The host_vars directory can be used to store host variables that can later be used in configuration scripts ansible playbooks

To create a host file for beta_server

mkdir /etc/ansible/host_vars
sudo vi /etc/ansible/host_vars/beta_server

you can place the configuration parameters in this file

---
ansible_ssh_user:  root
ansible_port: 5555 
ansible_host: 192.0.2.50

Similariy we can set configuration parameters for group by using group variables

sudo mkdir /etc/ansible/group_vars
sudo nano /etc/ansible/group_vars/dev

Since ansible connects to client machine via ssh . It requires information regarding ssh user and password on the client machine

We can configure ansible to user a specific user for all the servers in the dev group

The contents of the file are

---
ansible_ssh_user:  root
ansible_port: 5555 
ansible_host: 192.0.2.50

Instead of creating files,you can create directories named after group or hosts and ansible will read all the files within the directory

Now let us assume you have ssh private key which enables you to take ssh sessions on respective server

ssh -i access_key.pem user@hostname

For steps to create the private key refer to the following link

Let us configure the host variables for the beta server by creating sh_config file in the directory /etc/ansible/host_vars/dev2

---

ansible_ssh_private_key_file: /etc/ansible/a.pem
ansible_host: 172.17.0.2
ansible_port: 22
ansible_ssh_user: root

Following is the entry in the /etc/ansible/hosts file

[dev-servers]
dev2

To test the communication with the client run the command

root@pi-dektop:/home/pi/ansible# ansible -m ping dev2
dev2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

References

  • https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-ansible-on-ubuntu-16-04
  • http://docs.ansible.com/ansible/intro_inventory.html#hosts-and-groups
blog comments powered by Disqus