How would you install nginx on 10 servers using Ansible?
⚡
Quick Answer
List the 10 hosts in an inventory group, then run a playbook that targets that group with a package task (apt/yum) to ensure nginx is present. Ansible runs it in parallel over SSH, idempotently.
Detailed Answer
The strength here is idempotency and parallelism: state=present installs only where missing, and re-running is safe. Use the generic package module or the OS-specific one, add a handler to start/enable the service, and template the config. Scaling from 10 to 1000 servers is just a bigger inventory — the playbook does not change.
Code Example
- hosts: web
become: true
tasks:
- name: Install nginx
ansible.builtin.package:
name: nginx
state: present
- name: Ensure nginx running
ansible.builtin.service: { name: nginx, state: started, enabled: true }💡
Interview Tip
Stress idempotency and that scaling to more hosts just means a bigger inventory — the playbook is unchanged.
ansibleplaybooknginx