From ec1143c760901aab83dab402a6989c8a4aaa4c28 Mon Sep 17 00:00:00 2001 From: Ahmed Abdelhalim Date: Sat, 18 Jul 2026 02:19:08 +0200 Subject: Fix pve-lxc-ssh to install ssh correctly --- roles/pve-lxc-ssh/tasks/main.yml | 109 ++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 52 deletions(-) diff --git a/roles/pve-lxc-ssh/tasks/main.yml b/roles/pve-lxc-ssh/tasks/main.yml index 7a6314cb..f5d402bc 100644 --- a/roles/pve-lxc-ssh/tasks/main.yml +++ b/roles/pve-lxc-ssh/tasks/main.yml @@ -1,69 +1,74 @@ --- -- name: "Install openssh" +- name: "Check if sshd is installed" become: true ansible.builtin.command: cmd: > - pct exec {{ pve_lxc_id }} -- sh -c - 'which sshd > /dev/null 2>&1 && exit 0; + pct exec {{ pve_lxc_id }} -- sh -c ' . /etc/os-release; case $ID in - alpine) apk add --no-cache openssh ;; - debian|ubuntu) apt-get install -y openssh-server ;; + alpine) apk info openssh ;; + debian|ubuntu) dpkg -l openssh-server ;; *) echo "Unsupported distro: $ID" && exit 1 ;; - esac; - echo "installed"' - register: pve_lxc_ssh_install - changed_when: "'installed' in pve_lxc_ssh_install.stdout" + esac;' + register: pve_lxc_ssh_check + changed_when: false + failed_when: false tags: - "molecule-notest" -- name: "Generate ssh host keys" +- name: "Install openssh and configure openssh" become: true - ansible.builtin.command: - cmd: > - pct exec {{ pve_lxc_id }} -- sh -c - '[ -f /etc/ssh/ssh_host_ed25519_key ] && exit 0; - ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -C "" -N ""; - echo "generated"' - register: pve_lxc_ssh_keygen - changed_when: "'generated' in pve_lxc_ssh_keygen.stdout" tags: - "molecule-notest" + when: pve_lxc_ssh_check.rc != 0 + block: + - name: "Install openssh" + ansible.builtin.command: + cmd: > + pct exec {{ pve_lxc_id }} -- sh -c ' + . /etc/os-release; + case $ID in + alpine) apk add --no-cache openssh ;; + debian|ubuntu) apt-get install -y openssh-server ;; + *) echo "Unsupported distro: $ID" && exit 1 ;; + esac;' + changed_when: true -- name: "Render sshd_config" - ansible.builtin.template: - src: "sshd_config.j2" - dest: "/tmp/sshd_config_{{ pve_lxc_id }}" - mode: "0600" - tags: - - "molecule-notest" + - name: "Generate ssh host keys" + become: true + ansible.builtin.command: + cmd: > + pct exec {{ pve_lxc_id }} -- sh -c + '[ -f /etc/ssh/ssh_host_ed25519_key ] && exit 0; + ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -C "" -N "";' + changed_when: true -- name: "Push sshd_config into container" - become: true - ansible.builtin.command: - cmd: pct push {{ pve_lxc_id }} /tmp/sshd_config_{{ pve_lxc_id }} /etc/ssh/sshd_config --perms 0640 - changed_when: true - tags: - - "molecule-notest" + - name: "Render sshd_config" + ansible.builtin.template: + src: "sshd_config.j2" + dest: "/tmp/sshd_config_{{ pve_lxc_id }}" + mode: "0600" -- name: "Remove temp sshd_config" - ansible.builtin.file: - path: "/tmp/sshd_config_{{ pve_lxc_id }}" - state: absent - tags: - - "molecule-notest" + - name: "Push sshd_config into container" + become: true + ansible.builtin.command: + cmd: pct push {{ pve_lxc_id }} /tmp/sshd_config_{{ pve_lxc_id }} /etc/ssh/sshd_config --perms 0640 + changed_when: true -- name: "Enable and start sshd" - become: true - ansible.builtin.command: - cmd: > - pct exec {{ pve_lxc_id }} -- sh -c - '. /etc/os-release; - case $ID in - alpine) rc-update add sshd default; rc-service sshd restart ;; - debian|ubuntu) systemctl enable --now ssh; systemctl restart ssh ;; - *) echo "Unsupported distro: $ID" && exit 1 ;; - esac' - changed_when: false - tags: - - "molecule-notest" + - name: "Remove temp sshd_config" + ansible.builtin.file: + path: "/tmp/sshd_config_{{ pve_lxc_id }}" + state: absent + + - name: "Enable and start sshd" + become: true + ansible.builtin.command: + cmd: > + pct exec {{ pve_lxc_id }} -- sh -c + '. /etc/os-release; + case $ID in + alpine) rc-update add sshd default; rc-service sshd restart ;; + debian|ubuntu) systemctl enable --now ssh; systemctl restart ssh ;; + *) echo "Unsupported distro: $ID" && exit 1 ;; + esac' + changed_when: true -- cgit v1.2.3