📋
Infrastructure as Code

Ansible

Agentless IT automation and configuration management

🐧 Linux🍎 Mac🪟 Windows📋 Quick reference →
Reviewed: Tested on: Kubernetes 1.29 · Terraform 1.8 · Ubuntu 22.04

What is this?

Ansible configures many servers at once — install software, copy files, restart services.

📁

Config files for Ansible

Where to create or edit the main configuration — paths below match the setup steps.

  • inventory.ini

    Location: Project root or inventories/

    Host groups and connection vars

  • playbook.yml

    Location: Project root or playbooks/

    Tasks to run on target hosts

  • ansible.cfg

    Location: Project root or /etc/ansible/

    Defaults — inventory path, roles, SSH

📥

Step 01

Install Ansible

(01)Install Ansible

Linux
1# Ubuntu / Debian
2sudo apt-get update
3sudo apt-get install -y ansible
4
5# Or via pip (recommended for latest version)
6sudo apt-get install -y python3-pip
7pip3 install ansible --user
8
9# RHEL / Fedora
10sudo dnf install -y ansible

(02)Create Inventory File

Linux
1cat > inventory.ini << 'EOF'
2[webservers]
3web1.example.com
4web2.example.com
5
6[dbservers]
7db1.example.com
8EOF

Step 02

Verify Installation

(01)Check Version

Linux
1ansible --version

(02)Ping Hosts

Linux
1ansible all -i inventory.ini -m ping

Step 03

Manage Ansible

(01)Ad-hoc Commands

Linux
1# Run command on all hosts
2ansible all -i inventory.ini -a "uptime"
3
4# Run with sudo
5ansible webservers -i inventory.ini -a "apt update" -b
6
7# Copy file
8ansible webservers -i inventory.ini -m copy -a "src=./file dest=/tmp/file"

(02)Playbook Operations

Linux
1# Run playbook
2ansible-playbook -i inventory.ini site.yml
3
4# Check mode (dry run)
5ansible-playbook -i inventory.ini site.yml --check
6
7# Limit to specific hosts
8ansible-playbook -i inventory.ini site.yml --limit webservers

📋Config templates

1 YAML template for Ansible. Copy and deploy after setup.

1 ready-to-copy template. Expand one, copy the YAML, then run the deploy commands.

Jenkinsfile

yaml
1pipeline {
2 agent any
3
4 environment {
5 DOCKERHUB_CREDENTIALS = credentials('dockerhub')
6 }
7
8 stages {
9 stage('Docker Login') {
10 steps {
11 sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
12 }
13 }
14 stage('Build & Push Image') {
15 steps {
16 sh """
17 cd laravel-app
18 docker build -t $DOCKERHUB_CREDENTIALS_USR/laravel-app:latest .
19 docker push $DOCKERHUB_CREDENTIALS_USR/laravel-app:latest
20 """
21 }
22 }
23 stage('Deploy with Compose') {
24 steps {
25 sh """
26 cd laravel-app
27 docker compose pull
28 docker compose up -d
29 docker compose ps
30 """
31 }
32 }
33 }
34}

laravel-app/docker-compose.yml

yaml
1services:
2 app:
3 image: youruser/laravel-app:latest
4 ports:
5 - "8080:80"
6 depends_on:
7 - db
8 environment:
9 DB_HOST: db
10 DB_DATABASE: laravel
11 DB_USERNAME: root
12 DB_PASSWORD: secret
13
14 db:
15 image: mysql:5.7
16 environment:
17 MYSQL_DATABASE: laravel
18 MYSQL_ROOT_PASSWORD: secret
19 volumes:
20 - db_data:/var/lib/mysql
21
22volumes:
23 db_data:
📄

Step 01

Run Jenkins Pipeline

(01)Configure Jenkins

Linux
1# Add Docker Hub credentials (ID: dockerhub) in Jenkins
2# Install Docker + docker-compose on Jenkins agent
3# Create Pipeline job pointing to Jenkinsfile