Today we will learn how to install/uninstall Apache using ansible playbook on Linux or AIX machines. The most widely used web server in the world is the Apache HTTP server which provides pretty powerful features including extensive integration and media support as well. We’ve used RedHat Enterprise Linux 8 for our ansible control server and Red Hat Linux 6, AIX 7 machines for our target servers.
Ansible is an open-source automated tool that allows you to remotely configure systems across large server farms, install software, and perform complex tasks without manually log in to each server. In contrast to different other options, Ansible is installed on a single host, which can even be your local computer, and uses SSH to communicate with each remote host. This makes it very fast when configuring new servers because there is no need to install prerequisite software packages on each new server. It is very user-friendly because it uses a script in YAML format and uses a simple module-based syntax.
What is Ansible Playbook?
Playbooks are nothing but ansible configuration management scripts that can be used to manage the deployment and configuration of managed nodes. The playbook contains a set of policies or a set of steps in a regular IT process that you want your managed nodes to enforce. The ansible playbooks are written and developed in a simple text language known as YAML. A simple Ansible playbook contains one or multiple tasks.
Install Apache Using Ansible Playbook -- apache.yaml:
The below-mentioned playbook to install apache in Redhat Linux is written in YAML, and it contains multiple plays or tasks. Each task should have a name to display the current activity on the screen when running the script. This output is for human use, so it's great to describe each task step well.
With the help of this ansible-playbook, first of all, we will install the latest version of apache using the yum module, and then in the second step copy the demo HTML file to /var/www/html/ directory. After that, we will start the httpd service and enabled port 80 for traffic.
- hosts: you can mention the IP address of the remote machine on which you want to install apache. In this case, I have used "all" and it means that we can run this playbook on all machines. Also this way I can filter the machine at the time executing the playbook. However, it is recommended to replace it with your IP address to avoid any problem at the end.
- gather_facts: will gather information about remote machines first.
- name: name of the play or task. It should be descriptive.
- tags: tags are used to run a specific task or play from a whole playbook.
- yum: yum is the module used you can use apt-get according to your choice. Like yum there are other modules used in this playbook e.g. copy, service, etc.
---
-
hosts: all
gather_facts: true
become: true
tasks:
-
name: "1. Install Latest HTTPD/Apache"
tags: install_apache
yum:
name: httpd
state: present
-
name: "2. Copy dummy index.html file”
tags: copy_index_file
copy:
src: /tmp/index.html
dest: /var/www/html/index.html
mode: '0664'
-
name: "3. Start httpd service"
tags: start_apache
service:
enabled: true
name: httpd
state: started
-
name: "4. Add apache ip table rule"
tags: addport80_iptable
# below is a one line command
# due to space issue I break it
# in two lines
command: "iptables -A INPUT -m state
--state NEW -m tcp -p tcp --dport 80 -j ACCEPT"
-
name: "5. Save iptable rule"
tags: save_iptable
command: iptables-save
How to Stop Apache using Ansible Playbook -- stopapache.yaml
This playbook contains only one play and when it runs it will stop the apache service.
---
-
hosts: all
gather_facts: true
become: true
tasks:
-
name: "1. Stop httpd service"
tags: stop_apache
service:
name: httpd
state: stopped
How to Restart Apache using Ansible Playbook -- restartapache.yaml
This playbook is used to restart the apache service.
---
-
hosts: all
gather_facts: true
become: true
tasks:
-
name: "1. Restart httpd service"
tags: restart_apache
service:
name: httpd
state: restarted
Uninstall Apache using Ansible Playbook -- uninstallapache.yaml:
In case you want to uninstall the apache service you can use the below script.
---
-
hosts: all
gather_facts: true
become: true
tasks:
-
name: "1. Uninstall HTTPD/Apache"
tags: uninstall_apache
yum:
name: httpd
autoremove: yes
state: absent
How to run Ansible Playbook
- Open your shell terminal on the Ansible control server and go to /etc/ansible/ path.
- Check your playbook is syntactically correct or not. ansible-playbook apache.yaml -l appserver --check
- Check the list of possible tags in a specific playbook. ansible-playbook apache.yaml --list-tags
- Below is the command to run an ansible playbook on a specific host or a group of hosts. In this case, I am running it on appserver which is my group at the moment and contains two servers.
You are now able to create a simple playbook that will automate apache installation. Please check this post in case you want to manage users on a remote machine using ansible. Write in the comment section about your queries and concerns.
Thanks for providing such a great information.
ReplyDeleteDevOps Training
DevOps Online Training
Great Share
ReplyDeleteGreat Share
ReplyDeleteGreat Share
ReplyDeleteThanks for the interesting post, keep continue your good job.
ReplyDeleteDevOps Full Course Tutorial for Free, DevOps Tutorial for Beginners
Kubernetes Full Course Tutorial for Free, Kubernetes Tutorial for Beginners
Ansible Full Course Tutorial for Free, Ansible Tutorial for Beginners
Docker Full Course Tutorial for Free, Docker Tutorial for Beginners
Openstack Full Course Tutorial for Free, Openstack Tutorial for Beginners
Learnitguide.net
Thanks for the interesting post, keep continue your good job.
ReplyDeleteDevOps Full Course Tutorial for Free, DevOps Tutorial for Beginners
Kubernetes Full Course Tutorial for Free, Kubernetes Tutorial for Beginners
Ansible Full Course Tutorial for Free, Ansible Tutorial for Beginners
Docker Full Course Tutorial for Free, Docker Tutorial for Beginners
Openstack Full Course Tutorial for Free, Openstack Tutorial for Beginners
Learnitguide.net