summaryrefslogtreecommitdiffstats
path: root/roles/pve-lxc-ssh/tasks/main.yml
blob: f5d402bca5715f70dff2a92098ce21eb31497984 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
- name: "Check if sshd is installed"
  become: true
  ansible.builtin.command:
    cmd: >
      pct exec {{ pve_lxc_id }} -- sh -c '
      . /etc/os-release;
      case $ID in
        alpine) apk info openssh ;;
        debian|ubuntu) dpkg -l openssh-server ;;
        *) echo "Unsupported distro: $ID" && exit 1 ;;
      esac;'
  register: pve_lxc_ssh_check
  changed_when: false
  failed_when: false
  tags:
    - "molecule-notest"

- name: "Install openssh and configure openssh"
  become: true
  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: "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: "Render sshd_config"
      ansible.builtin.template:
        src: "sshd_config.j2"
        dest: "/tmp/sshd_config_{{ pve_lxc_id }}"
        mode: "0600"

    - 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: "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