ansibleaptufw

How to install UFW via ansible (.yml)


I found an Ansible playbook to install a LAMP server. Thing is Debian 11 does not include UFW in the default installation and the playbook tries to configure it and fails. I can remove these lines from the PB, but it would be great to be able to install UFW. Not sure how?

Here's the yml file:

---
- name: Install prerequisites
  apt: name={{ item }} update_cache=yes state=latest force_apt_get=yes
  loop: [ 'aptitude' ]

  #Apache Configuration
- name: Install Apache and PHP Packages
  apt: name={{ item }} update_cache=yes state=latest
  loop: [ 'apache2', 'php', 'php-mysql', 'libapache2-mod-php' ]

- name: Create document root
  file:
    path: "/var/www/{{ http_host }}"
    state: directory
    owner: "{{ app_user }}"
    mode: '0755'

- name: Set up Apache virtualhost
  template:
    src: "files/apache.conf.j2"
    dest: "/etc/apache2/sites-available/{{ http_conf }}"
    
- name: Enable new site
  shell: /usr/sbin/a2ensite {{ http_conf }}
  
- name: Disable default Apache site
  shell: /usr/sbin/a2dissite 000-default.conf
  when: disable_default
  notify: Reload Apache

# UFW Configuration
- name: "UFW - Allow HTTP on port {{ http_port }}"
  ufw:
    rule: allow
    port: "{{ http_port }}"
    proto: tcp

  # PHP Info Page
- name: Sets Up PHP Info Page
  template:
    src: "files/info.php.j2"
    dest: "/var/www/{{ http_host }}/info.php"

- name: Reload Apache
  service:
    name: apache2
    state: reloaded

- name: Restart Apache
  service:
    name: apache2
    state: restarted

Solution

  • Add additional tasks to install and enable UFW as given below.

      #UFW Configuration
    - name: Install UFW firewall
      apt: name=ufw update_cache=yes state=latest  
      
    - name: Enable UFW
      community.general.ufw:
        state: enabled
    

    Refer ufw_module documentation for more configuration params.