This Ansible playbook deploys and configures an Nginx web server on a target machine.
- Ansible Control Node: An Ubuntu machine with Ansible installed.
- Managed Node: An EC2 instance (or any other server) that you want to configure.
- SSH Access: The control node must have SSH access to the managed node using key-based authentication.
Ansible needs an inventory file to know which hosts to connect to. Create a file named inventory in the same directory as the playbook with the following content:
[web_server]
your_server_ip ansible_user=your_usernameReplace your_server_ip with the public IP address of your EC2 instance and your_username with the SSH username (e.g., ubuntu for an Ubuntu EC2 instance).
Execute the playbook using the following command:
ansible-playbook -i inventory deploy_nginx.ymlThis command tells Ansible to use the inventory file to connect to the managed node and run the tasks defined in deploy_nginx.yml.
Once the playbook has finished successfully, you can access the deployed website by navigating to http://your_server_ip in your web browser.
If you encounter an error like Unable to reload service nginx, it's likely due to a syntax error in the Nginx configuration. To debug this:
-
SSH into the managed node:
ssh your_username@your_server_ip
-
Check the Nginx configuration for syntax errors:
sudo nginx -t
-
Review the Nginx service status and logs for more details:
systemctl status nginx.service journalctl -xeu nginx.service