Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/content/posts/03-ansible-101-part-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: "Ansible 101 - Part 1"
publishedAt: 2025-08-04
description: ""
slug: "ansible-101-part-1"
isPublish: true
---

In the fast-paced world of IT infrastructure and application management, automation has become a key player in simplifying tasks, reducing human errors, and enhancing efficiency. Ansible, a powerful open-source automation tool, has gained widespread popularity due to its simplicity and versatility. In this blog, we'll take you on a journey to understand the fundamentals of Ansible and help you get started with building your automation playbook.

## Introduction to Ansible: What is Ansible?

At its core, Ansible is an automation engine that simplifies complex IT tasks, allowing you to manage systems, deploy applications, and orchestrate tasks with ease. Ansible follows a declarative approach, meaning you describe the desired state of your systems rather than scripting the steps to achieve that state. This approach makes Ansible highly readable, understandable, and efficient.

### Key Features and Benefits of Ansible

1. **Agentless Architecture:** One of Ansible's standout features is its agentless architecture. Unlike some other automation tools, Ansible doesn't require agents to be installed on target systems. This reduces the management overhead and potential security concerns associated with agents.

2. **Simple and Human-Readable Syntax:** Ansible playbooks use YAML (Yet Another Markup Language), which is easy to read and write. This simplifies collaboration among team members and enables even those without extensive programming experience to contribute to automation.

3. **Idempotent Operations:** Ansible playbooks are idempotent, meaning you can run them multiple times without causing unintended side effects. This ensures that the playbook's actions are predictable and consistent.

4. **Wide Range of Modules:** Ansible provides a comprehensive library of modules that cover various tasks, from package installation and service management to cloud provisioning and network configuration.

5. **Infrastructure as Code (IaC):** Ansible allows you to define your infrastructure as code, enabling you to version control and share your automation codebase.

6. **Scalability and Orchestration:** Ansible can scale from managing a single server to orchestrating complex multi-tier applications across multiple servers and cloud platforms.

### Ansible Installation and Setup:

I will suggest you use a Linux environment or Mac for running DevOps tasks.

Use `pipx` in your environment to install the full Ansible package:

```bash
$ pipx install --include-deps ansible
```

You can install the minimal `ansible-core` package:

```bash
$ pipx install ansible-core
```

Alternatively, you can install a specific version of `ansible-core`:

```bash
$ pipx install ansible-core==2.12.3
```

** Creating a basic inventory file **

Create a file at /etc/ansible/hosts (the default location for Ansible’s inventory file), and add one server to it:

```bash
$ sudo mkdir /etc/ansible
$ sudo touch /etc/ansible/hosts
```

Edit this host file and add this content to the file :

```bash
[example]
www.example.com
```

where [example] is the group of servers and example.com is the server IP address. You can create a virtual machine in Azure or create a droplet in DigitalOcean and perform ansible task in it.

Ansible takes port 22 as the default port. So if you are not using port 22 you have to define it like this: www.example.com:2222 in your ssh config file.

**Advice :**

- I will suggest you create a virtual machine in the cloud or a digitalocean droplet and add it to the inventory to practice Ansible.

- You can also perform these tasks on killerkoda.com for free.

- Please avoid using a virtual box if your laptop configuration is not suitable.

### Running AdHoc commands

Run this command in the terminal :

```bash
$ ansible example -m ping -u [username]
```

where [isuername] is the user you use to log in to the server. This command will ping the domain example using the given username.

You can also do the same using ssh :

```bash
ssh [email protected]
```

You can check all the adhoc commands in the Ansible docs. In the next article, I will discuss how to write ansible playbooks.
104 changes: 104 additions & 0 deletions src/content/posts/04-ansible-101-part-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
title: "Ansible 101 - Part 2"
publishedAt: 2025-09-01
description: ""
slug: "ansible-101-part-2"
isPublish: true
---

## Writing Your First Ansible Playbooks: A Beginner's Guide

### Your First Ansible Playbook

Now that you understand the benefits of Ansible, let's create your first Ansible playbook. A playbook is a structured document that defines a set of tasks to be executed on target systems. Here's a step-by-step guide:

### Step 1: Install Ansible

First, you need to install Ansible on your control node (the machine where you'll write and execute playbooks). Ansible can be installed on Linux, macOS, or Windows Subsystem for Linux (WSL).

You can install Ansible using package managers like `apt`, `yum`, or `brew`, or use Python's package manager, `pip`.

### Step 2: Inventory

Ansible uses an inventory file to define the target hosts or groups of hosts where your playbooks will run. Create an inventory file (e.g., `inventory.yml`) and specify your target hosts in it. For example:

```bash
webserver:
hosts:
webserver1:
ansible_host: 192.168.1.100
webserver2:
ansible_host: 192.168.1.101
```

In this example, we defined two web servers with their IP addresses.

### Step 3: Create a Playbook

Now it's time to create your playbook. Playbooks are written in YAML and consist of a series of tasks. Let's create a simple playbook to ensure the Nginx web server is installed and running on the target hosts.

```bash
- name: Install and start Nginx
hosts: webserver
become: yes

tasks:
- name: Update APT package cache
apt:
update_cache: yes

- name: Install Nginx
apt:
name: nginx
state: present

- name: Start Nginx
service:
name: nginx
state: started
```

In this playbook:

- `name` provides a description of the playbook.

- `hosts` specify the target hosts or groups (in our case, the "webserver" group).

- `become: yes` indicates that we'll execute tasks with elevated privileges (sudo).

- The `tasks` section lists the individual tasks to be performed, such as updating the package cache, installing Nginx, and starting the Nginx service.

### Step 4: Run the Playbook

To run the playbook, use the `ansible-playbook` command:

```bash
ansible-playbook -i inventory.yml your_playbook.yml
```

Replace `inventory.yml` with the path to your inventory file and `your_playbook.yml` with the name of your playbook.

The Ansible engine will connect to the target hosts defined in your inventory and execute the tasks sequentially.

### Step 5: Verify

After running the playbook, you can verify the status of your target hosts. Open a web browser and enter the IP address of one of your web servers. If Nginx is correctly installed and running, you should see the default Nginx welcome page.

Congratulations! You've just executed your first Ansible playbook.

### Setting user and sudo options with ansible-playbook

If no remote_user is defined alongside the hosts in a playbook, Ansible assumes you’ll connect as the user-defined in your inventory file for a particular host, and then will fall back to your local user account name. You can explicitly define a remote user to use for remote plays using the --user (-u) option:

```bash
$ ansible-playbook playbook.yml --user=johndoe
```

In some situations, you will need to pass along your sudo password to the remote server to perform commands via sudo. In these situations, you’ll need to use the --ask-become-pass (-K) option. You can also explicitly force all tasks in a playbook to use sudo with --become (-b). Finally, you can define the sudo user for tasks run via sudo (the default is root) with the --become-user (-U) option.

the following command will run our example playbook with sudo, performing the tasks as the sudo user janedoe, and Ansible will prompt you for the sudo password:

```bash
$ ansible-playbook playbook.yml --become --become-user=janedoe
--ask-become-pass
```
129 changes: 129 additions & 0 deletions src/content/posts/05-running-docker-image-of-java-application.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
title: "Running docker image of java application"
publishedAt: 2025-07-20
description: ""
slug: "Running docker image of java application"
isPublish: true
---

**Before Starting**

- I have wsl2 installed in my system and I am using my Linux environment to run this application. I also advised developers to use
- Linux environment as we will be going to use a lot of command line stuff later.
- You can also run it in eclipse or another code editor for demo purposes.

**Software needed :**
- Java
- Maven
- VS Code
- Linux in WSL2

**Sample Application**

Let’s clone the sample application that we’ll be using in this module to our local development machine. Run the following commands in a terminal to clone the repo.

```bash
$ cd /path/to/working/directory
$ git clone https://github.com/spring-projects/spring-petclinic.git
$ cd spring-petclinic
```

**Java and Maven version**

It's the version of maven and java I am using.

![image 1](https://cdn.hashnode.com/res/hashnode/image/upload/v1672669242503/ad80caf8-f713-4e67-b1fc-03276cfda163.png?auto=compress,format&format=webp)

**Steps to run the application locally**

- Go to your directory where your application lies in the command line. Like it's my working directory.

`arvind@DESKTOP-1DEUAEO:/mnt/c/Users/arvin/Desktop/spring-petclinic$`

- Run the application using mvnw spring-boot:run command or mvn spring-boot:run command. It will take some time to run the application for the first time. When I run this first time on my machine, it took almost 16 mins, so wait for a while.

![image 2](https://cdn.hashnode.com/res/hashnode/image/upload/v1672669341326/f6883bf3-9600-4295-8d96-4770931ae394.png?auto=compress,format&format=webp)

**Note:** If while running this command `mvnw spring-boot:run` if it is showing an error, you have to install mvnw using this command `mvn -N wrapper:wrapper` and then try to run the previous command again. In wsl2, mvnw is still not working sometimes due to a windows issue, then run this -

```bash
sudo apt install dos2unix
dos2unix mvnw
```

Now your mvnw will work properly.

- When you get this in your terminal, it means your application is started now. Now visit your http://localhost:8080.

- It will look something like this in your browser.

![Image 3](https://cdn.hashnode.com/res/hashnode/image/upload/v1672669365487/b79fc692-2b47-486e-a608-ba26be0305ea.png?auto=compress,format&format=webp)

### Errors

The error you can get while running the application.

You will get something like this that says execution failed and list some java files.

![image 4](https://cdn.hashnode.com/res/hashnode/image/upload/v1672669396245/82571d9a-897d-4edc-8abd-575cb63cf922.png?auto=compress,format&format=webp)

n this case, just open your application in vs code and convert it from CRLF to LF which you can find at the right bottom of the vs code.

### Maven build and containerize java application

**Before Starting**

-I have wsl2 installed in my system and I am using my Linux environment to run this application. I also advised developers to use a Linux environment as we will be going to use a lot of command line stuff later.

**Maven build**

In the previous step, we run our application normally. Now it's time to do the maven build (using mvnw) and then make its docker container and try to run the application from inside of our container.

In the root directory of your project, run this command - `./mvnw -Dmaven.test.skip=true spring-boot:build-image`

**Note :** If while running this command `./mvnw -Dmaven.test.skip=true spring-boot:build-image` if it is showing an error no such file or directory, it's a wsl 2 problem. In wsl2, mvnw is still not working sometimes due to windows issues, so run this -

```bash
sudo apt install dos2unix
dos2unix mvnw
```

Now your mvnw will works properly.

This command will create a target folder with a jar file. In this example, the name of the jar file will be - `spring-petclinic-2.7.0-SNAPSHOT.jar`

**Dockerfile**

In the root of the folder create a file named Dockerfile and copy the below content in it.

```bash
# Choose your Java image
FROM openjdk:8-jdk-alpine

# Create volume for the Java jar build process
VOLUME /tmp

# Copy the jar to the container
COPY target/*.jar app.jar

# Set your command to start the Java application
ENTRYPOINT ["java","-jar","/app.jar"]
```

**Build an image**

Now run this command in the terminal :

```bash
docker build -t myorg/myapp .
```

This will create a docker image of the application.

Now run the following command to run the container :

```bash
docker run -p 8080:8080 myorg/myapp
```

It will take some time and this will be showing in your terminal, and your application is started now. Now go to localhost:8080 and check your application running from inside a container.
Loading