# 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 {{ ansible_hostname }} Server

{{ inventory_hostname }}

{{ main_content }}

``` ## Step 5: Task zum Kopieren der Vorlagen hinzufügen Füge folgende Tasks zum webserver.yml Playbook hinzu: ```yaml - name: Copy index.html template template: src: templates/index.html.j2 dest: /var/www/html/index.html - name: Copy nginx configuration template template: src: templates/nginx.conf.j2 dest: /etc/nginx/nginx.conf notify: Restart Nginx ``` Füge einen weiteren [Handler](https://docs.ansible.com/ansible/latest/user_guide/playbooks_handlers.html) hinzu, um Nginx neu zu starten: ```yaml - name: Restart Nginx ansible.builtin.service: name: nginx state: restarted ``` ## Step 6: Run the Playbook Run the playbook with the command `ansible-playbook -i hosts.ini webserver.yml` to set up the web server and transfer the templates. Then check the web page and configuration on the Ubuntu server. Of course you have to adapt the templates to your environment. If you find any errors in my configuration please do not hesitate to write me: info@itzlu4u.de ## Conclusion You've learned how to create Ansible templates and use them in your playbook. Now you're ready to explore more advanced features of Ansible! You may have noticed that we didn't have to define all the variables used in the templates in the playbook. If you want to learn why that is, feel free to read up on Ansible facts or wait for one of my upcoming posts on this topic. Good luck on your automation journey! ---- [Like this Post?](https://www.buymeacoffee.com/itzlu4u) PS: Visit my [GitHub](https://github.com/itzlu4u/)