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:
1
|
mkdir ansible-webserver-template && cd $_
|
Step 2: Inventory-Datei erstellen
Create an inventory file hosts.ini
with the connection data to your Ubuntu server:
1
2
3
4
5
6
7
8
|
[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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
- 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ ansible_hostname }} Server</title>
</head>
<body>
<header>
<h1>{{ inventory_hostname }}</h1>
</header>
<main>
<p>{{ main_content }}</p>
</main>
<footer>
<p>{{ footer_text }}</p>
</footer>
</body>
</html>
|
Step 5: Task zum Kopieren der Vorlagen hinzufügen
Füge folgende Tasks zum webserver.yml Playbook hinzu:
1
2
3
4
5
6
7
8
9
10
11
|
- 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
hinzu, um Nginx neu zu starten:
1
2
3
4
|
- 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?
PS:
Visit my GitHub