AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible vs Kubernetes: When to Use Each & How They Work Together

By Luca Berton · Published 2024-01-01 · Category: installation

Ansible vs Kubernetes comparison. When to use Ansible, when to use Kubernetes, and how to combine them.

Ansible vs Kubernetes: When to Use Each & How They Work Together

Ansible and Kubernetes solve different problems in the automation stack. Ansible automates infrastructure provisioning and configuration. Kubernetes orchestrates containers. This guide explains when to use each and how they complement each other.

See also: Ansible vs Kubernetes: Key Differences & When to Use Each (2026 Guide)

Quick Comparison

AspectAnsibleKubernetes
Primary PurposeConfiguration management & provisioningContainer orchestration
What It ManagesServers, networks, cloud, applicationsContainers and microservices
ArchitectureAgentless (push-based)Agent-based (kubelet on each node)
State ModelProcedural (tasks run in order)Declarative (desired state)
LanguageYAML playbooksYAML manifests
ScopeAny infrastructure, any applicationContainerized applications
Learning CurveLowerHigher
ScalingManual (serial/parallel tasks)Automatic (HPA, VPA)
Self-HealingWith check + remediationBuilt-in (restarts, replaces)

When to Use Ansible

Ansible is the right choice for:

  • Server provisioning — Install packages, configure services, manage users
  • Network automation — Configure routers, switches, firewalls
  • Cloud infrastructure — Create VMs, VPCs, databases, load balancers
  • Configuration management — Ensure consistent server state
  • Application deployment — Deploy traditional (non-containerized) apps
  • Kubernetes cluster setup — Install and configure K8s itself
  • Multi-platform automation — Linux, Windows, network devices, cloud APIs
  • One-time operations — Migrations, patches, data transformations
# Ansible: Configure a web server
- hosts: webservers
  become: true
  tasks:
    - ansible.builtin.apt:
        name: [nginx, certbot]
        state: present
    - ansible.builtin.template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: reload nginx

When to Use Kubernetes

Kubernetes is the right choice for:

  • Container orchestration — Run and manage Docker containers
  • Microservices — Service discovery, load balancing, communication
  • Auto-scaling — Scale pods based on CPU, memory, or custom metrics
  • Self-healing — Automatically restart failed containers
  • Rolling updates — Zero-downtime deployments of containerized apps
  • Multi-environment parity — Dev/staging/production consistency
# Kubernetes: Deploy a web application
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    spec:
      containers:
        - name: web
          image: myapp:2.5.0
          ports:
            - containerPort: 8080
          resources:
            limits:
              memory: "256Mi"
              cpu: "500m"

See also: Ansible vs Docker: Differences, Use Cases, and How to Use Both

Using Ansible AND Kubernetes Together

The most powerful approach is using both:

1. Ansible Provisions, Kubernetes Orchestrates

Ansible → Provision infrastructure (VMs, networking, storage)
       → Install Kubernetes cluster
       → Configure cluster (RBAC, networking, monitoring)
       → Deploy applications to Kubernetes
Kubernetes → Run containers
           → Scale applications
           → Self-heal failures
           → Manage service mesh

2. Ansible Deploys to Kubernetes

# Use Ansible to deploy K8s resources
- name: Deploy application to Kubernetes
  hosts: localhost
  tasks:
    - name: Create namespace
      kubernetes.core.k8s:
        state: present
        definition:
          apiVersion: v1
          kind: Namespace
          metadata:
            name: production

    - name: Deploy application
      kubernetes.core.k8s:
        state: present
        src: manifests/deployment.yml

    - name: Wait for rollout
      kubernetes.core.k8s_info:
        kind: Deployment
        name: web-app
        namespace: production
      register: deployment
      until: deployment.resources[0].status.readyReplicas == deployment.resources[0].spec.replicas
      retries: 30
      delay: 10

3. Ansible Manages What Kubernetes Can't

  • Configure underlying OS on K8s nodes
  • Manage external services (databases, DNS, certificates)
  • Set up monitoring and logging infrastructure
  • Handle cloud-provider-specific resources
  • Manage non-containerized legacy applications

Architecture Comparison

Ansible (Push-Based)

Control Node → SSH → Managed Hosts
                  → Execute modules
                  → Return results
  • No agent required on managed hosts
  • Control node pushes changes when you run a playbook
  • Stateless — no persistent state to manage

Kubernetes (Declarative)

User → kubectl → API Server → etcd (state store)
                            → Scheduler → Nodes (kubelet)
                            → Controller Manager (reconciliation loop)
  • Kubelet agent runs on every node
  • Continuously reconciles desired state vs actual state
  • etcd stores all cluster state

Real-World Scenario

A typical production setup uses both:

  1. Terraform provisions cloud infrastructure (VPCs, VMs, managed services)
  2. Ansible configures VMs and installs Kubernetes
  3. Kubernetes runs containerized applications
  4. Ansible handles Day-2 operations (upgrades, patches, compliance)
Day 0: Terraform → Create VMs, networking, RDS, S3
Day 1: Ansible   → Install K8s, configure nodes, set up monitoring
Day 1: Kubectl   → Deploy applications to K8s
Day 2: Ansible   → OS patches, K8s upgrades, compliance checks
Day 2: K8s       → Auto-scale, self-heal, rolling updates

FAQ

Is Ansible a replacement for Kubernetes?

No. They serve different purposes. Ansible automates infrastructure and configuration. Kubernetes orchestrates containers. Most production environments use both — Ansible for infrastructure and Kubernetes for application workloads.

Can Ansible deploy to Kubernetes?

Yes. The kubernetes.core collection provides modules to manage Kubernetes resources (Deployments, Services, ConfigMaps) from Ansible playbooks. Install with ansible-galaxy collection install kubernetes.core.

Should I learn Ansible or Kubernetes first?

Start with Ansible — it has a lower learning curve and broader applicability. You'll use Ansible skills even when working with Kubernetes (provisioning clusters, managing nodes). Then learn Kubernetes when you need container orchestration.

Can Ansible replace Kubernetes for scaling?

Not effectively. Ansible can deploy and configure application instances, but it doesn't provide automatic scaling, self-healing, or built-in load balancing. Kubernetes excels at dynamic container management that would be very complex to replicate with Ansible alone.

How do Ansible and Kubernetes handle state differently?

Ansible is procedural — it runs tasks in order and each task makes a change. Kubernetes is declarative — you define the desired state and Kubernetes continuously works to maintain it. Kubernetes has a reconciliation loop; Ansible runs when you tell it to.

See also: Ansible for Kubernetes: Automate K8s Cluster Management and Application Deployment

Conclusion

Ansible and Kubernetes are complementary, not competing tools. Use Ansible for infrastructure provisioning, configuration management, and Day-2 operations. Use Kubernetes for container orchestration, auto-scaling, and self-healing. Together, they provide complete automation from infrastructure to application.

Related reading: running Ansible against Kubernetes clusters covers this in real-world detail.

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home