]> git.arvados.org - arvados.git/blob - tools/ansible/build-compute-image.yml
Merge branch '22754-process-panel-slowness' into main. Closes #22754
[arvados.git] / tools / ansible / build-compute-image.yml
1 #!/usr/bin/env ansible-playbook
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: Apache-2.0
5
6 - name: Load Arvados configuration
7   hosts: default
8   tasks:
9     - name: Load Arvados configuration file
10       delegate_to: localhost
11       ansible.builtin.include_vars:
12         name: arvados_config
13         file: "{{ arvados_config_file }}"
14     - name: Load Arvados cluster configuration
15       ansible.builtin.set_fact:
16         arvados_cluster: "{{ arvados_config.Clusters[arvados_cluster_id] }}"
17       failed_when: arvados_cluster is undefined
18     - name: Get Crunch dispatch public key
19       when: arvados_cluster.Containers.DispatchPrivateKey is defined and arvados_cluster.Containers.DispatchPrivateKey is truthy
20       delegate_to: localhost
21       block:
22         # `ssh-keygen` supports reading stdin for some operations with `-f -`,
23         # but `-y` is not one of those operations as of April 2025.
24         # We MUST have the dispatch private key in a file with correct
25         # permissions for `ssh-keygen -y -f` to read.
26         - name: Prepare tempfile for dispatch private key
27           ansible.builtin.tempfile:
28             suffix: ".key"
29           register: key_tempfile
30
31         # Try to parse DispatchPrivateKey as a URL.
32         # If it's recognized as a file: URL, copy that path to the tempfile.
33         # Otherwise, expect it's the private key,
34         # and write that content directly to the tempfile.
35         - name: Save dispatch private key to tempfile
36           vars:
37             key_url: "{{ arvados_cluster.Containers.DispatchPrivateKey | urlsplit }}"
38           ansible.builtin.copy:
39             src: "{{ key_url.path if key_url.scheme == 'file' else omit }}"
40             content: "{{ arvados_cluster.Containers.DispatchPrivateKey|regex_replace('\\n?$', '\\n') if key_url.scheme != 'file' else omit }}"
41             dest: "{{ key_tempfile.path }}"
42             mode: 0600
43
44         - name: Derive dispatch public key
45           ansible.builtin.command:
46             argv:
47               - ssh-keygen
48               - "-y"
49               - "-f"
50               - "{{ key_tempfile.path }}"
51           register: compute_dispatch_ssh_keygen
52
53       always:
54         - name: Remove dispatch private key tempfile
55           when: key_tempfile is defined
56           ansible.builtin.file:
57             path: "{{ key_tempfile.path }}"
58             state: absent
59
60 - name: Set up compute node base distribution
61   # `default` is the name that the Packer Ansible plugin assigns to the
62   # instance used to create the image.
63   hosts: default
64   tasks:
65     - name: Bootstrap packages required for Ansible
66       become: yes
67       ansible.builtin.raw: "apt-get -o DPkg::Lock::Timeout=300 -qy {{ item }}"
68       loop:
69         - update
70         - install gnupg python3-apt python3-debian xz-utils
71     - include_role:
72         name: distro_apt
73     - include_role:
74         name: arvados_apt
75     - name: List linux-image packages pre-upgrade
76       ansible.builtin.shell:
77         cmd: |
78           dpkg-query --list "linux-image-[1-9]*-$(dpkg --print-architecture)" |
79           awk '($1 ~ /^[irp][HUFWti]$/) { print $2; }'
80       register: linux_image_preupgrade
81     - name: Upgrade packages
82       become: yes
83       ansible.builtin.apt:
84         update_cache: true
85         upgrade: true
86     - name: Remove unwanted packages
87       become: yes
88       ansible.builtin.apt:
89         state: absent
90         purge: true
91         name:
92           - unattended-upgrades
93     - name: List linux-image packages post-upgrade
94       ansible.builtin.shell:
95         cmd: |
96           dpkg-query --list "linux-image-[1-9]*-$(dpkg --print-architecture)" |
97           awk '($1 ~ /^[irp][HUFWti]$/) { print $2; }'
98       register: linux_image_postupgrade
99     # Rebooting now accomplishes a few things: it means we can remove the old
100     # linux-image afterward, and the `ansible_kernel` fact will reflect what
101     # the image will boot into when used.
102     - name: Reboot into new kernel
103       when: "linux_image_preupgrade.stdout != linux_image_postupgrade.stdout"
104       become: yes
105       ansible.builtin.reboot: {}
106     - name: Remove old kernel(s)
107       when: "linux_image_preupgrade.stdout != linux_image_postupgrade.stdout"
108       become: yes
109       ansible.builtin.apt:
110         state: absent
111         purge: true
112         name: "{{ linux_image_preupgrade.stdout_lines }}"
113   handlers:
114     - name: apt update
115       ansible.builtin.debug:
116         msg: Skipping apt update handler before an apt upgrade
117         verbosity: 1
118
119 - name: Install compute node software
120   hosts: default
121   tasks:
122     - ansible.builtin.include_role:
123         name: compute_encrypt_tmp
124     - ansible.builtin.include_role:
125         name: arvados_compute
126     - ansible.builtin.include_role:
127         name: compute_docker
128       when: "arvados_cluster.Containers.RuntimeEngine == 'docker'"
129     - ansible.builtin.include_role:
130         name: compute_user
131
132     - name: Configure DNS
133       become: yes
134       ansible.builtin.lineinfile:
135         path: /etc/dhcp/dhclient.conf
136         regexp: "^[# ]*prepend +domain-name-servers "
137         line: "prepend domain-name-servers {{ dns_resolver }};"
138       when: dns_resolver is defined
139
140     - name: Clean apt packages
141       become: yes
142       ansible.builtin.apt:
143         autoremove: true
144         clean: true