Apache installed with Ansible - quick notes

Install Apache 2 web server automatically. Serves a web site on the front page of http://localhost. Pages can be edited as a normal user.

Simplified Ansible role. An example of package-file-server pattern. Just notes, no tutorial.

Using these short notes require a working Ansible configuration - Hello Ansible.

Package-file-service pattern

Daemons, like Apache2 web server, are somewhat similar. They are configured with package-file-service pattern. Install the daemon, change configuration by editing files, start it.

  • package - sudo apt-get install apache2
  • files - sudoedit /etc/apache2/...
  • service - sudo systemctl restart apache2

Changes to configuration files only take effect after you kick the daemon. Thus, file in tasks/main.yml notify the handler "restart apache2".

tree of roles/apache2/

roles/apache2/  # All files related to "apache2" role
├── files/      # Master copies of files
│   └── example.com.conf
├── handlers/   # restarting services
│   └── main.yml
└── tasks/      # most ansible code
    └── main.yml

tasks/main.yml

- apt:
    name: apache2
    state: present

- copy:
    dest: "/etc/apache2/sites-available/example.com.conf"
    src: "example.com.conf"
    owner: "root"
    group: "root"
    mode: "0644"
  notify: restart apache2

- file:
    src: /etc/apache2/sites-available/example.com.conf
    dest: /etc/apache2/sites-enabled/example.com.conf
    owner: root
    group: root
    state: link
  notify: restart apache2

handlers/main.yml

- name: restart apache2
  systemd:
    name: apache2
    state: restarted

files/example.com.conf

# Managed file, changes will be overwritten

<VirtualHost *:80>
        ServerName example.com
        DocumentRoot /home/tero/publicsite/

        <Directory /home/tero/publicsite/>
                require all granted
        </Directory>
</VirtualHost>

Adminstrivia

Apache httpd logo is a trademark of The Apache Software Foundation.