Geavanceerd
Zoeken inLabs
Zo zoek je
  • Zoek een taak of een tool (lvm, selinux, @podman); filter op niveau en op boek of cursus.
  • Met een leeg zoekvak toont een filter alleen al de labs — bijvoorbeeld alle moeilijke labs van één cursus.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Lab 4.1Alta3 Academy, Hoofdstuk 4

Automate a web server with Ansible: inventory, playbook, role, handler, vault and check mode

Niveau
Gemiddeld
60 min
Machines
a control node with ansible-core 2.15+, two Linux VMs (RHEL family or Ubuntu) reachable with SSH keys
Status
Nog niet begonnen

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Doel

Write the Ansible project you would really use to manage web servers: an inventory with groups and variables, a playbook that installs and configures Nginx through a role, a template, a handler that reloads only on change, a secret kept with Ansible Vault, and a safe workflow with --check --diff before every change. Run it twice to prove it is idempotent.

Voordat je begint

Taken

  1. Write ansible.cfg (inventory file, remote user, become, no host key prompts for the lab) and inventory.ini with the group web (both VMs) and a group variable http_port: 8080.
  2. Create a role webserver with ansible-galaxy role init roles/webserver.
  3. In the role: install nginx, deploy templates/site.conf.j2 (listen on http_port, a server name from a host variable), make sure the service is started and enabled, and open the port in the firewall.
  4. Use a handler reload nginx that runs only when the template changed.
  5. Keep a secret API token in group_vars/web/vault.yml encrypted with ansible-vault, and write it into /etc/nginx/api_token with mode 0600.
  6. Write site.yml that applies the role to web. Run it with --check --diff first, then for real.
  7. Run it a second time: the result must be changed=0 (idempotent). Change the port to 8081 and see that only the template task and the handler run.
  8. Use an ad-hoc command to check the page on both servers, and --limit web1 to change only one server.

Controleer je werk

ansible-inventory --graph
ansible-playbook site.yml --ask-vault-pass
ansible-playbook site.yml --ask-vault-pass | tail -4
ansible web -m uri -a "url=http://localhost:{{ http_port }} return_content=false"
ansible-vault view group_vars/web/vault.yml --ask-vault-pass
  • The graph shows @web with web1 and web2.
  • The second run shows changed=0 for both hosts.
  • The uri check gives "status": 200 on both servers.
  • vault.yml is unreadable in Git ($ANSIBLE_VAULT;1.1;AES256 header) and readable only with the password.

Hints

Toon de hints
  • Modules: ansible.builtin.package, ansible.builtin.template, ansible.builtin.service, ansible.posix.firewalld (RHEL) or community.general.ufw (Ubuntu), ansible.builtin.copy.
  • notify: reload nginx on the template task; the handler with the same name lives in roles/webserver/handlers/main.yml.
  • no_log: true on the task that writes the secret, so it never appears in the output.
  • A broken template would make the reload fail: test the full config in the handler chain (nginx -t) or with the template option validate: (it needs a %s placeholder and a file that is valid on its own).
  • On RHEL with SELinux, a port other than 80/443/8080 also needs community.general.seport (8080 is already allowed as http_port_t).

Oplossing

Toon de oplossing
  1. Project files.
    ansible.cfg
    [defaults]
    inventory = inventory.ini
    remote_user = youruser
    host_key_checking = False
    roles_path = roles
     
    [privilege_escalation]
    become = True
    inventory.ini
    [web]
    web1 ansible_host=192.168.56.11 site_name=web1.lab.internal
    web2 ansible_host=192.168.56.12 site_name=web2.lab.internal
     
    [web:vars]
    http_port=8080
  2. The role (only the files you change).
    roles/webserver/tasks/main.yml
    - name: Install nginx
      ansible.builtin.package:
        name: nginx
        state: present
     
    - name: Deploy the site config
      ansible.builtin.template:
        src: site.conf.j2
        dest: /etc/nginx/conf.d/site.conf
        mode: "0644"
      notify: reload nginx
     
    - name: Write the API token
      ansible.builtin.copy:
        content: "{{ vault_api_token }}\n"
        dest: /etc/nginx/api_token
        mode: "0600"
      no_log: true
     
    - name: Open the port (RHEL family)
      ansible.posix.firewalld:
        port: "{{ http_port }}/tcp"
        permanent: true
        immediate: true
        state: enabled
      when: ansible_os_family == "RedHat"
     
    - name: Start and enable nginx
      ansible.builtin.service:
        name: nginx
        state: started
        enabled: true
    roles/webserver/handlers/main.yml
    - name: reload nginx
      ansible.builtin.service:
        name: nginx
        state: reloaded
    roles/webserver/templates/site.conf.j2
    # managed by Ansible - do not edit by hand
    server {
        listen {{ http_port }};
        server_name {{ site_name }};
        location / {
            return 200 "hello from {{ inventory_hostname }}\n";
        }
    }
    site.yml
    - name: Web servers
      hosts: web
      roles:
        - webserver
  3. The secret and the runs.
    mkdir -p group_vars/web && ansible-vault create group_vars/web/vault.yml
    ansible-playbook site.yml --ask-vault-pass --check --diff
    ansible-playbook site.yml --ask-vault-pass
    ansible-playbook site.yml --ask-vault-pass
    web1 : ok=6    changed=0    unreachable=0    failed=0    skipped=0
    web2 : ok=6    changed=0    unreachable=0    failed=0    skipped=0

    In the vault file write one line: vault_api_token: "abc123-lab".

  4. A small change, one server.
    sed -i 's/http_port=8080/http_port=8081/' inventory.ini
    ansible-playbook site.yml --ask-vault-pass --limit web1 --diff
    ansible web -m uri -a "url=http://localhost:{{ http_port }}"

    On RHEL, port 8081 also needs the SELinux port type (community.general.seport with setype: http_port_t), or change it back to 8080.

Mijn notities

Nog geen notities.

Lessen voor dit lab

Commando's in dit lab