Files
homework_8/ansible/playbooks/site.yml
T

101 lines
3.2 KiB
YAML

---
- name: Deploy OTUS HW8 Jenkins infrastructure
hosts: local
gather_facts: true
vars:
hw8_root: "{{ playbook_dir }}/../.."
compose_dir: "{{ hw8_root }}/compose"
compose_file: "{{ compose_dir }}/docker-compose.yml"
compose_env_file: "{{ compose_dir }}/.env"
tasks:
- name: Check Docker CLI availability
ansible.builtin.command: docker --version
changed_when: false
- name: Check Docker Compose availability
ansible.builtin.command: docker compose version
changed_when: false
- name: Create compose env file from template if missing
ansible.builtin.copy:
src: "{{ compose_dir }}/.env.example"
dest: "{{ compose_env_file }}"
force: true
mode: "0644"
- name: Ensure image build script is executable
ansible.builtin.file:
path: "{{ compose_dir }}/scripts/build_and_push_images.sh"
mode: "0755"
- name: Pre-pull Jenkins base image (retry on Docker Hub timeouts)
ansible.builtin.command:
cmd: docker pull jenkins/jenkins:2.541.3-lts-jdk21
register: pull_jenkins_base
retries: 6
delay: 20
until: pull_jenkins_base.rc == 0
- name: Build and start registry, jenkins and nginx
ansible.builtin.command:
cmd: docker compose -f {{ compose_file }} --env-file {{ compose_env_file }} up -d --build registry jenkins nginx
args:
chdir: "{{ compose_dir }}"
register: compose_bootstrap
retries: 4
delay: 20
until: compose_bootstrap.rc == 0
- name: Wait for Jenkins to become available
ansible.builtin.uri:
url: "http://127.0.0.1:8081/login"
status_code: 200
register: jenkins_ready
retries: 60
delay: 5
until: jenkins_ready.status == 200
- name: Build and push slave/test images to local registry
ansible.builtin.command:
cmd: ./scripts/build_and_push_images.sh localhost:5005 1.0.0
args:
chdir: "{{ compose_dir }}"
register: build_and_push_result
retries: 3
delay: 20
until: build_and_push_result.rc == 0
- name: Start swarm agents
ansible.builtin.command:
cmd: docker compose -f {{ compose_file }} --env-file {{ compose_env_file }} up -d agent-maven agent-jjb
args:
chdir: "{{ compose_dir }}"
- name: Build jobs_uploader image with latest changes
ansible.builtin.command:
cmd: docker compose -f {{ compose_file }} --env-file {{ compose_env_file }} build jobs_uploader
args:
chdir: "{{ compose_dir }}"
- name: Remove stale jobs_uploader run containers
ansible.builtin.shell: |
ids="$(docker ps -aq --filter "name=jobs_uploader-run" || true)"
if [ -n "${ids}" ]; then
docker rm -f ${ids}
fi
args:
executable: /bin/bash
- name: Upload Jenkins jobs via JJB container
ansible.builtin.command:
cmd: timeout 900 docker compose -f {{ compose_file }} --env-file {{ compose_env_file }} run --rm jobs_uploader
args:
chdir: "{{ compose_dir }}"
- name: Show endpoint details
ansible.builtin.debug:
msg:
- "Jenkins UI: http://localhost:8088 (via nginx) or http://localhost:8081"
- "Registry: http://localhost:5005/v2/"