Skip to content

Commit 6149f44

Browse files
committed
feat: add blog posts
1 parent 8242a96 commit 6149f44

File tree

4 files changed

+987
-0
lines changed

4 files changed

+987
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: "Ansible 101 - Part 1"
3+
publishedAt: 2025-08-04
4+
description: ""
5+
slug: "ansible-101-part-1"
6+
isPublish: true
7+
---
8+
9+
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.
10+
11+
## Introduction to Ansible: What is Ansible?
12+
13+
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.
14+
15+
### Key Features and Benefits of Ansible
16+
17+
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.
18+
19+
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.
20+
21+
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.
22+
23+
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.
24+
25+
5. **Infrastructure as Code (IaC):** Ansible allows you to define your infrastructure as code, enabling you to version control and share your automation codebase.
26+
27+
6. **Scalability and Orchestration:** Ansible can scale from managing a single server to orchestrating complex multi-tier applications across multiple servers and cloud platforms.
28+
29+
### Ansible Installation and Setup:
30+
31+
I will suggest you use a Linux environment or Mac for running DevOps tasks.
32+
33+
Use `pipx` in your environment to install the full Ansible package:
34+
35+
```bash
36+
$ pipx install --include-deps ansible
37+
```
38+
39+
You can install the minimal `ansible-core` package:
40+
41+
```bash
42+
$ pipx install ansible-core
43+
```
44+
45+
Alternatively, you can install a specific version of `ansible-core`:
46+
47+
```bash
48+
$ pipx install ansible-core==2.12.3
49+
```
50+
51+
** Creating a basic inventory file **
52+
53+
Create a file at /etc/ansible/hosts (the default location for Ansible’s inventory file), and add one server to it:
54+
55+
```bash
56+
$ sudo mkdir /etc/ansible
57+
$ sudo touch /etc/ansible/hosts
58+
```
59+
60+
Edit this host file and add this content to the file :
61+
62+
```bash
63+
[example]
64+
www.example.com
65+
```
66+
67+
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.
68+
69+
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.
70+
71+
**Advice :**
72+
73+
- I will suggest you create a virtual machine in the cloud or a digitalocean droplet and add it to the inventory to practice Ansible.
74+
75+
- You can also perform these tasks on killerkoda.com for free.
76+
77+
- Please avoid using a virtual box if your laptop configuration is not suitable.
78+
79+
### Running AdHoc commands
80+
81+
Run this command in the terminal :
82+
83+
```bash
84+
$ ansible example -m ping -u [username]
85+
```
86+
87+
where [isuername] is the user you use to log in to the server. This command will ping the domain example using the given username.
88+
89+
You can also do the same using ssh :
90+
91+
```bash
92+
93+
```
94+
95+
You can check all the adhoc commands in the Ansible docs. In the next article, I will discuss how to write ansible playbooks.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
title: "Ansible 101 - Part 2"
3+
publishedAt: 2025-09-01
4+
description: ""
5+
slug: "ansible-101-part-2"
6+
isPublish: true
7+
---
8+
9+
## Writing Your First Ansible Playbooks: A Beginner's Guide
10+
11+
### Your First Ansible Playbook
12+
13+
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:
14+
15+
### Step 1: Install Ansible
16+
17+
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).
18+
19+
You can install Ansible using package managers like `apt`, `yum`, or `brew`, or use Python's package manager, `pip`.
20+
21+
### Step 2: Inventory
22+
23+
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:
24+
25+
```bash
26+
webserver:
27+
hosts:
28+
webserver1:
29+
ansible_host: 192.168.1.100
30+
webserver2:
31+
ansible_host: 192.168.1.101
32+
```
33+
34+
In this example, we defined two web servers with their IP addresses.
35+
36+
### Step 3: Create a Playbook
37+
38+
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.
39+
40+
```bash
41+
- name: Install and start Nginx
42+
hosts: webserver
43+
become: yes
44+
45+
tasks:
46+
- name: Update APT package cache
47+
apt:
48+
update_cache: yes
49+
50+
- name: Install Nginx
51+
apt:
52+
name: nginx
53+
state: present
54+
55+
- name: Start Nginx
56+
service:
57+
name: nginx
58+
state: started
59+
```
60+
61+
In this playbook:
62+
63+
- `name` provides a description of the playbook.
64+
65+
- `hosts` specify the target hosts or groups (in our case, the "webserver" group).
66+
67+
- `become: yes` indicates that we'll execute tasks with elevated privileges (sudo).
68+
69+
- The `tasks` section lists the individual tasks to be performed, such as updating the package cache, installing Nginx, and starting the Nginx service.
70+
71+
### Step 4: Run the Playbook
72+
73+
To run the playbook, use the `ansible-playbook` command:
74+
75+
```bash
76+
ansible-playbook -i inventory.yml your_playbook.yml
77+
```
78+
79+
Replace `inventory.yml` with the path to your inventory file and `your_playbook.yml` with the name of your playbook.
80+
81+
The Ansible engine will connect to the target hosts defined in your inventory and execute the tasks sequentially.
82+
83+
### Step 5: Verify
84+
85+
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.
86+
87+
Congratulations! You've just executed your first Ansible playbook.
88+
89+
### Setting user and sudo options with ansible-playbook
90+
91+
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:
92+
93+
```bash
94+
$ ansible-playbook playbook.yml --user=johndoe
95+
```
96+
97+
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.
98+
99+
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:
100+
101+
```bash
102+
$ ansible-playbook playbook.yml --become --become-user=janedoe
103+
--ask-become-pass
104+
```
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
title: "Running docker image of java application"
3+
publishedAt: 2025-07-20
4+
description: ""
5+
slug: "Running docker image of java application"
6+
isPublish: true
7+
---
8+
9+
**Before Starting**
10+
11+
- I have wsl2 installed in my system and I am using my Linux environment to run this application. I also advised developers to use
12+
- Linux environment as we will be going to use a lot of command line stuff later.
13+
- You can also run it in eclipse or another code editor for demo purposes.
14+
15+
**Software needed :**
16+
- Java
17+
- Maven
18+
- VS Code
19+
- Linux in WSL2
20+
21+
**Sample Application**
22+
23+
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.
24+
25+
```bash
26+
$ cd /path/to/working/directory
27+
$ git clone https://github.com/spring-projects/spring-petclinic.git
28+
$ cd spring-petclinic
29+
```
30+
31+
**Java and Maven version**
32+
33+
It's the version of maven and java I am using.
34+
35+
![image 1](https://cdn.hashnode.com/res/hashnode/image/upload/v1672669242503/ad80caf8-f713-4e67-b1fc-03276cfda163.png?auto=compress,format&format=webp)
36+
37+
**Steps to run the application locally**
38+
39+
- Go to your directory where your application lies in the command line. Like it's my working directory.
40+
41+
`arvind@DESKTOP-1DEUAEO:/mnt/c/Users/arvin/Desktop/spring-petclinic$`
42+
43+
- 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.
44+
45+
![image 2](https://cdn.hashnode.com/res/hashnode/image/upload/v1672669341326/f6883bf3-9600-4295-8d96-4770931ae394.png?auto=compress,format&format=webp)
46+
47+
**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 -
48+
49+
```bash
50+
sudo apt install dos2unix
51+
dos2unix mvnw
52+
```
53+
54+
Now your mvnw will work properly.
55+
56+
- When you get this in your terminal, it means your application is started now. Now visit your http://localhost:8080.
57+
58+
- It will look something like this in your browser.
59+
60+
![Image 3](https://cdn.hashnode.com/res/hashnode/image/upload/v1672669365487/b79fc692-2b47-486e-a608-ba26be0305ea.png?auto=compress,format&format=webp)
61+
62+
### Errors
63+
64+
The error you can get while running the application.
65+
66+
You will get something like this that says execution failed and list some java files.
67+
68+
![image 4](https://cdn.hashnode.com/res/hashnode/image/upload/v1672669396245/82571d9a-897d-4edc-8abd-575cb63cf922.png?auto=compress,format&format=webp)
69+
70+
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.
71+
72+
### Maven build and containerize java application
73+
74+
**Before Starting**
75+
76+
-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.
77+
78+
**Maven build**
79+
80+
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.
81+
82+
In the root directory of your project, run this command - `./mvnw -Dmaven.test.skip=true spring-boot:build-image`
83+
84+
**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 -
85+
86+
```bash
87+
sudo apt install dos2unix
88+
dos2unix mvnw
89+
```
90+
91+
Now your mvnw will works properly.
92+
93+
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`
94+
95+
**Dockerfile**
96+
97+
In the root of the folder create a file named Dockerfile and copy the below content in it.
98+
99+
```bash
100+
# Choose your Java image
101+
FROM openjdk:8-jdk-alpine
102+
103+
# Create volume for the Java jar build process
104+
VOLUME /tmp
105+
106+
# Copy the jar to the container
107+
COPY target/*.jar app.jar
108+
109+
# Set your command to start the Java application
110+
ENTRYPOINT ["java","-jar","/app.jar"]
111+
```
112+
113+
**Build an image**
114+
115+
Now run this command in the terminal :
116+
117+
```bash
118+
docker build -t myorg/myapp .
119+
```
120+
121+
This will create a docker image of the application.
122+
123+
Now run the following command to run the container :
124+
125+
```bash
126+
docker run -p 8080:8080 myorg/myapp
127+
```
128+
129+
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.

0 commit comments

Comments
 (0)