fab1bdf675906d30308db6aef995a198a756cf22
[infra/stack/kubernetes.git] / playbooks / roles / prepare-artifacts / tasks / main.yaml
1 ---
2 # ============LICENSE_START=======================================================
3 #  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END=========================================================
19
20 - name: Get list of k8s container image tarfiles
21   find:
22     path: "{{ engine_workspace }}/offline/containers"
23     patterns: ['*.tar', '*.tgz', '*.tar.gz']
24   register: container_image
25
26 # NOTE (fdegir): the user may not be member of docker group so we need root
27 # TODO (fdegir): we can perhaps skip loading already existing images here
28 - name: Load k8s container images from tarfiles
29   shell: docker load < {{ item.path }}
30   loop: "{{ container_image.files }}"
31   changed_when: false
32   become: true
33
34 # NOTE (fdegir): the escape of curly brackets in ansible is really messy unfortunately
35 # we also shouldn't attempt to tag and push container images that are already on local registry
36 # NOTE (fdegir): we do not push any image that is already on engine.local and any image without tag
37 - name: Get list of loaded k8s container images to push
38   shell: |
39     set -o pipefail
40     docker images --format '{{ '{{' }}.Repository{{ '}}' }}':'{{ '{{' }}.Tag{{ '}}' }}' | grep -v '{{ server_fqdn }}\|<none>' | sort
41   args:
42     executable: /bin/bash
43   changed_when: false
44   become: true
45   register: container_images
46
47 - name: Create dict of k8s container images to tag and push
48   set_fact:
49     container_images_dict: "{{ ( container_images_dict | default({}) ) | combine({item: item | regex_replace('.*?.io/', '')}) }}"
50   loop: "{{ container_images.stdout_lines }}"
51
52 # TODO (fdegir): it is messy to use ansible module for tagging and pushing but we can still look into it
53 # TODO (fdegir): we can perhaps skip tagging & pushing already existing images here
54 - name: Tag and push k8s container images to local registry
55   shell: |
56     docker tag {{ item.key }} {{ server_fqdn }}/{{ item.value }}
57     docker push {{ server_fqdn }}/{{ item.value }}
58   with_dict: "{{ container_images_dict }}"
59   changed_when: false
60   become: true
61
62 # vim: set ts=2 sw=2 expandtab: