
Introduction
Welcome to Master IT Automation with Ansible, Quickly! I'm Luca Berton, and I’m excited to be your guide on this fast-paced and impactful journey into IT automation. If you have ever thought, "There must be a better way to manage my infrastructure," then you’re in the right place. This course is designed to help you harness the power of Ansible to save time, reduce errors, and streamline your IT workflows.
Why Ansible?
Ansible is like having a smart assistant for your infrastructure. It helps automate a wide range of tasks, from deploying applications to configuring servers. What makes Ansible unique is its human-readable language, YAML, which ensures that you don’t need advanced programming skills to use it effectively. This makes it the perfect tool for both beginners and experienced professionals looking to improve their automation capabilities.
Imagine This…
Deploying a dozen servers in minutes instead of hours.
Patching systems across the globe with a single command.
Automating routine IT tasks so you can focus on high-value projects.
That’s the power of Ansible! And in this course, we’ll focus on what you need to know to get started right away.
What You Will Learn
This course is designed for practical application. We’ll cover:
Installing and Configuring Ansible on your system.
Writing your first playbook to deploy a web server.
Using Ansible modules to automate IT tasks efficiently.
A hands-on project where you’ll deploy a fully functional server.
By the end of this course, you will have a solid foundation in Ansible and be ready to start automating your IT infrastructure with confidence.
Who Should Take This Course?
This course is ideal for:
System Administrators looking to streamline IT operations.
DevOps Engineers aiming for efficient CI/CD workflows.
Cloud Professionals automating deployments in AWS, Azure, or VMware.
IT Enthusiasts who want to master infrastructure as code.
Course Approach
This course follows a hands-on, example-driven approach. You will work on real-world automation tasks using Ansible, and by the end of the course, you’ll be able to apply Ansible confidently in your projects.
Are You Ready?
Let’s use Ansible to automate your IT infrastructure. Your journey to mastering automation starts now!
Welcome to the Getting Started with Ansible section! In this lecture, we will cover the fundamental steps to install and configure Ansible on different operating systems, verify its installation, and execute your first ad-hoc command.
Step 1: Installing Ansible on Your System
Linux (First-Class Citizen of Ansible)
Since Ansible is designed around Linux and Python, the best approach is to install it via your system’s package manager:
For RHEL-based distributions (Fedora, CentOS, Rocky Linux, AlmaLinux)
dnf install ansible -y
For Debian-based distributions (Ubuntu, Debian)
apt install ansible -y
macOS Installation
Mac users can install Ansible easily using Homebrew:
First, ensure Homebrew is installed:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then, install Ansible:
brew install ansible
Windows Installation (Using WSL)
Ansible does not natively run on Windows, but it can be used via the Windows Subsystem for Linux (WSL):
Install WSL by running:
wsl --install
Choose and install a Linux distribution (Ubuntu, Debian, or RHEL).
Once inside WSL, install Ansible using the package manager (as shown in the Linux section).
Step 2: Verifying Ansible Installation
After installation, confirm that Ansible is working correctly by checking the version:
ansible --version
This command prints the installed Ansible version along with its dependent libraries. Ensure your system is updated by using the package manager of your distribution.
Step 3: Configuring Ansible Inventory & Running Your First Command
Understanding the Ansible Inventory File
To manage systems, Ansible needs an inventory file. By default, it is located at:
/etc/ansible/hosts
If this directory does not exist, create it manually:
sudo mkdir -p /etc/ansible
sudo touch /etc/ansible/hosts
For our initial test, we will use localhost and specify a local connection:
[local]
localhost ansible_connection=local
Creating the Ansible Configuration File (ansible.cfg)
The Ansible configuration file allows users to define timeouts, plugins, and other options. Create a simple ansible.cfg in the /etc/ansible/ directory:
[defaults]
inventory = /etc/ansible/hosts
host_key_checking = False
timeout = 30
Step 4: Testing the Ansible Connection
Now, let’s ping all the hosts in the inventory to ensure Ansible is communicating properly:
ansible all -m ping
If everything is configured correctly, you should see a successful response indicating that Ansible is ready to use!
Congratulations! What’s Next?
You have successfully:
Installed Ansible on Linux, macOS, and Windows (via WSL).
Verified the installation with ansible --version.
Configured the inventory file and Ansible settings.
Executed your first Ansible ad-hoc command.
Now, we move forward to creating an initial workflow using Ansible Playbooks!
Welcome to the next step in mastering Ansible! In this lecture, we will write our first Ansible Playbook to automate the installation and configuration of a web server.
What is an Ansible Playbook?
Think of a playbook as a recipe. It defines the steps needed to automate a task, such as installing software, configuring services, or deploying applications. The best part? It’s written in YAML, a simple, human-readable format that makes automation easy and accessible.
Step 1: Creating an Ansible Playbook
Let’s create a playbook that sets up an Nginx web server and deploys a custom web page.
Ansible Playbook Structure
A playbook consists of:
Plays – Define the automation workflow.
Tasks – The actual steps that Ansible will execute.
Modules – Reusable Ansible components that execute specific tasks.
Variables – Dynamic values used within tasks or templates.
Here’s a basic Ansible Playbook to install Nginx and deploy a webpage:
---
- name: Template Module Demo
hosts: all
become: true # Run as superuser
vars:
page_title: "Welcome to Ansible Automation"
page_description: |
This page is managed by Ansible.
Automating IT has never been easier!
tasks:
- name: Install Nginx
ansible.builtin.apt:
name: nginx
state: latest
- name: Deploy Custom Web Page
ansible.builtin.template:
src: index.html.j2
dest: /var/www/html/index.html
Step 2: Creating the Web Page Template
Ansible uses Jinja2 templating to dynamically insert variables into configuration files.
Let’s create a template file named index.html.j2:
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ page_title }}</title>
</head>
<body>
<h1>{{ page_title }}</h1>
<p>{{ page_description }}</p>
</body>
</html>
The {{ page_title }} and {{ page_description }} placeholders will be replaced dynamically with the values from our playbook.
Step 3: Setting Up the Inventory File
Before running the playbook, we need to define which servers Ansible should manage in an inventory file (inventory.ini):
[webserver]
foo.example.com ansible_host=192.168.1.100 ansible_user=devops ansible_ssh_private_key_file=~/.ssh/id_rsa
This file:
Specifies target hosts (foo.example.com).
Uses SSH authentication with a key.
Defines a DevOps user for managing the machine.
Step 4: Running the Playbook
Let’s check the target system before execution:
ssh devops@foo.example.com
dpkg -l | grep nginx # Check if Nginx is installed
ls /var/www/html/ # Check if index.html exists
At this point, Nginx should not be installed and the web directory should be empty.
Now, run the playbook using the following command:
ansible-playbook -i inventory.ini setup_webserver.yml
If successful, you’ll see changed status indicating that Ansible installed Nginx and deployed the template.
Step 5: Verifying the Results
Checking the Web Server
After execution, verify that Nginx is installed and the web page is deployed:
dpkg -l | grep nginx # Nginx should now be installed
cat /var/www/html/index.html # Webpage content should be present
Opening the Web Page in a Browser
Now, open your browser and navigate to:
http://foo.example.com
You should see your customized web page generated by Ansible! ?
Step 6: Demonstrating Idempotency
One of Ansible’s key features is idempotency, meaning it only makes changes when necessary.
Run the playbook again:
ansible-playbook -i inventory.ini setup_webserver.yml
This time, you’ll see an OK status instead of CHANGED, proving that Ansible only updates what’s needed.
Congratulations! You’ve Successfully Written Your First Playbook! ?
In this lecture, we covered:
Understanding Ansible Playbooks as automation recipes.
Writing a playbook to install Nginx and deploy a webpage.
Using templates (Jinja2) for dynamic content.
Setting up an inventory file for remote execution.
Running and verifying the playbook on a remote machine.
Demonstrating Ansible’s idempotency.
Next, we will explore Ansible Roles to further modularize automation!
This lesson explores Ansible modules, which are pre-built tools that automate IT tasks such as installing software, managing services, modifying files, and executing system commands.
Key Topics Covered:
1. Understanding Ansible Modules
Modules execute tasks efficiently and securely.
Examples: Software installation, file management, service control, system commands.
2. Using the Debug Module
Displays messages and variable values for troubleshooting.
Uses Jinja2 templating for dynamic content.
3. Modifying Files with Lineinfile
Used to modify specific lines in configuration files.
Example: Enabling password authentication for SSH.
4. Writing Multiple Lines with Blockinfile
Inserts multiple lines inside a file.
Example: Managing configurations with # Managed by Ansible.
5. Managing Services with the Service Module
Enables and starts services like ChronyD (NTP client) at boot.
6. Running System Commands
Command module: Runs commands without environment interpretation.
Shell module: Runs commands with environment variables and shell features (pipes, redirects).
7. Exploring Ansible Modules in the Documentation
Visit docs.ansible.com to find modules for:
- Cloud providers (AWS, Azure, VMware)
- Networking (Cisco, Juniper)
- Security (Firewall, SELinux)
What You Learned:
Debugging output using the debug module.
Modifying files with lineinfile and blockinfile.
Managing services using the service module.
Running system commands with command and shell modules.
Exploring Ansible’s vast module ecosystem.
Lesson 5 explores Ansible statements, focusing on enhancing playbooks through dynamic execution. It covers conditional execution using the when statement, which allows tasks to run only when specific conditions are met, making it useful for verifying system configurations and package installations. Loops are introduced as a way to iterate through lists and perform tasks dynamically, such as reading multiple files from a directory, which is beneficial for log processing and configuration management. Ansible facts are discussed as a method for retrieving system details like CPU, memory, OS version, and IP addresses, enabling automated system audits and adaptive task execution. The lesson also explains magic variables, which provide built-in execution context and help automate processes such as generating inventory files dynamically. Extra variables are highlighted as a way to override values from the command line, allowing for more flexibility when running playbooks, particularly in CI/CD pipelines. Finally, handlers are introduced as a mechanism for event-driven automation, ensuring tasks like service restarts only occur when necessary, reducing redundancy and improving efficiency. By mastering these concepts, users can create more adaptable, efficient, and reusable Ansible playbooks.
Congratulations! You’ve learned the core concepts of Ansible automation, and now it’s your turn to put your skills into action.
Your final project is to automate a web server deployment. You will:
Install Nginx or Apache
Deploy a custom HTML page
Ensure the service starts on system boot
Demonstrate idempotency by running the playbook twice
Share your results by uploading your playbook and screenshots
This project ties together all the essential Ansible skills you’ve learned, from installing packages to configuring services and managing system state efficiently.
Step 1: Installing PostgreSQL on a Red Hat-based System
Before diving into the final project, let's explore a real-world example of installing and configuring PostgreSQL on a Red Hat-like system using Ansible.
Playbook Breakdown
The following playbook will:
Install PostgreSQL server and dependencies
Check if PostgreSQL is initialized
Initialize the database only if needed
Start and enable PostgreSQL service on boot
PostgreSQL Playbook (install_postgres.yml)
---
- name: Install and Configure PostgreSQL
hosts: all
become: true
tasks:
- name: Install PostgreSQL and dependencies
ansible.builtin.yum:
name:
- postgresql-server
- postgresql
state: present
- name: Check if PostgreSQL is initialized
ansible.builtin.stat:
path: /var/lib/pgsql/data/pg_hba.conf
register: postgres_data
- name: Initialize PostgreSQL database
ansible.builtin.command: postgresql-setup initdb
when: not postgres_data.stat.exists
- name: Start and enable PostgreSQL service
ansible.builtin.service:
name: postgresql
state: started
enabled: yes
Step 2: Running the Playbook
Now, let's execute this playbook on a fresh Red Hat-based system and observe the changes.
Initial System State
Before running the playbook, let’s check:
rpm -qa | grep postgresql # PostgreSQL should NOT be installed
ls /var/lib/pgsql/data/ # Configuration files should be missing
systemctl status postgresql # Service should NOT exist
Expected output: PostgreSQL not installed and service does not exist.
Executing the Playbook
Run the playbook:
ansible-playbook -i inventory.ini install_postgres.yml
Expected result:
Installs PostgreSQL if missing
Checks if PostgreSQL is initialized
Initializes database only if needed
Starts and enables PostgreSQL service
Demonstrating Idempotency
Run the playbook again to test idempotency:
ansible-playbook -i inventory.ini install_postgres.yml
Expected result: OK status without unnecessary changes.
Final System Verification
Now, let's manually check the system:
rpm -qa | grep postgresql # Should list installed packages
ls /var/lib/pgsql/data/ # Configuration files should be present
systemctl status postgresql # Service should be running
Expected output: PostgreSQL installed and running successfully.
Step 3: Your Final Project – Automating a Web Server Deployment
Now, it’s your turn!
Your Task:
Install Nginx or Apache using Ansible
Deploy a custom HTML page with a welcome message
Ensure the web server starts on boot
Demonstrate idempotency by running the playbook twice
Example Playbook (deploy_webserver.yml)
---
- name: Deploy a Web Server
hosts: all
become: true
tasks:
- name: Install Nginx
ansible.builtin.yum:
name: nginx
state: present
- name: Deploy HTML Page
ansible.builtin.copy:
content: "<h1>Welcome to Ansible Automation!</h1>"
dest: /usr/share/nginx/html/index.html
- name: Start and Enable Web Server
ansible.builtin.service:
name: nginx
state: started
enabled: yes
How to Submit Your Project
Take screenshots showing:
Playbook execution results
Successful web server deployment
Idempotency test (running playbook twice)
Upload your playbook and screenshots to the project gallery and share your experience!
Final Thoughts & Congratulations!
You have successfully:
Installed and configured software with Ansible
Used conditionals (when), loops, and handlers
Demonstrated idempotency for reliable automation
Written and executed a full Ansible Playbook
You are now well on your way to mastering IT automation with Ansible!
Next Steps:
- Explore Ansible Roles to structure playbooks.
- Automate cloud deployments (AWS, Azure, VMware).
- Integrate CI/CD pipelines with Ansible automation.
Keep practicing, keep automating, and happy Ansible coding!
Welcome to IT Automation with Ansible Quickstart! This fast-paced, hands-on course is designed to give you a practical introduction to Ansible—one of the most powerful and widely used IT automation tools today.
This course, led by Luca Berton, a seasoned Ansible Expert with five published books, will equip you with essential automation skills in just 40 minutes. It will help you transform the way you manage IT infrastructure.
Why Learn Ansible?
Whether you’re a DevOps Professional, System Administrator, or IT Enthusiast, mastering Ansible enables you to:
Streamline workflows and eliminate manual tasks.
Automate server provisioning and application deployment.
Manage configurations across multiple systems effortlessly.
Enhance security compliance with consistent automation.
Unlike traditional automation tools, Ansible is agentless, simple, and highly flexible, making it the preferred choice for modern IT automation.
What You Will Learn
This hands-on course takes you from zero to automation-ready by covering:
Ansible Fundamentals – Learn how Ansible works and why it's so powerful.
Inventory Management – Organize and define the infrastructure you want to automate.
Using Ansible Modules – Explore built-in modules to manage servers, files, and applications.
Running Ad-Hoc Commands – Execute quick, one-time automation tasks.
Writing Your First Playbook – Automate real-world infrastructure setups.
Best Practices for Automation – Ensure smooth, scalable, and error-free automation.
What You’ll Achieve
By the end of this course, you will:
Understand Ansible’s core functionality and agentless architecture.
Write and execute Ansible playbooks to automate key IT tasks.
Optimize workflows, reduce human errors, and improve efficiency.
Be ready to apply Ansible automation in real-world environments.
Who Should Take This Course?
System Administrators looking to automate server management.
DevOps Engineers streamlining CI/CD pipelines.
Cloud & IT Professionals managing infrastructure at scale.
IT Enthusiasts eager to gain hands-on experience with automation.
Why Take This Course?
Short & Effective – Learn essential Ansible skills in just 30 minutes.
Hands-On Learning – Practical, step-by-step guidance to automation success.
No Prior Experience Needed – Beginner-friendly, no programming required.
Industry-Relevant Skills – Boost your career with cutting-edge automation expertise.
Ready to Take Control of Your Infrastructure?
Join now and start automating with Ansible Quickstart! Transform the way you work—boost productivity, reduce manual effort, and manage IT infrastructure effortlessly.