Actualiser create-test-stack.yml

This commit is contained in:
l.covela 2026-01-19 15:59:30 +01:00
parent 265299166c
commit 95f0453608
1 changed files with 34 additions and 55 deletions

View File

@ -10,114 +10,93 @@
bastion_port: 17100 bastion_port: 17100
bastion_user: ansible bastion_user: ansible
# Portainer via tunnel (local fixe) # Portainer (via tunnel local)
local_portainer_url: "https://127.0.0.1:9443" portainer_url: https://127.0.0.1:9443
# Stack test # Stack test
stack_name: "test-stack-semaphore" stack_name: test-stack-semaphore
# type=2 => Docker standalone (Compose) ; type=1 => Swarm stack_type: 2 # 2 = Docker standalone, 1 = Swarm
stack_type: 2
# Endpoint: si tu connais l'ID, mets-le ici (sinon on prend le 1er endpoint) # Tunnel SSH
endpoint_id: "" ssh_control_socket: /tmp/ssh-tunnel-portainer-9443.sock
# Tunnel # Compose minimal de test
ssh_control_socket: "/tmp/ssh-tunnel-portainer-9443.sock"
ssh_log: "/tmp/ssh-tunnel-portainer-9443.log"
# Compose minimal pour test
stack_compose: | stack_compose: |
services: services:
web: web:
image: nginx:alpine image: nginx:alpine
restart: unless-stopped restart: unless-stopped
# Pas d'exposition de port : on teste juste que la stack se crée et démarre
# Si tu veux la voir, on pourra ajouter Traefik plus tard.
tasks: tasks:
- block: - block:
- name: Check local port 9443 is not already in use
shell: "ss -lnt | grep -q ':9443 '" - name: Check local port 9443 is free
shell: ss -lnt | grep -q ':9443 '
register: port_in_use register: port_in_use
changed_when: false changed_when: false
failed_when: port_in_use.rc == 0 failed_when: port_in_use.rc == 0
- name: Open SSH tunnel to Portainer (fixed local port 9443) - name: Open SSH tunnel to Portainer
shell: | shell: >
rm -f "{{ ssh_log }}" ssh -p {{ bastion_port }}
ssh -p {{ bastion_port }} \ -o ExitOnForwardFailure=yes
-o ExitOnForwardFailure=yes \ -o StrictHostKeyChecking=no
-o StrictHostKeyChecking=no \ -o UserKnownHostsFile=/dev/null
-o UserKnownHostsFile=/dev/null \ -M -S {{ ssh_control_socket }}
-o ServerAliveInterval=10 \ -f -N
-o ServerAliveCountMax=3 \ -L 127.0.0.1:9443:10.30.0.151:9443
-o ConnectTimeout=10 \ {{ bastion_user }}@{{ bastion_host }}
-M -S {{ ssh_control_socket }} \
-f -N \
-L 127.0.0.1:9443:10.30.0.151:9443 \
{{ bastion_user }}@{{ bastion_host }} \
> "{{ ssh_log }}" 2>&1
changed_when: true changed_when: true
- name: Wait for local tunnel port 9443 - name: Wait for tunnel to be ready
wait_for: wait_for:
host: 127.0.0.1 host: 127.0.0.1
port: 9443 port: 9443
delay: 1
timeout: 20 timeout: 20
- name: Check Portainer is reachable - name: Check Portainer API
uri: uri:
url: "{{ local_portainer_url }}/api/status" url: "{{ portainer_url }}/api/status"
method: GET method: GET
validate_certs: false validate_certs: false
headers: headers:
X-API-Key: "{{ lookup('env','PORTAINER_API_KEY') }}" X-API-Key: "{{ lookup('env','PORTAINER_API_KEY') }}"
status_code: [200] status_code: [200]
register: status_check
- name: Get endpoints list - name: Get Portainer endpoints
uri: uri:
url: "{{ local_portainer_url }}/api/endpoints" url: "{{ portainer_url }}/api/endpoints"
method: GET method: GET
validate_certs: false validate_certs: false
headers: headers:
X-API-Key: "{{ lookup('env','PORTAINER_API_KEY') }}" X-API-Key: "{{ lookup('env','PORTAINER_API_KEY') }}"
status_code: [200]
register: endpoints_resp register: endpoints_resp
- name: Pick endpoint_id (use provided or first) - name: Select first endpoint
set_fact: set_fact:
endpoint_id_effective: >- endpoint_id: "{{ endpoints_resp.json[0].Id }}"
{{
(endpoint_id | string | length > 0)
| ternary(endpoint_id | int, (endpoints_resp.json[0].Id | int))
}}
- name: Create test stack (Compose, method=string) - name: Create test stack
uri: uri:
url: "{{ local_portainer_url }}/api/stacks?type={{ stack_type }}&method=string&endpointId={{ endpoint_id_effective }}" url: "{{ portainer_url }}/api/stacks?type={{ stack_type }}&method=string&endpointId={{ endpoint_id }}"
method: POST method: POST
validate_certs: false validate_certs: false
headers: headers:
X-API-Key: "{{ lookup('env','PORTAINER_API_KEY') }}" X-API-Key: "{{ lookup('env','PORTAINER_API_KEY') }}"
Content-Type: "application/json" Content-Type: application/json
body_format: json body_format: json
body: body:
Name: "{{ stack_name }}" Name: "{{ stack_name }}"
StackFileContent: "{{ stack_compose }}" StackFileContent: "{{ stack_compose }}"
Env: [] Env: []
# Si la stack existe déjà, Portainer peut renvoyer une erreur 409
status_code: [200, 201, 409] status_code: [200, 201, 409]
register: create_stack_resp register: create_stack
- name: Result - name: Result
debug: debug:
msg: msg: >
- "✅ API status: {{ status_check.status }}" Stack creation HTTP status {{ create_stack.status }}
- "✅ endpointId utilisé: {{ endpoint_id_effective }}" (409 means the stack already exists)
- "✅ Create stack HTTP status: {{ create_stack_resp.status }}"
- " Si status=409: la stack '{{ stack_name }}' existe déjà."
always: always:
- name: Close SSH tunnel - name: Close SSH tunnel