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.
* 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: truesends the credentials on the first request, instead of waiting for the server to answer 401 first.status_code: 200turns the request into a test: any other status fails the task.return_content: truesaves the response body under the keycontentof the registered variable.- The password comes from an encrypted
vars_filesentry, so it never appears in plain text in the playbook. become: false: an HTTP request needs no root rights.
Go deeper:
ansible.builtin.uri module (official docs) โ authentication,
status_codeandreturn_content.