|
1 | 1 | ---
|
2 |
| -- hosts: localhost |
3 |
| - become: yes |
| 2 | +- name: Build and run a Java container with Podman |
| 3 | + hosts: localhost |
4 | 4 | tasks:
|
5 |
| - - name: Install Podman if not present |
6 |
| - apt: |
7 |
| - name: podman |
8 |
| - state: present |
9 |
| - update_cache: yes |
| 5 | + - name: Generate a random number |
| 6 | + set_fact: |
| 7 | + random_number: "{{ 9999 | random }}" |
10 | 8 |
|
11 |
| - - name: Pull the latest OpenJDK image using Podman |
12 |
| - ansible.builtin.command: |
13 |
| - cmd: podman pull docker.io/library/openjdk:latest |
14 |
| - register: podman_pull_result |
| 9 | + - name: Remove existing container if it exists |
| 10 | + command: > |
| 11 | + podman rm -f java_test_container_{{ random_number }} |
| 12 | + ignore_errors: yes |
| 13 | + failed_when: false |
15 | 14 |
|
16 |
| - - name: Ensure the image is pulled |
17 |
| - debug: |
18 |
| - msg: "Pulled image: {{ podman_pull_result.stdout }}" |
| 15 | + - name: Check if Podman network exists |
| 16 | + command: > |
| 17 | + podman network inspect podman |
| 18 | + register: network_inspect |
| 19 | + ignore_errors: yes |
19 | 20 |
|
20 |
| - - name: Run Java container with Podman and mount the test-code directory |
21 |
| - ansible.builtin.command: |
22 |
| - cmd: podman run -d --name java_test_container -v /home/user/test-code:/usr/src/myapp docker.io/library/openjdk:latest sleep infinity |
23 |
| - register: podman_run_result |
| 21 | + - name: Create Podman network if it does not exist |
| 22 | + command: > |
| 23 | + podman network create podman |
| 24 | + when: network_inspect.rc != 0 |
24 | 25 |
|
25 |
| - - name: Ensure the container is running |
26 |
| - debug: |
27 |
| - msg: "Container running: {{ podman_run_result.stdout }}" |
28 |
| - |
29 |
| - - name: Display running Podman containers |
30 |
| - ansible.builtin.command: |
31 |
| - cmd: podman ps |
| 26 | + - name: Run Java container interactively with Podman and mount the test-code directory |
| 27 | + command: > |
| 28 | + podman run -it --name java_test_container_{{ random_number }} |
| 29 | + -v /home/edo9k/Documentos/repos/learn:/usr/src/myapp |
| 30 | + docker.io/library/openjdk:latest |
| 31 | + /bin/bash |
32 | 32 |
|
0 commit comments