Webserver

We are now going to install, configure and enable a web server on our target systems. Take a look at the webserver.yml playbook. It’s first installing the packages, then using systemctl to enable the webserver and making sure it’s started.

Execute the playbook

ansible-playbook webserver.yml

After the playbook completes successfully, run the following commands to ensure the package is installed and the service is started and enabled:

ansible all -m command -a "rpm -q httpd"
ansible all -m command -a "systemctl is-active httpd"
ansible all -m command -a "systemctl is-enabled httpd"

Edit the playbook and add another task to create a custom web page.

    - name: Create index page
      ansible.builtin.copy:
        dest: /var/www/html/index.html
        content: |
          <html>
          <body>
          <h1>Hello from Ansible!</h1>
          <p>Hostname: {{ ansible_hostname }}</p>
          </body>
          </html>

This task uses the copy module to add file to the system. You could copy a file local to the ansible server to the target system, but in this case, we are providing the contents inline.

Run the playbook again. Notice that the first two tasks are green (unchanged), but the new task is yellow (change made). This demonstrates idempotency. Run the curl command against the servers to see the custom web page.

curl http://${CODER_USER}-vm1.sandbox.fdlabs.dev
curl http://${CODER_USER}-vm2.sandbox.fdlabs.dev