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.*
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- 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.
- 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. Test that Ansible is working via an ad-hoc command. We can use the following:
ansible all -m ping
- Sign in to the server called Ansible Control Node using the cloud_user, and then change to the ansible user via the
- Ensure that `firewalld` is not installed on the Ansible nodes, `node1` and `node2`
Check that
firewalld
is not installed on the nodes:ssh node1 sudo systemctl status firewalld
Use
exit
to move onto the nextnode
check:ssh node2 sudo systemctl status firewalld
- Using an Ansible playbook, install the Apache Web Server, `httpd` and also `elinks` onto the Ansible nodes and start the Apache Web Server.
Create the playbook:
vim setup-server.yml
- 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
- Ensure the webserver is working and test that apache on the nodes can be accessed.
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. Ensure the `firewalld` is running. Test the Apache test pages are not avaiable.
- 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
Save and exit.
Run the playbook with the changes applied:
vim setup-server.yml ansible-playbook setup-server.yml
Verify that we are unable to access the test pages on
node1
andnode2
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.
- Make changes to the playbook for installing
- Change the Ansible playbook to add the firewall rule to allow port 80.
- 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
Save and exit.
Run the playbook with the changes applied:
vim setup-server.yml ansible-playbook setup-server.yml
Verify that we are unable to access the test pages on
node1
andnode2
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.
- Make changes to the playbook for installing
- Test that you can access the test pages on the web servers on the 2 Ansible nodes.
Run the playbook:
vim setup-server.yml ansible-playbook setup-server.yml
Using the
elinks
command again:elinks http://node1 elinks http://node2