Skip to content

Commit 9f94a6e

Browse files
authored
ansible: Add blog post file (#613)
1 parent cf9fcb6 commit 9f94a6e

File tree

4 files changed

+176
-5
lines changed

4 files changed

+176
-5
lines changed
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Ansible helloworld example
22

3-
- Inventory file: [inventory.ini](./inventory.ini)
3+
- Inventory file: [first-inventory.ini](./first-inventory.ini)
44
- First playbook file: [first-playbook.yml](./first-playbook.yml)
55

66
- Run the first playbook
@@ -10,7 +10,7 @@
1010
cd devops-basics/topics/ansible/basic/helloworld
1111

1212
# Run playbook
13-
ansible-playbook -i inventory.ini first-playbook.yml
13+
ansible-playbook -i first-inventory.ini first-playbook.yml
1414
```
1515

1616
- Or use the demo script if you want: [ansible-helloworld.sh](./ansible-helloworld.sh)

topics/ansible/basic/helloworld/ansible-helloworld.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ console_log "Checking Ansible version"
2020
ansible --version
2121

2222
console_log "Checking inventory host"
23-
ansible all --list-hosts -i inventory.ini
23+
ansible all --list-hosts -i first-inventory.ini
2424

2525
console_log "Send ping command to the host"
26-
ansible all -m ping -i inventory.ini
26+
ansible all -m ping -i first-inventory.ini
2727

2828
console_log "Run the first playbook - get uptime and OS release on localhost"
2929
# Note: `-v` flag if you want to verbose the ansible execution result
30-
ansible-playbook -i inventory.ini first-playbook.yml
30+
ansible-playbook -i first-inventory.ini first-playbook.yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
## Overview 👋
2+
3+
In this blog post, we'll introduce **Ansible**, explore its core concepts, and walk through the process of creating your first playbook. We'll also provide the additional resources for you to explore the Ansible.
4+
5+
## Introduction to Ansible 💡
6+
7+
Ansible is an open-source IT automation tool that simplifies application deployment, cloud provisioning, and configuration management across diverse environments. It uses a declarative language to describe the desired state of the system, and then takes the necessary actions to achieve that state. Ansible has become incredibly popular due to its simplicity, agentless architecture, and extensive community support.
8+
**Document:** [ansible.com](https://www.ansible.com/), [ansible basics](https://github.com/tungbq/devops-basics/tree/main/topics/ansible)
9+
10+
## Install Ansible 🔧
11+
12+
Use `pip` in your selected Python environment to install the full Ansible package for the current user:
13+
14+
```bash
15+
python3 -m pip install --user ansible
16+
```
17+
18+
Or follow this [official installation guide](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible) for more installation detailed.
19+
20+
## Core Ansible Concepts for Beginners 📘
21+
22+
Before we dive into our first playbook, it's essential to understand some of the core concepts in Ansible:
23+
24+
1. **Inventory**: An inventory file is a list of hosts or groups of hosts against which Ansible will run its tasks. It provides a way to organize and manage the target systems.
25+
2. **Modules**: Modules are the building blocks of Ansible. They are reusable, stand-alone scripts that perform specific tasks, such as managing packages, files, services, or executing commands.
26+
3. **Tasks**: Tasks are individual units of work that Ansible executes on the target hosts. They are defined within a playbook and can invoke one or more Ansible modules.
27+
4. **Playbooks**: Playbooks are **YAML** files that contain one or more plays. A play is a set of tasks that map a group of hosts to a set of roles or tasks to be executed.
28+
5. **Roles**: Roles are a way to organize and package related tasks, files, and templates. They promote code reuse and make playbooks more readable and maintainable.
29+
6. **Facts**: Facts are pieces of information about the target hosts, such as operating system, IP address, or installed packages. Ansible collects these facts automatically and can use them to make decisions or customize task execution.
30+
7. **Handlers**: Handlers are special tasks that are triggered by other tasks in a playbook. They are typically used to restart services or perform specific actions after a change has been made to the system.
31+
By understanding these core concepts, you'll have a solid foundation for working with Ansible and creating more complex and powerful automation workflows.
32+
33+
Now let's move on the practice section and start playing with Ansible
34+
35+
## Hands on 👷
36+
37+
NOTE: The sample code for this hands on could be found [here](https://github.com/tungbq/devops-basics/tree/main/topics/ansible/basic/helloworld)
38+
39+
### 1. Create Inventory File
40+
41+
Here's an example of a basic inventory file:
42+
43+
```ini
44+
[webservers]
45+
web1.example.com
46+
web2.example.com
47+
48+
[dbservers]
49+
db1.example.com
50+
db2.example.com
51+
; other hosts definition as needed
52+
```
53+
54+
To keep this practice simple, we will use the `localhost` as the target host, create a new file named `first-inventory.ini` and add the following content:
55+
56+
```ini
57+
; first-inventory.ini
58+
[my-localhost]
59+
127.0.0.1 ansible_connection=local
60+
```
61+
62+
Or you can use my sample code on [**GitHub**](https://github.com/tungbq/devops-basics/blob/main/topics/ansible/basic/helloworld/first-inventory.ini)
63+
64+
### 2. Create The First Playbook - Check uptime and OS release
65+
66+
Let's create our first playbook that will simply check `uptime` and `OS release` on the target hosts.
67+
Create a new file named `first-playbook.yml` and add the following content:
68+
69+
```yaml
70+
# first-playbook.yml
71+
---
72+
- name: Basic tasks
73+
# This host is defined in the `first-inventory.ini`
74+
hosts: my-localhost
75+
tasks:
76+
- name: Execute uptime command
77+
command: uptime
78+
register: uptime_result
79+
- debug: var=uptime_result.stdout_lines
80+
81+
- name: Check OS release
82+
command: cat /etc/os-release
83+
register: os_result
84+
- debug: var=os_result.stdout_lines
85+
```
86+
87+
Or you can use my sample code on [**GitHub**](https://github.com/tungbq/devops-basics/blob/main/topics/ansible/basic/helloworld/first-playbook.yml)
88+
89+
Here's what each section of the playbook means:
90+
91+
`---` Indicates the start of a YAML document.
92+
`hosts`: All specifies that the tasks in this play should be executed on all hosts in the inventory file.
93+
`tasks`: Marks the beginning of the list of tasks to be executed.
94+
`name`: Provide a human-readable description of the task.
95+
`command`: The shell command to be executed on the target hosts.
96+
`register`: Capture the output from task execution and store it in a variable.
97+
`debug`: An Ansible module used to print statements.
98+
99+
### 3. Run the Ansible playbook
100+
101+
To run the playbook, use the following Ansible command:
102+
103+
```bash
104+
ansible-playbook -i first-inventory.ini first-playbook.yml
105+
```
106+
107+
BONUS: I wrote a small script to automate this demo [ansible-helloworld.sh](https://github.com/tungbq/devops-basics/blob/main/topics/ansible/basic/helloworld/ansible-helloworld.sh). You could use it if interested.
108+
109+
### 4. The result
110+
111+
Once the command executed successfully, you would see the output similar to:
112+
113+
```bash
114+
PLAY [Basic tasks] ************************************************************************************************************************************************************************
115+
116+
TASK [Gathering Facts] ********************************************************************************************************************************************************************
117+
ok: [127.0.0.1]
118+
119+
TASK [Execute uptime command] *************************************************************************************************************************************************************
120+
changed: [127.0.0.1]
121+
122+
TASK [debug] ******************************************************************************************************************************************************************************
123+
ok: [127.0.0.1] => {
124+
"uptime_result.stdout_lines": [
125+
" 15:02:46 up 4:24, 2 users, load average: 6.62, 4.45, 3.50"
126+
]
127+
}
128+
129+
TASK [Check OS release] *******************************************************************************************************************************************************************
130+
changed: [127.0.0.1]
131+
132+
TASK [debug] ******************************************************************************************************************************************************************************
133+
ok: [127.0.0.1] => {
134+
"os_result.stdout_lines": [
135+
"PRETTY_NAME=\"Ubuntu 22.04.2 LTS\"",
136+
"NAME=\"Ubuntu\"",
137+
"VERSION_ID=\"22.04\"",
138+
"VERSION=\"22.04.2 LTS (Jammy Jellyfish)\"",
139+
"VERSION_CODENAME=jammy",
140+
"ID=ubuntu",
141+
"ID_LIKE=debian",
142+
"HOME_URL=\"https://www.ubuntu.com/\"",
143+
"SUPPORT_URL=\"https://help.ubuntu.com/\"",
144+
"BUG_REPORT_URL=\"https://bugs.launchpad.net/ubuntu/\"",
145+
"PRIVACY_POLICY_URL=\"https://www.ubuntu.com/legal/terms-and-policies/privacy-policy\"",
146+
"UBUNTU_CODENAME=jammy"
147+
]
148+
}
149+
150+
PLAY RECAP ********************************************************************************************************************************************************************************
151+
127.0.0.1 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
152+
```
153+
154+
Congratulations 🎉, you've successfully created and run the first Ansible playbook! 💥
155+
156+
## Conclusion ✏️
157+
158+
In this post, you already explored the basic Ansible concepts, wrote, and ran your own `first playbook` on your `localhost`. In the future, I will create another post for more advanced Ansible examples.
159+
160+
If you want to learn more about DevOps, you could visit the [devops-basics](https://github.com/tungbq/devops-basics) for more content about various DevOps tools and concepts like Ansible, Docker, K8s, Terraform, Cloud, CI/CD, ...
161+
162+
And kindly consider supporting me by giving it a star ⭐️.
163+
164+
<table>
165+
<tr>
166+
<td>
167+
<a href="https://github.com/tungbq/devops-basics" style="text-decoration: none;"><strong>Star devops-basics ⭐️ on GitHub</strong></a>
168+
</td>
169+
</tr>
170+
</table>
171+
Thank you for reading and happy coding! 💖

0 commit comments

Comments
 (0)