Skip to content

Commit d7c3cce

Browse files
author
Jonathan Rosser
committed
Add utility playbook to download required binaries for all roles
Usage: ansible-playbook vexxhost.kubernetes.download_binaries -e target=localhost This playbook uses the existing download_artifact role and downloads all versions of all binaries required for all roles. Pass extra variables with -e as required to configure download_artifact.
1 parent 3cd365b commit d7c3cce

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- name: include playbook
2+
ansible.builtin.import_playbook: vexxhost.kubernetes.download_binaries
3+
vars:
4+
target: instance
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright (c) 2023 VEXXHOST, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
dependency:
16+
name: galaxy
17+
driver:
18+
name: docker
19+
platforms:
20+
- name: instance
21+
image: geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2204}-ansible:latest
22+
command: ${MOLECULE_DOCKER_COMMAND:-""}
23+
privileged: true
24+
cgroupns_mode: host
25+
pre_build_image: true
26+
environment:
27+
container: docker
28+
security_opts:
29+
- apparmor=unconfined
30+
volumes:
31+
- /sys/fs/cgroup:/sys/fs/cgroup:rw
32+
- /lib/modules:/lib/modules:ro
33+
provisioner:
34+
name: ansible
35+
config_options:
36+
connection:
37+
pipelining: true
38+
verifier:
39+
name: ansible
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (c) 2023 VEXXHOST, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
- name: Prepare
16+
hosts: all
17+
become: true
18+
pre_tasks:
19+
- name: Wait for systemd to complete initialization
20+
ansible.builtin.command: systemctl is-system-running
21+
register: systemctl_status
22+
until: >
23+
'running' in systemctl_status.stdout or
24+
'degraded' in systemctl_status.stdout
25+
retries: 30
26+
delay: 5
27+
changed_when: false
28+
failed_when: systemctl_status.rc > 1
29+
tasks:
30+
- name: Run APT update
31+
ansible.builtin.apt:
32+
update_cache: yes
33+
when: ansible_facts['pkg_mgr'] == "apt"

molecule/download_binaries/verify.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (c) 2023 VEXXHOST, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
- name: Verify
16+
hosts: all
17+
become: true
18+
vars:
19+
dir: /var/lib/downloads
20+
tasks:
21+
- name: Stat target dir
22+
ansible.builtin.stat:
23+
path: "{{ dir }}"
24+
register: _dir_stat
25+
26+
- name: Assert that target dir is created
27+
ansible.builtin.assert:
28+
that:
29+
- _dir_stat.stat.exists
30+
- _dir_stat.stat.isdir
31+
32+
- name: Find target files
33+
ansible.builtin.find:
34+
paths: "{{ dir }}"
35+
register: _dir_find
36+
37+
- name: Assert that some files were downloaded
38+
ansible.builtin.assert:
39+
that:
40+
- _dir_find.matched > 0

playbooks/download_binaries.yml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
- name: Download all binaries required at runtime for ansible-collection-kubernetes
2+
hosts: "{{ target | default('all') }}"
3+
gather_facts: false
4+
vars:
5+
# some role defaults use vars only defined in the download_artifact role
6+
_download_artifact_goarch_groups:
7+
x86_64: amd64
8+
aarch64: arm64
9+
armv7l: arm
10+
11+
download_artifact_goarch: >-
12+
{%- if ansible_facts['architecture'] in _download_artifact_goarch_groups -%}
13+
{{ _download_artifact_goarch_groups[ansible_facts['architecture']] }}
14+
{%- else -%}
15+
{{ ansible_facts['architecture'] }}
16+
{%- endif -%}
17+
18+
download_artifact_work_directory: /var/lib/downloads
19+
20+
role_location: "{{ playbook_dir }}/../roles"
21+
tasks:
22+
- name: Gather minimal facts
23+
ansible.builtin.setup:
24+
gather_subset: min
25+
delegate_to: localhost
26+
27+
- name: Find all subdirectories in the role location
28+
ansible.builtin.find:
29+
file_type: directory
30+
paths: "{{ role_location }}"
31+
recurse: true
32+
register: role_paths
33+
delegate_to: localhost
34+
35+
- name: Select only defaults/ directories and load vars
36+
# also load vars from download_artifact to get architecture mappings
37+
ansible.builtin.include_vars:
38+
dir: "{{ item }}"
39+
with_items:
40+
- "{{ role_paths.files | selectattr('path', 'search', 'defaults') | map(attribute='path') }}"
41+
delegate_to: localhost
42+
43+
- name: Generate list of all binaries for all roles
44+
vexxhost.containers.binary_downloads:
45+
prefixes: "{{ query('varnames', '_download_url$') | map('replace', '_download_url', '') }}"
46+
register: binaries
47+
delegate_to: localhost
48+
49+
- name: Include download_artifact role
50+
ansible.builtin.include_role:
51+
name: vexxhost.containers.download_artifact
52+
vars:
53+
download_artifact_url: "{{ item.url }}"
54+
download_artifact_dest: "{{ item.dest }}"
55+
download_artifact_checksum: "sha256:{{ item.checksum }}"
56+
download_artifact_owner: "{{ ansible_user }}"
57+
download_artifact_mode: "0755"
58+
download_artifact_unarchive: false
59+
download_artifact_no_log: false
60+
with_items: "{{ binaries.downloads }}"

0 commit comments

Comments
 (0)