CONFIGURING APACHE WEBSERVER USING ANSIBLE
Amit Sharma
CKA || 1xAWS || 4xGCP || 1xAzure || 2xRedHat Certified || DevOps Engineer [???????]@Searce Inc || Freelancer || Terraform || Ansible || GitLab || Jenkins || Kubernetes || Docker || Openshift || AWS || GCP || Azure
Hello Guys, Back with another article. In this you will find how we can automate Apache WebServer using the linux automation tool i.e Ansible.
The Apache HTTP Server, colloquially called Apache, is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.
What is Ansible ?
Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn't depend on agent software and has no additional security infrastructure, so it's easy to deploy.
How Ansible works
In Ansible, there are two categories of computers: the control node and managed nodes. The control node is a computer that runs Ansible. There must be at least one control node, although a backup control node may also exist. A managed node is any device being managed by the control node.
Ansible works by connecting to nodes (clients, servers, or whatever you're configuring) on a network, and then sending a small program called an Ansible module to that node. Ansible executes these modules over SSH and removes them when finished. The only requirement for this interaction is that your Ansible control node has login access to the managed nodes. SSH Keys are the most common way to provide access, but other forms of authentication are also supported.
Ansible playbooks
While modules provide the means of accomplishing a task, the way you use them is through an Ansible playbook. A playbook is a configuration file written in YAML that provides instructions for what needs to be done in order to bring a managed node into the desired state. Playbooks are meant to be simple, human-readable, and self-documenting. They are also idempotent, meaning that a playbook can be run on a system at any time without having a negative effect upon it. If a playbook is run on a system that's already properly configured and in its desired state, then that system should still be properly configured after a playbook runs.
Modules in Ansible
Modules (also referred to as “task plugins†or “library pluginsâ€) are discrete units of code that can be used from the command line or in a playbook task. Ansible executes each module, usually on the remote managed node, and collects return values
Here in this article you will find the complete end to end automation of hadoop hdfs cluster using ansible.
So, you need to write the playbooks of respective configuration on the target node. we need to first configure the namenode and start it. The playbook of namenode is written below.
Variables in Ansible:
Ansible uses variables to manage differences between systems. With Ansible, you can execute tasks and playbooks on multiple different systems with a single command. ... You can define these variables in your playbooks, in your inventory, in re-usable files or roles, or at the command line.
Ansible Installation In Below Slides:
Roles
Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users.
Role Creation:
Contents in Role:
So, you can run the ansible and configure the systems easily !!
Variables for Configuring Yum & Apache Web Server
--- # vars file for YumConfiguration mounting_path: "/dvd" dvd_iso: "/dev/cdrom" appstream_loc: "file:///dvd/AppStream" baseos_loc: "file:///dvd/BaseOS" installing_software: "httpd" src_loc: "/root/my.html"
dest_loc: "/var/www/html"
Handlers for Configuring Yum & Apache Web Server
Handlers are just like regular tasks in an Ansible playbook (see Tasks) but are only run if the Task contains a notify directive and also indicates that it changed something. For example, if a config file is changed, then the task referencing the config file templating operation may notify a service restart handler.
Handlers will play a vital role in restarting the httpd server. when there is some change in the code then only the apache webserver will restart again. This will reduce the time taking by other servers.
# handlers file for YumConfiguration - name: web_restart service: name: "{{ installing_software }}" state: restarted
Tasks for Configuring Yum & Apache Web Server
In this below code. you will find the code for yum configuration as well as configuration of apache webserver.
--- # tasks file for YumConfiguration - name: "Creating Directory" file: state: directory path: "{{ mounting_path }}" - name: "Mounting /dev/cdrom to /dvd" mount: src: "{{ dvd_iso }}" path: "{{ mounting_path }}" state: mounted fstype: "iso9660" - name: "AppStream" yum_repository: baseurl: "{{ appstream_loc }}" name: "appstream" description: "dvd1" gpgcheck: no - name: "BaseOS" yum_repository: baseurl: "{{ baseos_loc }}" name: "baseos" description: "dvd2" gpgcheck: no - name: "Installing HTTPD" package: name: "{{ installing_software }}" state: present - name: "Copying the Files" template: src: "{{ src_loc }}" dest: "{{ dest_loc }}" notify: "web_restart" #- name: "do not permit traffic in default zone on port 80/tcp" # ansible.posix.firewalld: # port: 80/tcp # permanent: yes # state: disabled - name: "Staring Httpd" service: name: "{{ installing_software }}" state: started
Main Playbook:
In main playbook you need to write the roles that you have created. When you run the main playbook role will run behind the scene.
- hosts: ApacheWebServer roles: - role: YumConfiguration
Below command you can use to run the respective playbook.
ansible-playbook playnook_name.yml
Each Step is now you can see while running the playbook. When you copy something in the html files then only your webserver will restart. This is due to we have taken into consideration of handlers in this task.
Finally the webpage appears.