{{ main_content }}
# Practise: Ansible Template, configure NGINX After creating a simple website in the first exercise, you can now use the knowledge from the previous blog post and use Ansible templates, to create the content of the web page and the configuration of the web server. ## Step 1: Vorbereiten der Umgebung First create a new directory for the project and navigate to the directory: ```bash mkdir ansible-webserver-template && cd $_ ``` ## Step 2: Inventory-Datei erstellen Create an inventory file `hosts.ini` with the connection data to your Ubuntu server: ```bash [webserver] ubuntu-server ansible_host=IP-Adresse192.168..X.X [webserver:vars] ansible_user = user ansible_ssh_user = user ansible_password = password ansible_ssh_password = password ``` ## Step 3: Webserver-Playbook erstellen Erstelle ein Playbook webserver.yml: ```yaml - name: Set up webserver with templates hosts: webserver become: yes vars: nginx_user: nginx server_listen_port: 80 root: /var/www/html/ index: index.html main_content: This is a simple website powered by Ansible and Nginx. footer_text: Copyright © 2023 - All Rights Reserved tasks: - name: Install Nginx ansible.builtin.apt: name: nginx state: present notify: Start Nginx handlers: - name: Start Nginx ansible.builtin.service: name: nginx state: started ``` ## Step 4: Ansible-Templates erstellen Erstelle ein Verzeichnis ``templates`` und darin zwei Vorlagen: ``index.html.j2`` für den Inhalt der Webseite und ``nginx.conf.j2`` für die Webserver-Konfiguration. ### NGINX ```jinja2 user {{ nginx_user }}; worker_processes auto; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; server { listen {{ server_listen_port }}; server_name {{ inventory_hostname }}; root {{ web_root }}; index {{ web_index }}; location / { try_files $uri $uri/ =404; } } } ``` ### HTML ```jinja2
{{ main_content }}