End-of-Day 2 Exercise · Ansible Vault
Ansible VAULT and GALAXY
Assignment
- Deploy a secure website.
- Automatically deploy an nginx web server that is accessible over the network and protected by username/password authentication.
- Requirements:
- installation using a Galaxy collection
- password stored in Ansible Vault
- no sensitive values stored in plain text in files
The password will be: superpassword
Example requirements.yml
collections:
- name: community.general
- name: ansible.posix
Minimal playbook
- name: Secure website
hosts: web
become: true
vars:
site_user: student
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
- name: Install Python dependency for htpasswd
ansible.builtin.apt:
name: python3-passlib
state: present
update_cache: true
- name: Install apache2-utils (for htpasswd)
ansible.builtin.apt:
name: apache2-utils
state: present
- name: Create HTTP user
community.general.htpasswd:
path: /etc/nginx/.htpasswd
name: "{{ site_user }}"
password: "{{ vault_web_password }}"
- name: Web page
ansible.builtin.copy:
dest: /var/www/html/index.html
content: "<h1>Ansible server OK</h1>"
- name: Deploy nginx configuration
ansible.builtin.template:
src: site.conf.j2
dest: /etc/nginx/sites-available/default
notify: restart nginx
- name: Start nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
handlers:
- name: restart nginx
ansible.builtin.service:
name: nginx
state: restarted
templates/site.conf.j2
server {
listen 80;
server_name _;
location / {
root /var/www/html;
index index.html;
auth_basic "Restricted area";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
Commands to run in order
# Install the collection
ansible-galaxy collection install -r requirements.yml
ansible-vault create group_vars/web/vault.yml
containing: vault_web_password: superpassword
ansible-playbook -i inventory.ini site.yml --ask-vault-pass