Automating Web Server Deployment: A Guide to Installing Apache with Ansible
Automation is at the heart of modern IT, and Ansible stands out as a powerful tool in this environment. This popular automation tool allows for remote installation, configuration, and management of various systems and services, reducing the need for manual intervention on each server. In this guide, we will look at how to use Ansible to install and configure Apache2 on multiple servers at once using YAML Playbooks.
Setting Up the Environment
Before diving into the automation process, ensure you have Ansible installed on the master server. Additionally, make sure that SSH communication is established between the master server and the target hosts (slaves).
Ansible Playbook: Installing Apache2
Ansible Playbooks are written in YAML, utilizing a module-based format for efficient automation. Let’s create a playbook (apache2.yml
) to install Apache2 on our target servers:
- hosts: servers
sudo: yes
tasks:
- name: Install Apache2
apt: name=apache2 update_cache=yes state=latest
- name: Enable mod_rewrite
apache2_module: name=rewrite state=present
notify:
- restart apache2
handlers:
- name: Restart Apache2
service: name=apache2 state=restarted
Explanation:
- hosts: servers: Specifies the server group name.
- sudo: yes: Grants sudo privileges for the tasks.
- tasks: Defines a series of tasks to be executed.
- handlers: Specifies handlers for notifications.
Save the playbook and run it using the following command:
ansible-playbook apache2.yml
This playbook installs Apache2 on all specified servers and ensures mod_rewrite is enabled. The handlers
section restarts Apache2 if changes are detected.
PLAY [servers] ********************************************************************************
TASK [Gathering Facts] ************************************************************************
ok: [slave2]
ok: [slave1]
TASK [install apache2] ************************************************************************
changed: [slave1]
changed: [slave2]
TASK [enabled mod_rewrite] ********************************************************************
changed: [slave1]
changed: [slave2]
RUNNING HANDLER [restart apache2] *************************************************************
changed: [slave2]
changed: [slave1]
PLAY RECAP ************************************************************************************
slave1 : ok=4 changed=3 unreachable=0 failed=0
slave2 : ok=4 changed=3 unreachable=0 failed=0
Verifying the Installation
After the playbook execution completes successfully, Apache2 should be installed on the specified servers. Verify this by accessing the Apache2 welcome page through the web browser using the IP addresses of the slave servers.
Conclusion
Congratulations! You’ve successfully automated the installation of Apache2 on multiple servers using Ansible. This approach not only saves time but also ensures consistency across your infrastructure. In the next article, we’ll delve into deploying a static website with images using Ansible Playbooks. Keep reading and sharing our posts for more automation insights. Happy automating!
Great Article!!! thanks for sharing your ideas and thoughts with us.
Java Training in Chennai
ReactJS Training in Chennai
Best CCNA Training Institute in Chennai
ccna certification in Chennai
CCNA Training in Chennai
Much thanks for installing apache …
regards
Tangki Panel
Tangki Fiberglass
Jual Septic Tank
This is really such a great article. I really enjoyed it. Thank you for sharing.
IEEE Projects on Cloud Computing 2019
JavaScript Training in Chennai
Final Year Projects for CSE
JavaScript Training in Chennai
Wonderful blog!!! More Useful to us… Thanks for sharing with us…
Selenium Training in Bangalore
Selenium Training in Coimbatore
Selenium Course in Bangalore
selenium course in coimbatore
Java Training in Bangalore
Python Training in Bangalore
IELTS Coaching in Coimbatore
Java Training in Coimbatore
Welcome! Exceptionally supportive counsel inside this article! It is the little changes that produce the biggest changes. Much obliged for sharing!
evrmag