Debian Codegraph @

ansible_collections/containers/podman/playbooks/examples/build_node_ai_api.yml

raw
1 ---
2 - name: Build Node.js AI prediction API without Dockerfile using Buildah connection
3 hosts: localhost
4 gather_facts: false
5 vars:
6 buildah_base_image: "node:24"
7 image_name: "my-ai-node-app:latest"
8 workdir: "/app"
9 # App sources live under examples/node_app/
10 app_src_dir: "{{ playbook_dir }}/node_app"
11 tasks:
12 - name: Ensure app sources exist
13 stat:
14 path: "{{ app_src_dir }}/package.json"
15 register: app_sources
16
17 - name: Fail if sources are missing
18 fail:
19 msg: "Example sources not found under {{ app_src_dir }}. Provide package.json and app.js."
20 when: not app_sources.stat.exists
21
22 - name: Start Buildah working container
23 command: buildah from {{ buildah_base_image }}
24 register: from_out
25 changed_when: true
26
27 - name: Set container id fact
28 set_fact:
29 container_id: "{{ from_out.stdout | trim }}"
30
31 - name: Add working container as a dynamic host to inventory
32 add_host:
33 name: "buildcntr_{{ container_id }}"
34 ansible_connection: containers.podman.buildah
35 ansible_host: "{{ container_id }}"
36 ansible_buildah_working_directory: "{{ workdir }}"
37
38 - name: Configure image metadata (workdir)
39 command: buildah config --workingdir {{ workdir }} {{ container_id }}
40
41 - name: Copy package files first for better layer caching
42 copy:
43 src: "{{ item }}"
44 dest: "{{ workdir }}/"
45 with_items:
46 - "{{ app_src_dir }}/package.json"
47 - "{{ app_src_dir }}/package-lock.json"
48 delegate_to: "buildcntr_{{ container_id }}"
49
50 - name: Install dependencies
51 command: npm install
52 delegate_to: "buildcntr_{{ container_id }}"
53
54 - name: Copy application code
55 copy:
56 src: "{{ app_src_dir }}/app.js"
57 dest: "{{ workdir }}/app.js"
58 delegate_to: "buildcntr_{{ container_id }}"
59
60 - name: Expose port and set default command
61 shell: |
62 buildah config --port 3000 {{ container_id }}
63 buildah config --cmd "node app.js" {{ container_id }}
64
65 - name: Commit image
66 command: buildah commit {{ container_id }} {{ image_name }}
67
68 - name: Show resulting image
69 command: buildah images --format '{{"{{.Name}}:{{.Tag}}"}}\t{{"{{.Size}}"}}'
70 register: imgs
71 changed_when: false
72
73 - debug:
74 var: imgs.stdout_lines
75
76