Tutorials · DevOps & Containers

How to Automate Switch and Server Configuration With Ansible

Stop typing the same commands on fifty devices. Describe the desired state once in an Ansible playbook and apply it to every switch, gateway and edge server over SSH.

Advanced20 min6 steps

Before you start

  • Ansible on a management host or jump host
  • SSH key access to the devices
  • A test device or lab

Step by step

  1. Write an inventory

    Group devices by role and site.

    # inventory.ini
    [edge]
    site1-edge ansible_host=10.1.99.20
    site2-edge ansible_host=10.2.99.20
    
    [edge:vars]
    ansible_user=admin
    
  2. Check connectivity

    Ansible connects over SSH with your key.

    ansible -i inventory.ini edge -m ping
    
  3. Write a playbook

    Describe the desired state: here, Docker installed and the site stack deployed on every edge server.

    # edge.yml
    - hosts: edge
      become: true
      tasks:
        - name: Docker is installed
          ansible.builtin.package: { name: docker.io, state: present }
        - name: Compose files are in place
          ansible.builtin.copy: { src: files/compose.yaml, dest: /opt/site/compose.yaml }
        - name: Stack is running
          ansible.builtin.command: docker compose -f /opt/site/compose.yaml up -d
          changed_when: false
    
  4. Dry run first

    --check --diff shows what would change without changing anything.

    ansible-playbook -i inventory.ini edge.yml --check --diff --limit site1-edge
    
  5. Roll out in waves

    Apply to one site, verify, then the rest.

    ansible-playbook -i inventory.ini edge.yml --limit site1-edge
    ansible-playbook -i inventory.ini edge.yml
    
  6. Network devices

    For switches, use the vendor's Ansible collection or its CLI over SSH with the same inventory and dry-run discipline.

Commands are generic, industry-standard examples. Exact syntax and menu names vary by model and firmware; check your device's manual on the Documentation page.

Pro tips

  • Keep playbooks in Git and review them like code.
  • Store secrets in Ansible Vault, never in plain text.