This commit is contained in:
Hubert Cornet 2024-05-06 15:20:57 +02:00
parent 6a2fbcee07
commit 1ef3dd3c83
21 changed files with 487 additions and 0 deletions

View File

@ -0,0 +1,8 @@
---
docker:
hosts:
docker01:
ansible_host: 192.168.200.222
ansible_user: 'ubuntu'
ansible_become: true
ansible_become_method: sudo

View File

@ -0,0 +1,7 @@
---
- name: Install Docker on Ubuntu
hosts: all
become: true
roles:
- docker_install
- portainer_deploy

View File

@ -0,0 +1,5 @@
---
- name: Restart Docker
ansible.builtin.systemd:
name: docker
state: restarted

View File

@ -0,0 +1,41 @@
---
- name: Ensure apt is using HTTPS
ansible.builtin.apt:
name: "{{ item }}"
state: present
loop:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
- name: Add Docker GPG key
ansible.builtin.apt_key:
url: "https://download.docker.com/linux/ubuntu/gpg"
state: present
- name: Add Docker repository
ansible.builtin.apt_repository:
repo: "{{ docker_apt_repository }}"
state: present
- name: Install Docker CE
ansible.builtin.apt:
name: docker-ce
state: present
update_cache: true
- name: Configure Docker daemon options
ansible.builtin.template:
src: "templates/docker_daemon.json.j2"
dest: "/etc/docker/daemon.json"
owner: 'root'
group: 'root'
mode: '0755' # Optional file permissions
notify: Restart Docker
- name: Ensure Docker service is enabled and running
ansible.builtin.systemd:
name: docker
enabled: true
state: started

View File

@ -0,0 +1,3 @@
{
"storage-driver": "{{ docker_daemon_options['storage-driver'] }}"
}

View File

@ -0,0 +1,5 @@
---
docker_apt_release_channel: "stable"
docker_apt_repository: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
docker_daemon_options:
storage-driver: "overlay2"

View File

@ -0,0 +1,6 @@
---
- name: Start Portainer
community.docker.docker_compose:
project_src: /home/ubuntu/docker-compose/portainer
state: present
restarted: true

View File

@ -0,0 +1,34 @@
---
- name: Ensure docker-compose is installed
ansible.builtin.package:
name: docker-compose
state: present
- name: Ensure Docker service is running
ansible.builtin.service:
name: docker
state: started
enabled: true
- name: Setup Portainer directory
ansible.builtin.file:
path: /home/ubuntu/docker-compose/portainer
state: directory
mode: '0755' # Optional file permissions
owner: ubuntu # Optional ownership
group: ubuntu # Optional group ownership
- name: Deploy Portainer using Docker Compose
ansible.builtin.template:
src: "templates/docker_compose.yaml.j2"
dest: "/home/ubuntu/docker-compose/portainer/docker-compose.yaml"
mode: '0755' # Optional file permissions
owner: ubuntu # Optional ownership
group: ubuntu # Optional group ownership
notify:
- Start Portainer
- name: Run Portainer docker-compose up
community.docker.docker_compose:
project_src: /home/ubuntu/docker-compose/portainer
state: present

View File

@ -0,0 +1,13 @@
version: '3.3'
services:
portainer:
image: portainer/portainer-ce:{{ portainer_version }}
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- portainer_data:/data
ports:
- "9000:9000"
restart: always
volumes:
portainer_data:

View File

@ -0,0 +1,2 @@
---
portainer_version: "latest"

View File

@ -0,0 +1,52 @@
---
- name: Deploy Docker Container with Docker Compose
hosts: all
become: true
tasks:
- name: Ensure Docker is installed
ansible.builtin.package:
name: docker
state: present
- name: Ensure Docker service is running
ansible.builtin.service:
name: docker
state: started
enabled: true
- name: Create a directory for Docker Compose files
ansible.builtin.file:
path: /home/ubuntu/ansible-docker/docker-compose
state: directory
mode: '0755' # Optional file permissions
owner: ubuntu # Optional ownership
group: ubuntu # Optional group ownership
- name: Create a directory for Nginx website files
ansible.builtin.file:
path: /home/ubuntu/docker/nginx/web
state: directory
mode: '0755' # Optional file permissions
owner: ubuntu # Optional ownership
group: ubuntu # Optional group ownership
- name: Copy docker-compose to remote host
ansible.builtin.copy:
src: /home/ubuntu/nginx/docker-compose.yaml
dest: /home/ubuntu/ansible-docker/docker-compose/docker-compose.yaml
mode: '0755' # Optional file permissions
owner: ubuntu # Optional ownership
group: ubuntu # Optional group ownership
- name: Copy Nginx website folder to remote host # copies a folder - note no file extension
ansible.builtin.copy:
src: /home/ubuntu/nginx/website
dest: /home/ubuntu/docker/nginx/web
mode: '0755' # Optional file permissions
owner: ubuntu # Optional ownership
group: ubuntu # Optional group ownership
- name: Start Docker Compose
community.docker.docker_compose:
project_src: /home/ubuntu/ansible-docker/docker-compose
state: present

View File

@ -0,0 +1,24 @@
---
- name: Undo Docker Compose Deployment
hosts: all
become: true
tasks:
- name: Stop Docker Container
community.docker.docker_compose:
project_src: /home/ubuntu/ansible-docker/docker-compose
state: absent
- name: Remove Docker Compose file
ansible.builtin.file:
path: /home/ubuntu/ansible-docker/docker-compose/docker-compose.yml
state: absent
- name: Remove Docker Compose directory
ansible.builtin.file:
path: /home/ubuntu/ansible-docker
state: absent
- name: Remove Website directory
ansible.builtin.file:
path: /home/ubuntu/docker/nginx/web
state: absent

View File

@ -0,0 +1,8 @@
---
docker:
hosts:
docker01:
ansible_host: 192.168.200.50
ansible_user: 'ubuntu'
ansible_become: true
ansible_become_method: sudo

View File

@ -0,0 +1,31 @@
version: "3.9"
services:
web:
image: nginx
container_name: jimsgarage
volumes:
- /home/ubuntu/docker/nginx/templates:/etc/nginx/templates
- /home/ubuntu/docker/nginx/web/website:/usr/share/nginx/html
environment:
- NGINX_HOST=nginx.jimsgarage.co.uk
- NGINX_PORT=80
labels:
- "traefik.enable=true"
- "traefik.http.routers.nginx.entrypoints=http"
- "traefik.http.routers.nginx.rule=Host(`nginx.jimsgarage.co.uk`)"
- "traefik.http.middlewares.nginx-https-redirect.redirectscheme.scheme=https"
- "traefik.http.routers.nginx.middlewares=nginx-https-redirect"
- "traefik.http.routers.nginx-secure.entrypoints=https"
- "traefik.http.routers.nginx-secure.rule=Host(`nginx.jimsgarage.co.uk`)"
- "traefik.http.routers.nginx-secure.tls=true"
- "traefik.http.routers.nginx-secure.service=nginx"
- "traefik.http.services.nginx.loadbalancer.server.port=80"
- "traefik.docker.network=proxy"
networks:
proxy:
security_opt:
- no-new-privileges:true
networks:
proxy:
external: true

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

View File

@ -0,0 +1,108 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Jim's Garage Ansible Demo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<style>
.hero {
background: url(Jims-Garage-1.png) no-repeat center center;
background-size: cover;
height: 400px;
display: flex;
align-items: center;
justify-content: center;
color: white;
text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.7);
}
.features {
margin-top: 50px;
text-align: center;
}
.feature {
padding: 20px;
transition: transform 0.3s ease;
}
.feature:hover {
transform: scale(1.05);
}
.footer {
background-color: #333;
color: white;
text-align: center;
padding: 20px;
position: fixed;
width: 100%;
bottom: 0;
}
</style>
</head>
<body>
<!-- Navigation Bar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">My Webpage</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#features">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#contact">Contact</a>
</li>
</ul>
</div>
</nav>
<!-- Hero Section -->
<div class="hero" id="home">
<h1>Welcome to Jim's Garage Ansible Demo</h1>
</div>
<!-- Features Section -->
<div class="container features" id="features">
<h2>Our Features</h2>
<div class="row">
<div class="col-md-4">
<div class="feature">
<i class="fas fa-cogs fa-3x"></i>
<h4>Feature 1</h4>
<p>Dynamic and interactive elements.</p>
</div>
</div>
<div class="col-md-4">
<div class="feature">
<i class="fas fa-bolt fa-3x"></i>
<h4>Feature 2</h4>
<p>Responsive design and transitions.</p>
</div>
</div>
<div class="col-md-4">
<div class="feature">
<i class="fas fa-heart fa-3x"></i>
<h4>Feature 3</h4>
<p>Engaging user experiences.</p>
</div>
</div>
</div>
</div>
<!-- Footer Section -->
<div class="footer">
<p>© 2024 My Webpage. All rights reserved.</p>
</div>
</body>
</html>

View File

@ -0,0 +1,57 @@
---
- name: Update Windows, Arch Linux, and Ubuntu
hosts: all
tasks:
- name: Gather facts
ansible.builtin.setup:
- name: Update Windows
when: ansible_facts['os_family'] == 'Windows'
ansible.windows.win_updates:
category_names:
- SecurityUpdates
- UpdateRollups
- CriticalUpdates
state: installed
register: win_update_result
- name: Check if Windows requires a reboot
when: win_update_result.changed and win_update_result.reboot_required | default(false)
ansible.windows.win_reboot:
reboot_timeout: 600
register: win_reboot_result
- name: Update Arch Linux
when: ansible_facts['os_family'] == 'Arch'
community.general.pacman:
update_cache: true
upgrade: true
register: arch_update_result
- name: Check if Arch Linux requires a reboot
when: ansible_facts['os_family'] == 'Arch' and arch_update_result.changed
ansible.builtin.stat:
path: /run/reboot-required
register: arch_reboot_required
- name: Reboot Arch Linux if required
when: arch_reboot_required.stat.exists | default(false)
ansible.builtin.reboot:
reboot_timeout: 600
- name: Update Ubuntu
when: ansible_facts['os_family'] == 'Debian'
ansible.builtin.apt:
upgrade: dist
update_cache: true
- name: Check if a reboot is required on Ubuntu
when: ansible_facts['os_family'] == 'Debian'
ansible.builtin.stat:
path: /var/run/reboot-required
register: ubuntu_reboot_required
- name: Reboot Ubuntu if required
when: ubuntu_reboot_required.stat.exists | default(false)
ansible.builtin.reboot:
reboot_timeout: 600

View File

@ -0,0 +1,14 @@
arch:
hosts:
arch01:
ansible_host: 192.168.200.214
ansible_user: 'root'
ansible_python_interpreter: /usr/bin/python3
docker:
hosts:
docker01:
ansible_host: 192.168.200.50
ansible_user: 'ubuntu'
ansible_become: true
ansible_become_method: sudo

View File

@ -0,0 +1,67 @@
---
- name: Deploy Docker Container with Docker Compose
hosts: all
become: true
tasks:
- name: Include variables file
ansible.builtin.include_vars: myvars.yaml
- name: Ensure Docker is installed
ansible.builtin.package:
name: docker
state: present
- name: Ensure Docker service is running
ansible.builtin.service:
name: docker
state: started
enabled: true
- name: Create a directory for Docker Compose files
ansible.builtin.file:
path: /home/ubuntu/ansible-docker/docker-compose
state: directory
mode: '0755' # Optional file permissions
owner: ubuntu # Optional ownership
group: ubuntu # Optional group ownership
- name: Create a directory for Nginx website files
ansible.builtin.file:
path: /home/ubuntu/docker/nginx/web
state: directory
mode: '0755' # Optional file permissions
owner: ubuntu # Optional ownership
group: ubuntu # Optional group ownership
- name: Copy docker-compose to remote host
ansible.builtin.copy:
src: /home/ubuntu/nginx/docker-compose.yaml
dest: /home/ubuntu/ansible-docker/docker-compose/docker-compose.yaml
mode: '0755' # Optional file permissions
owner: ubuntu # Optional ownership
group: ubuntu # Optional group ownership
- name: Copy Nginx website folder to remote host # copies a folder - note no file extension
ansible.builtin.copy:
src: /home/ubuntu/nginx/website
dest: /home/ubuntu/docker/nginx/web
mode: '0755' # Optional file permissions
owner: ubuntu # Optional ownership
group: ubuntu # Optional group ownership
- name: Replace old name with new name (requires Ansible >= 2.4)
ansible.builtin.replace:
path: /home/ubuntu/docker/nginx/web/website/index.html
regexp: "Jim's Garage"
replace: "{{ website_name }}"
- name: Access and print secret
ansible.builtin.replace:
path: /home/ubuntu/docker/nginx/web/website/index.html
regexp: "Our Features"
replace: "{{ api_key }}"
- name: Start Docker Compose
community.docker.docker_compose:
project_src: /home/ubuntu/ansible-docker/docker-compose
state: present

View File

@ -0,0 +1 @@
password

View File

@ -0,0 +1 @@
api_key: SuperSecretPassword