Write a playbook that installs and starts Nginx on Ubuntu web servers.
BeginnerHands-onWrite-an-Ansible-playbook tasks
Answer
- name: Install and run nginx
hosts: web
become: true
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
cache_valid_time: 3600
- name: Start and enable nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
Run: ansible-playbook -i inventory.ini site.yml --check --diff, then without --check.
Key trick → cache_valid_time stops apt from refreshing on every run, and modules with state: are idempotent, so a second run reports changed=0.
More on Write-an-Ansible-playbook tasks
Write an inventory with groups, a group of groups, and per-group variables.BeginnerWrite a playbook that deploys a config file from a Jinja2 template and restarts the service only if it changed.BeginnerWrite a role structure for a Node.js app and show what goes in each folder.IntermediateWrite a task that creates multiple users with SSH keys from a list.BeginnerWrite a rolling deploy playbook: 2 servers at a time, removed from the load balancer during the update.AdvancedEncrypt a database password with Ansible Vault and use it in a playbook.Beginner