Sign in

Deploy Monitoring Tool Prometheus Using Ansible

What is Monitoring?

The Periodic tracking (for example, daily, weekly, monthly, quarterly, annually) of any activity’s progress by systematically gathering and analyzing data and information is called Monitoring. The target audience/beneficiaries must be defined along with what you are doing, and whether your activities are being implemented as planned or not.

Let’s take a simple example of monitoring..

Let’s say we have a huge system where we have Switch router, Firewall, Linux/Windows OS, DataBase Server, WebApp, and many other things are connected together, now here how can we check that everything is working perfectly or not if any system goes down how we will get to know about that.

So I want something who keeps on checking all the systems, wants to keep an eye everywhere if anything happens they should notify us. For this we have to do monitoring, this is the simple meaning of monitoring.

How monitoring can help us;

  • Helps in investing in the issues.
  • Gave alerts for any misbehavior activities.
  • Get to know about the problem,
  • help us to visualize in a proper way,…

now we know what is monitoring, now understand how we can do the monitoring. So here in this article, I will tell you about one great monitoring tool i.e., Prometheus.

What is Prometheus ?

Prometheus is an open-source system monitoring and alerting toolkit originally built at SoundCloud. Since its inception in 2012, many companies and organizations have adopted Prometheus, and the project has a very active developer and user community. It is now a standalone open source project and maintained independently of any company.

Prometheus is just a tool who collects the metrics of the system, metrics are just a way to collect information about resources, for example like CPU uses, RAM uses, this information we can collect is known as metrics.


Prometheus’s main features are:

  • a multi-dimensional data model with time series data identified by metric name and key/value pairs
  • PromQL, a flexible query language to leverage this dimensionality
  • no reliance on distributed storage; single server nodes are autonomous
  • time-series collection happens via a pull model over HTTP
  • Pushing time series is supported via an intermediary gateway
  • targets are discovered via service discovery or static configuration
  • multiple modes of graphing and dashboarding support

Now we understand what is monitoring and Prometheus. So, let’s start with how we can set up Prometheus using ansible.

steps to setup Prometheus using ansible ;

Step 1 :

  • Install ansible and do the full configuration of ansible
  • then create a playbook for setup Prometheus

Step 2 : Install Prometheus

Let’s start creating a playbook

In this step I have Installed Prometheus with some specific version, here I used unarchive command to directly install and extract the file in the same step otherwise we have to first download it then we have extract this file.

Step 3: Copy Prometheus file to the bin, there is the specific use of this file that’s why we have to keep this file in the bin, I will discuss this later

step 4: Copy Prometheus configuration file ;

step 5: After this we have to start the Prometheus service, to start the service we have systemctl in Linux, but for Prometheus, systemctl is not by default available. so, we have to create systemctl for Prometheus by ourselves.

Let’s see how to create a systemd unit file in Linux for any of the tools;

  • First copy the executable file to /usr/local/bin folder,
  • make sure this file is executable,
  • then go to /etc/systemd/system folder and create service for Prometheus like;

Then systemctl daemon-reload and systemctl start service name to start the service. This is the way to create a systemd unit file in Linux.

Step 6: copy systemd service file and start the service

Step 7: Now I am checking if I can access Prometheus or not, port of Prometheus is 9090;

Prometheus server is deployed successfully using ansible.

With all this, we have our Prometheus server up and running and collecting data from its own instance.

But to collect data how my Prometheus server will know that from which node or system I have to collect data who is gonna tell this to the Prometheus server, for this there is one agent who tells the Prometheus server to collect data from us, and that agent is known as node-exporter.

We have to install node-exporter in every node from where we want to collect the metrics and also need to tell about that system IP with node-exporter port in Prometheus conf file.

Now let’s see how we can do this using Ansible, again the same step as Prometheus...

step1: download and install node-exporter

step 2: Copy file to the bin folder

step 3: Create systemd unit file for node-exporter

step 4: copy systemd service file and start the service

step 5: Now I am checking if I can access node-exporter or not, port of Prometheus is 9100;

That’s all our server setup is completed successfully, now go to Prometheus dashboard and check if everything is working great or not …


Thanks for reading this article,

here you can find this code on my Github :