diff --git a/ansible/roles/jupyter-repo2docker/tasks/main.yml b/ansible/roles/jupyter-repo2docker/tasks/main.yml index c024bf9a..ffa24c13 100644 --- a/ansible/roles/jupyter-repo2docker/tasks/main.yml +++ b/ansible/roles/jupyter-repo2docker/tasks/main.yml @@ -20,7 +20,7 @@ when: ansible_os_family == "Debian" - include_role: - name: linux-ansible-init + name: azimuth_cloud.image_utils.linux_ansible_init - include_role: name: linux-podman diff --git a/ansible/roles/linux-ansible-init/files/ansible-init.py b/ansible/roles/linux-ansible-init/files/ansible-init.py deleted file mode 100644 index 5e9a77a0..00000000 --- a/ansible/roles/linux-ansible-init/files/ansible-init.py +++ /dev/null @@ -1,105 +0,0 @@ -#!/usr/lib/ansible-init/bin/python - -import json -import logging -import os -import pathlib -import subprocess - -import requests - - -logging.basicConfig(level = logging.INFO, format = "[%(levelname)s] %(message)s") - - -logger = logging.getLogger(__name__) - - -def assemble_list(data, prefix): - """ - Assembles a list of items based on keys in data with the format "__". - """ - list_items = {} - for key, value in data.items(): - if not key.startswith(prefix): - continue - idx, item_key = key.removeprefix(prefix).split("_", maxsplit = 1) - list_items.setdefault(idx, {})[item_key] = value - return [list_items[k] for k in sorted(list_items.keys())] - - -def ansible_exec(cmd, *args, **kwargs): - """ - Execute an Ansible command with the appropriate environment. - """ - environ = os.environ.copy() - environ["ANSIBLE_CONFIG"] = "/etc/ansible-init/ansible.cfg" - cmd = f"/usr/lib/ansible-init/bin/ansible-{cmd}" - subprocess.run([cmd, *args], env = environ, check = True, **kwargs) - - -logger.info("fetching instance metadata") -METADATA_URL = "http://169.254.169.254/openstack/latest/meta_data.json" -response = requests.get(METADATA_URL) -response.raise_for_status() -user_metadata = response.json().get("meta", {}) - - -logger.info("extracting collections and playbooks from metadata") -collections = assemble_list(user_metadata, "ansible_init_coll_") -playbooks = assemble_list(user_metadata, "ansible_init_pb_") -logger.info(f" found {len(collections)} collections") -logger.info(f" found {len(playbooks)} playbooks") - - -logger.info("installing collections") -ansible_exec( - "galaxy", - "collection", - "install", - "--force", - "--requirements-file", - "/dev/stdin", - input = json.dumps({ "collections": collections }).encode() -) - - -logger.info("executing remote playbooks for stage - pre") -for playbook in playbooks: - if playbook.get("stage", "post") == "pre": - logger.info(f" executing playbook - {playbook['name']}") - ansible_exec( - "playbook", - "--connection", - "local", - "--inventory", - "127.0.0.1,", - playbook["name"] - ) - - -logger.info("executing playbooks from /etc/ansible-init/playbooks") -for playbook in sorted(pathlib.Path("/etc/ansible-init/playbooks").glob("*.yml")): - logger.info(f" executing playbook - {playbook}") - ansible_exec( - "playbook", - "--connection", - "local", - "--inventory", - "127.0.0.1,", - str(playbook) - ) - - -logger.info("executing remote playbooks for stage - post") -for playbook in playbooks: - if playbook.get("stage", "post") == "post": - logger.info(f" executing playbook - {playbook['name']}") - ansible_exec( - "playbook", - "--connection", - "local", - "--inventory", - "127.0.0.1,", - playbook["name"] - ) diff --git a/ansible/roles/linux-ansible-init/files/ansible-init.service b/ansible/roles/linux-ansible-init/files/ansible-init.service deleted file mode 100644 index e0876424..00000000 --- a/ansible/roles/linux-ansible-init/files/ansible-init.service +++ /dev/null @@ -1,18 +0,0 @@ -[Unit] -Description= -Wants=network.target -After=network-online.target -ConditionPathExists=!/var/lib/ansible-init.done - -[Service] -Type=oneshot -RemainAfterExit=true -ExecStart=/usr/bin/ansible-init -ExecStartPost=touch /var/lib/ansible-init.done -Restart=on-failure -RestartSec=10 -StandardOutput=journal+console -StandardError=journal+console - -[Install] -WantedBy=multi-user.target default.target diff --git a/ansible/roles/linux-ansible-init/files/ansible.cfg b/ansible/roles/linux-ansible-init/files/ansible.cfg deleted file mode 100644 index 6c11f510..00000000 --- a/ansible/roles/linux-ansible-init/files/ansible.cfg +++ /dev/null @@ -1,3 +0,0 @@ -[defaults] -home = /var/lib/ansible-init -local_tmp = /var/tmp/ansible-init diff --git a/ansible/roles/linux-ansible-init/tasks/main.yml b/ansible/roles/linux-ansible-init/tasks/main.yml deleted file mode 100644 index 1b037791..00000000 --- a/ansible/roles/linux-ansible-init/tasks/main.yml +++ /dev/null @@ -1,46 +0,0 @@ ---- - -- name: Install python3-venv - package: - name: python3-venv - -- name: Install dependencies into virtualenv - pip: - name: - - ansible - - jmespath - - requests - state: latest - virtualenv: /usr/lib/ansible-init - virtualenv_command: python3 -m venv --upgrade-deps - -- name: Ensure config directories exist - file: - state: directory - path: "/etc/ansible-init/{{ item }}" - loop: - - vars - - playbooks - - includes - -- name: Install Ansible config file - copy: - src: ansible.cfg - dest: /etc/ansible-init/ansible.cfg - -- name: Install ansible-init script - copy: - src: ansible-init.py - dest: /usr/bin/ansible-init - mode: +x - -- name: Install systemd unit file - copy: - src: ansible-init.service - dest: /etc/systemd/system/ansible-init.service - -- name: Ensure ansible-init service is enabled - systemd: - daemon_reload: yes - name: ansible-init.service - enabled: yes diff --git a/ansible/roles/linux-rdp-gateway/tasks/main.yml b/ansible/roles/linux-rdp-gateway/tasks/main.yml index cfb57a7c..91d8b2fe 100644 --- a/ansible/roles/linux-rdp-gateway/tasks/main.yml +++ b/ansible/roles/linux-rdp-gateway/tasks/main.yml @@ -20,7 +20,7 @@ when: ansible_os_family == "Debian" - include_role: - name: linux-ansible-init + name: azimuth_cloud.image_utils.linux_ansible_init - include_role: name: linux-podman diff --git a/ansible/roles/linux-rstudio/tasks/main.yml b/ansible/roles/linux-rstudio/tasks/main.yml index c2be6b13..69c205dc 100644 --- a/ansible/roles/linux-rstudio/tasks/main.yml +++ b/ansible/roles/linux-rstudio/tasks/main.yml @@ -20,7 +20,7 @@ when: ansible_os_family == "Debian" - include_role: - name: linux-ansible-init + name: azimuth_cloud.image_utils.linux_ansible_init - include_role: name: linux-podman diff --git a/ansible/roles/linux-webconsole/tasks/main.yml b/ansible/roles/linux-webconsole/tasks/main.yml index 6123eac4..f78bdf52 100644 --- a/ansible/roles/linux-webconsole/tasks/main.yml +++ b/ansible/roles/linux-webconsole/tasks/main.yml @@ -52,7 +52,7 @@ when: desktop_enabled is defined and desktop_enabled - include_role: - name: linux-ansible-init + name: azimuth_cloud.image_utils.linux_ansible_init - include_role: name: linux-podman diff --git a/requirements.yml b/requirements.yml index 0f382856..072de5a7 100644 --- a/requirements.yml +++ b/requirements.yml @@ -14,6 +14,9 @@ collections: version: 1.15.4 - name: prometheus.prometheus version: 0.17.1 + - name: https://github.com/azimuth-cloud/ansible-collection-image-utils + type: git + version: 0.2.0 roles: - src: https://github.com/stackhpc/ansible-role-os-manila-mount.git