{"id":6000,"date":"2025-07-18T12:52:29","date_gmt":"2025-07-18T17:52:29","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=6000"},"modified":"2025-07-18T14:25:42","modified_gmt":"2025-07-18T19:25:42","slug":"ansible-playbook-for-setting-ubuntu-ufw-firewall","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/ansible-playbook-for-setting-ubuntu-ufw-firewall\/","title":{"rendered":"Ansible Playbook for Ubuntu UFW Firewall Rules"},"content":{"rendered":"\n<p>Here is a simple Ansible UFW firewall playbook.<\/p>\n\n\n\n<p>You will need to install the Ansible collection <code>community.general<\/code> if it is not already installed.<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\">ansible-galaxy collection install community.general<\/code><\/pre>\n\n\n\n<p>Next create the playbook.  Modify the ports to your liking.  Also remember you can put these variables in the Ansible inventory file so each hosts can have different settings.  <\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\">---\n- hosts: ubuntu\n  become: true\n  vars:\n    ssh_port: 22\n    http_port: 80\n    https_port: 443\n\n  tasks:\n  - name: Ensure UFW is installed\n    apt:\n      name: ufw\n      state: present\n      update_cache: yes\n\n  - name: Restrict SSH access to local IP addresses (RFC 1918)\n    block:\n      - name: Allow SSH from 10.0.0.0\/8\n        community.general.ufw:\n          rule: allow\n          from_ip: 10.0.0.0\/8\n          port: \"{{ ssh_port }}\"\n          proto: tcp\n      - name: Allow SSH from 172.16.0.0\/12\n        community.general.ufw:\n          rule: allow\n          from_ip: 172.16.0.0\/12\n          port: \"{{ ssh_port }}\"\n          proto: tcp\n      - name: Allow SSH from 192.168.0.0\/16\n        community.general.ufw:\n          rule: allow\n          from_ip: 192.168.0.0\/16\n          port: \"{{ ssh_port }}\"\n          proto: tcp\n\n  - name: Allow HTTPS access from the world\n    community.general.ufw:\n      rule: allow\n      port: \"{{ https_port }}\"\n      proto: tcp\n\n  - name: Allow HTTP access from the world\n    community.general.ufw:\n      rule: allow\n      port: \"{{ http_port }}\"\n      proto: tcp\n\n  - name: Enable UFW and set default policy to deny incoming\n    community.general.ufw:\n      state: enabled\n      policy: deny\n<\/code><\/pre>\n\n\n\n<p>Save the playbook as <code>ubuntu_firewall.yaml<\/code><\/p>\n\n\n\n<p>Run the playbook with:<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\">ansible-playbook ubuntu_firewall.yaml -i inventory\/hosts<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Here is a more advanced playbook that will loop through multiple IP addresses and ports.<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\">---\n- hosts: ubuntu\n  become: true\n  vars:\n    local_ports:\n      - { port: '22', proto: 'tcp' }  # SSH\n      - { port: '161', proto: 'udp' }  # SNMP\n    public_ports:\n      - { port: '80', proto: 'tcp' }  # HTTP\n      - { port: '443', proto: 'tcp' } # HTTPS\n    local_ips:\n      - 10.0.0.0\/8\n      - 172.16.0.0\/12\n      - 192.168.0.0\/16\n\n  tasks:\n    - name: Ensure UFW is installed\n      apt:\n        name: ufw\n        state: present\n        update_cache: yes\n\n    - name: Allow public_ports\n      community.general.ufw:\n        rule: allow\n        port: \"{{ item.port }}\"\n        proto: \"{{ item.proto }}\"\n      loop: \"{{ public_ports }}\"\n\n\n    - name: Allow access to local_ports from RFC 1918 local addresses\n      block:\n        - name: Allow local_ports from RFC 1918 local IPs\n          community.general.ufw:\n            rule: allow\n            from_ip: \"{{ item.0 }} \"\n            port: \"{{ item.1.port }}\"\n            proto: \"{{ item.1.proto }}\"\n          loop: \"{{ local_ips | product(local_ports) | list }}\"\n\n    - name: Enable UFW and set default policy to deny incoming\n      community.general.ufw:\n        state: enabled\n        policy: deny<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is a simple Ansible UFW firewall playbook. You will need to install the Ansible collection community.general if it is not already installed. Next create the playbook. Modify the ports to your liking. Also remember you can put these variables &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/ansible-playbook-for-setting-ubuntu-ufw-firewall\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1646,1647,3,1661,28],"tags":[1193,51,160,1200,49,416],"class_list":["post-6000","post","type-post","status-publish","format-standard","hentry","category-ansible","category-automation","category-linux","category-playbooks","category-ubuntu","tag-ansible","tag-debian","tag-firewall","tag-playbook","tag-ubuntu-2","tag-ufw"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/6000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=6000"}],"version-history":[{"count":11,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/6000\/revisions"}],"predecessor-version":[{"id":6011,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/6000\/revisions\/6011"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=6000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=6000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=6000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}