aboutsummaryrefslogtreecommitdiff
path: root/containers.yml
blob: 048a981982250bc4626bafd3ab41aff678460d02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
- hosts: localhost

  tasks:
    - name: Rebuild images
      containers.podman.podman_image:
        name: "{{ item.name }}:{{ ansible_date_time.date }}-{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}{{ ansible_date_time.second }}"
        path: "{{ item.path }}"
        build:
          file: "{{ item.file }}"
          cache: false
      tags: rebuild
      loop: "{{ containers }}"

    - name: Tag new images to latest
      containers.podman.podman_tag:
        image: "{{ item.name }}:{{ ansible_date_time.date }}-{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}{{ ansible_date_time.second }}"
        target_names: "{{ item.name }}:latest"
      tags: rebuild
      loop: "{{ containers }}"

    - name: Stop systemd pod service
      ansible.builtin.systemd:
        name: "pod-{{ pod.name }}.service"
        state: stopped
        scope: user
      ignore_errors: true
      tags:
        - remove
        - service-stop
      when: pod is defined

    - name: Remove systemd pod service file
      ansible.builtin.file:
        path: "~/.config/systemd/user/pod-{{ pod.name }}.service"
        state: absent
      tags: remove
      when: pod is defined

    - name: Stop systemd container service
      ansible.builtin.systemd:
        name: "container-{{ item.name }}.service"
        state: stopped
        scope: user
      ignore_errors: true
      tags:
        - remove
        - service-stop
      loop: "{{ containers }}"

    - name: Remove systemd container service file
      ansible.builtin.file:
        path: "~/.config/systemd/user/container-{{ item.name }}.service"
        state: absent
      tags: remove
      loop: "{{ containers }}"

    - name: Remove pod
      containers.podman.podman_pod:
        name: "{{ pod.name }}"
        state: absent
      tags:
        - remove
        - container-stop
      when: pod is defined

    - name: Remove containers
      containers.podman.podman_container:
        name: "{{ item.name }}"
        state: absent
      tags:
        - remove
        - container-stop
      loop: "{{ containers }}"

    - name: Create pod
      containers.podman.podman_pod:
        name: "{{ pod.name }}"
        ports: "{{ pod.ports }}"
        state: started
      tags: container-start
      when: pod is defined

    - name: Start containers
      ansible.builtin.shell: "podman container runlabel {{ item.runlabel }} {{ item.name}}:latest"
      tags: container-start
      loop: "{{ containers }}"

    - name: Generate systemd service file for pod
      containers.podman.podman_generate_systemd:
        name: "{{ pod.name }}"
        dest: "~/.config/systemd/user/"
        new: true
      tags: generate-systemd
      when: pod is defined

    - name: Generate systemd service file for container (when pod is not in use)
      containers.podman.podman_generate_systemd:
        name: "{{ item.name }}"
        dest: "~/.config/systemd/user/"
        new: true
      tags: generate-systemd
      loop: "{{ containers }}"
      when: pod is not defined

    - name: Reload systemd daemon
      ansible.builtin.systemd:
        daemon_reload: true
        scope: user
      tags:
        - remove
        - generate-systemd

    - name: Pause for container full startup
      ansible.builtin.pause:
        seconds: 30

    - name: Remove pod
      containers.podman.podman_pod:
        name: "{{ pod.name }}"
        state: absent
      when: pod is defined

    - name: Remove containers
      containers.podman.podman_container:
        name: "{{ item.name }}"
        state: absent
      loop: "{{ containers }}"

    - name: Start systemd pod service
      ansible.builtin.systemd:
        name: "pod-{{ pod.name}}.service"
        state: started
        enabled: true
        scope: user
      tags: service-start
      when: pod is defined

    - name: Start systemd container service
      ansible.builtin.systemd:
        name: "container-{{ item.name }}.service"
        state: started
        enabled: true
        scope: user
      tags: service-start
      loop: "{{ containers }}"

    - name: Prune old images
      ansible.builtin.shell: podman image prune -f