$ -weight: 600;">sudo -weight: 500;">apt -weight: 500;">update
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">update
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">update
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install ansible -y
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install ansible -y
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install ansible -y
ansible --version
ansible --version
ansible --version
ansible [core 2.14.3] config file = /etc/ansible/ansible.cfg configured module search path = ['/home/youruser/.ansible/plugins/modules', ...] ansible python module location = /usr/lib/python3/dist-packages/ansible executable location = /usr/bin/ansible python version = 3.10.6 (...)
ansible [core 2.14.3] config file = /etc/ansible/ansible.cfg configured module search path = ['/home/youruser/.ansible/plugins/modules', ...] ansible python module location = /usr/lib/python3/dist-packages/ansible executable location = /usr/bin/ansible python version = 3.10.6 (...)
ansible [core 2.14.3] config file = /etc/ansible/ansible.cfg configured module search path = ['/home/youruser/.ansible/plugins/modules', ...] ansible python module location = /usr/lib/python3/dist-packages/ansible executable location = /usr/bin/ansible python version = 3.10.6 (...)
-weight: 600;">sudo nano /etc/ansible/hosts
-weight: 600;">sudo nano /etc/ansible/hosts
-weight: 600;">sudo nano /etc/ansible/hosts
[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11 [webservers:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsa
[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11 [webservers:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsa
[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11 [webservers:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsa
ansible webservers -m ping
ansible webservers -m ping
ansible webservers -m ping
mkdir nginx-deploy && cd nginx-deploy
mkdir nginx-deploy && cd nginx-deploy
mkdir nginx-deploy && cd nginx-deploy
nano site.yml
nano site.yml
nano site.yml
---
- name: Install and configure Nginx on Ubuntu hosts: webservers become: yes tasks: - name: Update -weight: 500;">apt cache -weight: 500;">apt: update_cache: yes cache_valid_time: 3600 - name: Install Nginx -weight: 500;">apt: name: nginx state: present - name: Ensure Nginx is running and enabled systemd: name: nginx state: started enabled: yes - name: Copy custom Nginx config copy: src: files/nginx.conf dest: /etc/nginx/nginx.conf owner: root group: root mode: '0644' notify: Restart Nginx handlers: - name: Restart Nginx systemd: name: nginx state: restarted
---
- name: Install and configure Nginx on Ubuntu hosts: webservers become: yes tasks: - name: Update -weight: 500;">apt cache -weight: 500;">apt: update_cache: yes cache_valid_time: 3600 - name: Install Nginx -weight: 500;">apt: name: nginx state: present - name: Ensure Nginx is running and enabled systemd: name: nginx state: started enabled: yes - name: Copy custom Nginx config copy: src: files/nginx.conf dest: /etc/nginx/nginx.conf owner: root group: root mode: '0644' notify: Restart Nginx handlers: - name: Restart Nginx systemd: name: nginx state: restarted
---
- name: Install and configure Nginx on Ubuntu hosts: webservers become: yes tasks: - name: Update -weight: 500;">apt cache -weight: 500;">apt: update_cache: yes cache_valid_time: 3600 - name: Install Nginx -weight: 500;">apt: name: nginx state: present - name: Ensure Nginx is running and enabled systemd: name: nginx state: started enabled: yes - name: Copy custom Nginx config copy: src: files/nginx.conf dest: /etc/nginx/nginx.conf owner: root group: root mode: '0644' notify: Restart Nginx handlers: - name: Restart Nginx systemd: name: nginx state: restarted
mkdir files
nano files/nginx.conf
mkdir files
nano files/nginx.conf
mkdir files
nano files/nginx.conf
ansible-playbook site.yml
ansible-playbook site.yml
ansible-playbook site.yml
ansible-playbook site.yml --check
ansible-playbook site.yml --check
ansible-playbook site.yml --check
mkdir templates
nano templates/nginx.conf.j2
mkdir templates
nano templates/nginx.conf.j2
mkdir templates
nano templates/nginx.conf.j2
worker_processes {{ ansible_processor_vcpus }};
events { worker_connections 1024;
} http { include /etc/nginx/mime.types; default_type application/octet-stream; server { listen 80; server_name {{ server_hostname }}; location / { root /var/www/html; index index.html; } }
}
worker_processes {{ ansible_processor_vcpus }};
events { worker_connections 1024;
} http { include /etc/nginx/mime.types; default_type application/octet-stream; server { listen 80; server_name {{ server_hostname }}; location / { root /var/www/html; index index.html; } }
}
worker_processes {{ ansible_processor_vcpus }};
events { worker_connections 1024;
} http { include /etc/nginx/mime.types; default_type application/octet-stream; server { listen 80; server_name {{ server_hostname }}; location / { root /var/www/html; index index.html; } }
}
- name: Deploy templated Nginx config template: src: templates/nginx.conf.j2 dest: /etc/nginx/nginx.conf notify: Restart Nginx
- name: Deploy templated Nginx config template: src: templates/nginx.conf.j2 dest: /etc/nginx/nginx.conf notify: Restart Nginx
- name: Deploy templated Nginx config template: src: templates/nginx.conf.j2 dest: /etc/nginx/nginx.conf notify: Restart Nginx - ๐ Prerequisites โ What You Need
- โ๏ธ Installing Ansible on Ubuntu
- ๐ง Configuring Ansible Hosts
- ๐ฆ Creating a Playbook to Install Nginx
- ๐ Preparing Files and Running the Playbook
- ๐ง Best Practices for Reliable Nginx Deployment
- ๐ Use Variables and Templates
- ๐ Idempotency โ Your Best Friend
- ๐งช Test in Stages
- ๐ฉ Final Thoughts
- โ Frequently Asked Questions
- Can I use Ansible to -weight: 500;">install Nginx on multiple Ubuntu versions?
- Do I need to -weight: 500;">install Ansible on the target Ubuntu servers?
- How do I handle Nginx configuration reloads without downtime? - Control node : Your laptop or jump box. Linux preferred. Ubuntu/Debian? Even better. Windows users โ use WSL2. Ansible doesnโt run natively on Windows, and wrestling with Cygwin ainโt worth it. (like this one โ small, wry, self-aware)
- Managed nodes : Your Ubuntu boxes. Python 3 installed. SSH access open.
- SSH key-based auth. Non-negotiable. Trust me, password prompts mid-playbook destroy your flow. And your dignity.
- Sudo rights on target machines. Because Nginx needs root, and youโre not running everything as root. Right? - become: yes โ runs tasks with -weight: 600;">sudo.
- -weight: 500;">apt module โ Ubuntuโs best friend.
- systemd โ keeps Nginx alive after reboots.
- copy task โ swaps in your config, if youโve got one.
- Handlers โ only run when triggered. Like a -weight: 500;">restart after config changes. Elegant. - Start with one server โ not all five.
- Use ansible-playbook --step to go task-by-task. Pause. Verify.
- Add tags โ makes debugging easier: name: Install Nginx
-weight: 500;">apt:
name: nginx
state: present
tags: -weight: 500;">install
- name: Install Nginx
-weight: 500;">apt:
name: nginx
state: present
tags: -weight: 500;">install - name: Install Nginx
-weight: 500;">apt:
name: nginx
state: present
tags: -weight: 500;">install