LOGBOOK

HELP

Quiz Entry - updated: 2026.11.05

How can a play check that a web server's Basic Authentication works, and show the page it returned?

Use ansible.builtin.uri to request the page with force_basic_auth: true, the user and password from variables, return_content: true and status_code: 200; register the result and print auth_test['content'] with debug. The task fails if the server answers with anything other than 200.

Two-play playbook: play 1 installs and configures Apache with variables and facts, play 2 loads a vaulted password and tests the site with uri, register and debug

* The test play reads its password from a vault-encrypted vars file. *

- name: Test web server with basic auth
  hosts: debian
  become: false
  vars:
    web_user: guest
  vars_files:
    - vars/secret.yml          # vault-encrypted, defines web_pass
  tasks:
    - name: Connect to web server with basic auth
      ansible.builtin.uri:
        url: http://localhost
        validate_certs: false
        force_basic_auth: true
        user: "{{ web_user }}"
        password: "{{ web_pass }}"
        return_content: true
        status_code: 200
      register: auth_test

    - name: Display auth_test content
      ansible.builtin.debug:
        var: auth_test['content']

What each piece does:

  • force_basic_auth: true sends the credentials on the first request, instead of waiting for the server to answer 401 first.
  • status_code: 200 turns the request into a test: any other status fails the task.
  • return_content: true saves the response body under the key content of the registered variable.
  • The password comes from an encrypted vars_files entry, so it never appears in plain text in the playbook.
  • become: false: an HTTP request needs no root rights.

Go deeper:

From Quiz: ITIA / Ansible Variables, Secrets and Facts | Updated: Nov 05, 2026