Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Using Ansible Firewall Rules

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 set up and configured for use which will save you time when doing this hands-on lab. *This course is not approved or sponsored by Red Hat.*

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 45m
Published
Clock icon Sep 13, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Sign on to the Ansible Control Node Server as cloud_user and change to the `ansible` user. Test to ensure that Ansible has been set up for you.

    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
      
  2. Challenge

    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
    
  3. Challenge

    Using an Ansible playbook, install the Apache Web Server, `httpd` and also `elinks` onto the Ansible nodes and start the Apache Web Server.

    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
    
  4. Challenge

    Ensure the webserver 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
    
  5. Challenge

    Using the playbook, install `firewalld` on the Ansible nodes. Ensure the `firewalld` is running. Test the Apache test pages are not available.

    1. Make changes to the playbook for installing firewalld:
    ---
    - 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. Save and exit.
    2. Run the playbook with the changes applied:
    vim setup-server.yml
    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 will be blocking them.

  6. Challenge

    Change the Ansible playbook to add the firewall rule to allow port 80.

    1. Make changes to the playbook for installing firewalld:
    ---
    - 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
    
    1. Save and exit.
    2. Run the playbook with the changes applied:
    vim setup-server.yml
    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 will be blocking them.

  7. Challenge

    Test that you can access the test pages on the web servers on the 2 Ansible nodes.

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

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans