DEV Community

ERINFOLAMI PETER
ERINFOLAMI PETER

Posted on

Basic Ansible tutorial (Installation and setup)

Introduction

In the last article we talked about the power of ansible, it's importance and why you should consider using it in your company or on your next big project if you haven't been using it. Ansible is a configuration management tool that streamlines the process of controlling large number of servers. Ansible is just one of the other configuration tool out there, some of which includes puppet, chef.
In this tutorial we’ll discuss how to install Ansible on your machine (for this tutorial we'll be using a linux environment, ubuntu) and go over some basics of how to use this software.

Prerequisites

Before installing ansible you must have An Ansible Control Node which is the machine we'll install ansible on to configure other servers. The machine should have a non-root user with sudo privileges.

Installation

To install on ubuntu machine add the project official's PPA

sudo apt-add-repository ppa:ansible/ansible
Enter fullscreen mode Exit fullscreen mode

Then update

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Install ansible

sudo apt install ansible
Enter fullscreen mode Exit fullscreen mode

You can also install ansible via python (This works for any OS).

Make sure you have python installed on your machine then run the following to install ansible

python3 -m pip install --user ansible
Enter fullscreen mode Exit fullscreen mode

Use python for windows.

We've successfully installed ansible on our machine

Using ansible

Now let's use ansible!, one of the simplest way is first by creating an inventory file in your working directory.

touch inventory
Enter fullscreen mode Exit fullscreen mode

in the inventory file, write the ip address or the domain name of the ansible hosts (machines you want to remotely access). For testing purposes you can input the ip address of your current machine with no space or just localhost.
NOTE: The ansible hosts(machines ansible want to run commands remotely) must accept ssh connection and also must have set up the required ssh key, if you are using your local machine as the ansible host for testing purposes, you might need to add the public key of your machine to authorized_keys so ssh via identity-file can work

Then Run

ceejay@ceejay:~/ceejay/ansible_practice$ ansible all --key-file ~/.ssh/id_rsa -i inventory -m ping

localhost | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python3"

    },

    "changed": false,

    "ping": "pong"

}

Enter fullscreen mode Exit fullscreen mode

-i - accepts the inventory file.
-m - The ansible module to use. There a lot of ansible modules, you can check them here. The ping module is basically just to test if ansible is able to ssh into the server.

You can also create an ansible config file ansible.cfg to define the basic configurations. Create this file in your working directory, this would override the default config file in /etc/ansible.
in the ansible.cfg file copy paste the following

[defaults]

inventory = inventory

private_key_file = ~/.ssh/id_rsa

Enter fullscreen mode Exit fullscreen mode

Save this file and run

ceejay@ceejay:~/ceejay/ansible_practice$ ansible all -m ping

localhost | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python3"

    },

    "changed": false,

    "ping": "pong"

}

Enter fullscreen mode Exit fullscreen mode

This method makes it cleaner and easier to run.

We've just seen how to use ansible, in the coming article we'll explore more possibilities with ansible, there's so much more powerful stuffs you can do with ansible.

Top comments (0)