Datadog installation using Ansible Playbook and Jenkins

Datadog installation using Ansible Playbook and Jenkins

Introduction

Welcome to our guide on how to install Datadog using Ansible and Jenkins. In today's tech world, monitoring your systems is crucial for smooth operations. Datadog is a powerful tool for keeping an eye on your infrastructure and applications.In this guide, we'll show you how to set up Datadog on your servers with Ansible and Jenkins. This automation combo makes the installation process efficient and reliable, saving you time and ensuring consistency.Whether you're a seasoned pro or just getting started with Datadog, our guide will help you simplify the installation and unleash Datadog's monitoring capabilities.

Let's get started!

Prerequisites

1. Ec2 instances[2]

master-server, Target-server

Ensure that both the EC2 instances having same key at the time of creation.

AMI-ubuntu, Type-t2.micro, port 22,80,443 should be enabled

2. Ansible Installed (only on master server)

3. Jenkins Installed (only on master server)

Step 1: Launch an Ec2 instances and install Ansible And Jenkins only on master server

1. successfully launch an ec2 instances.And connect them with either putty or ssh client.

2. By executing the following commands one by one we can install the ansible on our master server.

sudo yum update

sudo amazon-linux-extras install ansible2

ansible --version

3. Install Jenkins on master server

Refer this URL to Install and unlock Jenkins -- >>

Step 2: Copying a .pem Key from Local to Master Server via SSH

1. mkdir keys: This part creates a new directory named "keys" in your current location.

2. cd keys: This part changes the current working directory to the newly created "keys" directory.

3. Then go to your local machine command prompt where your .pem key is downloded. ex- cd Downloads/and copy that .pem key on the master server in the keys directory

4. For that we can simply use below scp command. My .pem key file name is master-key.pem so we have to copy that key to keys directory.

scp -i "key_name" <key_source_path> <Instance_host_name>:<key_destination_path>

scp -i "master-key.pem" Downloads\master-key.pem ec2-user@ec2-13-232-76-204.ap-south-1.compute.amazonaws.com :/home/ec2-user/keys/master-key.pe

5. We have successfully transferred the

.pem key to the 'keys' directory on the master server

6. give the permision to master-key.pem file

sudo chmod 400 master-key.pem

Step 3:Update Host file and Configuration file

1.Update hosts file

Go to the host file path as shown below cd /etc/ansible/ open that hosts file in vim editor mode to update.

Add the private ip address of the aTarget server and update the hosts file as shown below.

Add the variable in the hosts file.as shown below image.

Here, add the user name and youe ssh private key file pathin the group [servers:vars]

save and exit the file

2. Update configuration file

1. Now update the ansible.cfg file present on path cd /etc/ansible/

2. Run the "ls" command to list files and directories.

3. Verify the "ansible.cfg" file is present in the current directory.

4. Add the below content that ansible.cfg file

[defaults]
host_key_checking = false
remote_user = ec2-user
ask_pass = false
private_key_file = /home/ec2-user/keys/master-key.pem
roles_path = /etc/ansible/roles
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
# Location where ansible look for hosts
inventory = /etc/ansible/hosts

  1. Confirm Server Conectivity

Validate the connectivity status of our target-server with the master-server.

ansible server -m ping

Step 4: Install Datadog Agent Ansible Role

  • First we have to Create a datadog free trial account via browser.

Install the Datadog Ansible collection from Ansible Galaxy on your master-server

ansible-galaxy install datadog.datadog

Step 5: Write a Playbook

1. create a datadog.yml file to write playbook.

cd /etc/ansible     # to go inside the path
mkdir playbook      # create directory
cd playbook         # go inside that directory
vim datadog.yml     # create and open datadog.yml file in vim editor

2. Write a playbook to install datadog agent as well as enable process and log monitoring on target server.

vim datadog.yml

- name: Install Datadog Agent on target server in AWS
  hosts: server
  become: yes
  roles:
    - { role: datadog.datadog, become: yes }
  vars:
    datadog_api_key: "d39dcb436ee0844c256ac0a124ce16f7"
    datadog_config:
      tags:
        - env:dev
      logs_enabled: true
      process_config:
      enabled: "true"

3. write a playbook to stop the datadog agent on target server

vim stop_datadog.yml

- name: Stop Datadog Agent
  hosts: server
  become: yes

  tasks:
    - name: Ensure Datadog Agent service is stopped and disabled
      systemd:
        name: datadog-agent
        enabled: no
        state: stopped

4. write a playbook to start and enable the datadog agent on target server

vim start_datadog.yml

- name: Start and Enable Datadog Agent
  hosts: server
  become: yes
  tasks:
    - name: Ensure Datadog Agent service is started and enabled
      systemd:
        name: datadog-agent
        enabled: yes
        state: started

Step 6: Integrate Ansible with Jenkins

1. Install Ansible plugins in Jenkins.

Search for an Ansible and Install it.

2. Go to the Manage Jenkins -->Tools. Search for Ansible Installation.Add the default Ansible path /usr/bin

Step 7: Create a freestyle job to run the Ansible playbook.

1. Create a freestyle job to install dataog agent on target server.

Click on create a job

Item name -> freestyle job -> ok

Add the description.

Provide the Ansible playbook path.

Add jenkins.

Provide Jenkins credentials.

Add key in the private key section.

Select the username which we create before. i.e ec2-user

Click on Build Now.

Go to the console output

Datadog Agent is Installed Successfully on target server.

2. Create a freestyle job to stop datadog agent on target server.

Add the Description

Add the Ansible playbook path.

Select the username we created before. i.e ec2-user

Click on Build Now.

Go to the console output.

Here we stop the datadog Agent successfuly on Target server.

3. Create freestyle job to start datadog agent.

Add the Description.

Add the Ansible Playbook path.

Select the usename we created before.i.e ec2-user

Click on Build Now.

Go to the Console output.

Here we can Start Datadog Agent on target server.

Summary

This blog demonstrates the seamless integration of Ansible and Jenkins for automating the installation of Datadog, a robust monitoring and analytics platform. Leveraging the power of Ansible playbooks and the automation capabilities of Jenkins, we have established a streamlined process for deploying Datadog agents to target servers within our infrastructure. This efficient and reliable workflow ensures that our monitoring solution is consistently up-to-date, providing real-time insights into the performance and health of our systems. By following this comprehensive guide, you can enhancing your ability to monitor, troubleshoot, and optimize your infrastructure.

Thanks For Reading! Stay Tuned! 🤗

Happy Learning!

Neha Bawane.