Skip to content

Latest commit

 

History

History
83 lines (57 loc) · 1.52 KB

File metadata and controls

83 lines (57 loc) · 1.52 KB

Ansible Use Case: Package Management Across Environments

Overview

This lab demonstrates how to use an Ansible playbook to install required packages (curl and git) on a remote server. It ensures idempotent execution and highlights package management automation.


Project Structure

.
├── hosts.ini
└── install_packages.yml

Implementation Steps

1. Create Inventory File

vi hosts.ini
[webservers]
server1 ansible_user=server1_admin ansible_ssh_pass='server1_admin@123!' ansible_become=true ansible_become_pass='server1_admin@123!'

2. Create Playbook

vi install_packages.yml
---
- name: Install curl and git on server1
  hosts: server1
  become: true

  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes
        cache_valid_time: 3600

    - name: Install curl and git
      apt:
        name:
          - curl
          - git
        state: present

3. Run Playbook

ansible-playbook -i hosts.ini install_packages.yml

Executes the playbook and installs required packages on server1.


Key Concepts

  • Playbook-based package management
  • Installing multiple packages using apt
  • Idempotent execution (state: present)
  • Privilege escalation using become

Summary

This lab demonstrates how Ansible can automate package installation across servers, ensuring required tools are installed only when needed.