Skip to main content

Using Ansible Firewall Rules

Hands-On Lab

 

Length

00:30:00

Difficulty

Intermediate

Being able to use Ansible to create and change firewall rules is a valuable skill. On top of that, it is now an objective of the Red Hat Certified Ansible Specialist exam. In this lab we will create playbooks using the firewalld module, and use them to perform firewall related tasks. Note: Ansible has been setup and configured for use which will save you time when doing this Hands-On Lab.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Using Ansible Firewall Rules

Introduction

In this hands-on lab, we will use an Ansible playbook to install the Apache Web Server, and then install a firewall. Afterwards, we will add a rule to the firewall, that will allow Apache to be accessed externally.

Note: Ansible has been set up and configured for use.

Solution

To access the lab environment, log in to the control node server as cloud_user, using the IP address and login information provided in the Credentials section of the hands-on lab page.

Sign onto the Ansible Control Node Server as cloud_user and Change to the ansible User

  1. Sign in to the server called Ansible Control Node using the cloud_user, and then change to the ansible user via the su - ansible command.
  2. Test that Ansible is working via an ad hoc command. We can use the following:
ansible all -m ping

Ensure That firewalld Is Not Installed on the Ansible nodes, node1 and node2

  1. Check that firewalld is not installed on the nodes:
ssh node1
sudo systemctl status firewalld
  1. Use exit to move onto the next node check:
ssh node2
sudo systemctl status firewalld

Note: For both nodes we should encounter a "service not be found message."

  1. Use exit to close node2, and then clear the screen.

Using an Ansible Playbook, Install the Apache Web Server, httpd and elinks onto the Ansible Nodes

Using a playbook, we should install httpd and elinks onto the Ansible nodes.

  1. Create the playbook:
vim setup-server.yml
  1. The playbook file could appear like so for the installations, plus enabling and starting the Apache Web Server:
---
- hosts: all
  user: ansible
  become: yes
  gather_facts: no
  tasks:
    - name: install elinks
      action: yum name=elinks state=installed
    - name: install httpd
      action: yum name=httpd state=installed
    - name: Enable Apache on system reboot
      service: name=httpd enabled=yes
    - name: Start service httpd, if not started
      service:
        name: httpd
        state: started
  1. Run the playbook:
ansible-playbook setup-server.yml
  1. Verify that the Apache HTTP Server on Red Had Enterprise Test Linux page can be seen:
elinks https://localhost

Ensure the Web Server is Working and Test That Apache on the Nodes Can Be Accessed

  1. Using the elinks command. We will verify that we can see the Apache HTTP Server test page on the 2 Ansible nodes with the following commands:
elinks http://node1
elinks http://node2

Using the Playbook, Install firewalld on the ansible Nodes

We will ensure that firewalld is running and verify that the Apache test pages are not available.

  1. Make changes to the playbook for installing firewalld:
vim setup-server.yml

We need to add the firewall section at the bottom. It should look like this when we're done:

---
- hosts: all
  user: ansible
  become: yes
  gather_facts: no
  tasks:
    - name: install elinks
      action: yum name=elinks state=installed
    - name: install httpd
      action: yum name=httpd state=installed
    - name: Enable Apache on system reboot
      service: name=httpd enabled=yes
    - name: Start service httpd, if not started
      service:
        name: httpd
        state: started
    - name: install firewalld
      action: yum name=firewalld state=installed
    - name: Enable firewalld on system reboot
      service: name=firewalld enabled=yes
    - name: Start service firewalld, if not started
      service:
        name: firewalld
        state: started
  1. Run the playbook again to apply the changes:
ansible-playbook setup-server.yml
  1. Verify that we are unable to access the test pages on node1 and node2 with the following commands:
elinks http://node1
elinks http://node2

Note: We will be unable to access the test pages because the firewall blocks them.

Change the Ansible Playbook to Add the Firewall Rule to Allow Port 80

  1. Make changes to allow port 80 in the playbook and restart the firewalld service. It should look like this when we're done:
---
- hosts: all
  user: ansible
  become: yes
  gather_facts: no
  tasks:
    - name: install elinks
      action: yum name=elinks state=installed
    - name: install httpd
      action: yum name=httpd state=installed
    - name: Enable Apache on system reboot
      service: name=httpd enabled=yes
    - name: Start service httpd, if not started
      service:
        name: httpd
        state: started
    - name: install firewalld
      action: yum name=firewalld state=installed
    - name: Enable firewalld on system reboot
      service: name=firewalld enabled=yes
    - firewalld:
        service: http
          permanent: yes
          state: enabled
      - name: Restart service firewalld
        service:
        name: firewalld
        state: restarted

Test That You Can access the Test Pages on the Web Servers on the 2 Ansible Nodes.

  1. Run the playbook:
ansible-playbook setup-server.yml
  1. Using the elinks command again:
 elinks http://node1
 elinks http://node2

This time we should be able to access the test web pages on the two Ansible nodes.

Conclusion

Congratulations — you've completed this hands-on lab.