Skip to main content

Command Palette

Search for a command to run...

Prometheus Installation on Linux using Ansible-playbook

Installation of Prometheus on AWS EC2- Linux using Ansible

Updated
2 min read
Prometheus Installation on Linux using Ansible-playbook

Introduction :

Installation of Prometheus on different instances is very time-consuming. Instead of manual installation, we make it easy using Ansible-playbook.

Tools:

  • AWS Account

  • Ansible

Getting your hands dirty!

  • First, create an AWS ec-2 instance with a Linux image and create a new or add an existing security group. After creation make sure you can log in using a security key (make sure your ssh port 22 is open). Add port "9090" for the Prometheus server.

  • Add created Linux instance IP into an Ansible hosts file. I use [prometeus] as a hostname in the inventory file.

    In ansible_ssh_private_key_file we have to specify the full path of the inventory file.

      [prometheus]
      13.232.52.98 ansible_user=ec2-user ansible_ssh_private_key_file=/home/Downloads/mydtkey.pem
    
  • You can check using the below code whether your host is reachable or not :

      ansible all -m ping -i /home/Downloads/Ansible_files/hosts.in
    
  • Ansible-playbook for installation and configuration for systemd "Prometheus.service" :

    promtheus_install.yaml :

      - hosts:  prometheus
        user:   ec2-user
        become: yes
    
        tasks:
          - name: change ownership to ec2-user
            command: chown -R ec2-user:ec2-user /opt  
    
          - name: Download prometheus archive #dowloading prometheus and unarchive 
            unarchive: 
              src: https://github.com/prometheus/prometheus/releases/download/v2.37.8/prometheus-2.37.8.linux-amd64.tar.gz
              dest: /opt
              remote_src: yes
              owner: ec2-user
    
          - name: change name of extracted folder 
            command: "mv /opt/prometheus-2.37.8.linux-amd64  /opt/prometheus"
    
          - name: copy prometheus to /usr/local/bin/ #copy folder to bin for execution
            copy: 
              src: /opt/prometheus/prometheus
              dest: /usr/local/bin/
              remote_src: yes 
              mode: '0755'
    
          - name: copy promtool to /usr/local/bin/
            copy: 
              src: /opt/prometheus/promtool
              dest: /usr/local/bin/
              remote_src: yes    
              mode: '0755'
    
          - name: change ownership to ec2-user #changing ownership to ec2-user
            command: chown -R ec2-user:ec2-user /usr/local/bin/prometheus
    
          - name: change ownership of promtool
            command: chown -R ec2-user:ec2-user /usr/local/bin/promtool 
    
          - name: copy consoles from prometheus to /etc/promethus/
            copy: 
              src: /opt/prometheus/consoles 
              dest: /etc/prometheus/
              remote_src: yes
    
          - name: copy console_libraries from prometheus to /etc/promethus/
            copy: 
              src: /opt/prometheus/console_libraries 
              dest: /etc/prometheus/
              remote_src: yes
    
          - name: copy prometheus.yml from prometheus to /etc/promethus/
            copy: 
              src: /opt/prometheus/prometheus.yml
              dest: /etc/prometheus/
              remote_src: yes
    
          - name: change ownership to ec2-user
            command: chown -R ec2-user:ec2-user /etc/prometheus/
    
          - name: create prometheus.service in /etc/systemd/system/prometheus.service 
            file: 
              path: /etc/systemd/system/prometheus.service
              state: touch 
              mode: '0755'
    
          - name : paste content in service file 
            copy: 
              dest: /etc/systemd/system/prometheus.service
              content: | 
                [Unit]
                Description=Prometheus
                Wants=network-online.target
                After=network-online.target          
    
                [Service]
                User=root
                group=root
                ExecStart=/usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml          
    
                [Install]
                WantedBy=multi-user.target
    
          - name: reload systemd 
            command: systemctl daemon-reload 
    
          - name: start prometheus #starting the prometheus service 
            command: systemctl start prometheus
    
  • Now time to run our playbook:

      ansible-playbook prometheus_install.yaml -i /home/Downloads/Ansible_files/hosts.ini
    

Output :

Prometheus :

Conclusion:

This article covers the installation of Prometheus with the help of Ansible and AWS ec2 instance.

I can be found @Linkedin.

If you've enjoyed it or have any queries, please feel free to reach out.