Playbooks to a new Lilik
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.5 KiB

  1. # We can not use include_role here since it not share the connection with the current role
  2. - include: roles/service/tasks/main.yaml
  3. vars:
  4. service_name: ssh
  5. service_packages:
  6. - openssh-server
  7. - openssh-sftp-server
  8. - name: lookup user ca key
  9. set_fact:
  10. user_ca_key: "{{ lookup('file', 'test_ssh_ca.pub') }}"
  11. - name: Update container user CA key
  12. copy:
  13. content: "ssh-rsa {{ user_ca_key }}"
  14. dest: "/etc/ssh/user_ca.pub"
  15. notify: restart ssh
  16. - name: Check if host certificate is valid
  17. shell: '[[ $(ssh-keygen -f /etc/ssh/ssh_host_ed25519_key-cert.pub -L |grep "$(ssh-keygen -f /etc/ssh/user_ca.pub -l|cut -d " " -f 2)" -A 3 |grep Valid |cut -d " " -f 13) > $(date +%Y-%m-%dT%H:%M:%S --date "+1 month") ]]'
  18. args:
  19. executable: /bin/bash
  20. register: vm_has_valid_ssh_certificate
  21. changed_when: false
  22. failed_when: false
  23. - debug:
  24. var: vm_has_valid_ssh_certificate
  25. verbosity: 2
  26. - block:
  27. - name: Read host public key
  28. slurp:
  29. src: "/etc/ssh/ssh_host_ed25519_key.pub"
  30. register: vm_public_key
  31. - debug:
  32. var: vm_public_key['content']
  33. verbosity: 2
  34. - name: generate host request
  35. set_fact:
  36. ca_request:
  37. type: 'sign_request'
  38. request:
  39. keyType: 'ssh_host'
  40. hostName: '{{ ansible_docker_extra_args | default(inventory_hostname) }}.lilik.it'
  41. keyData: "{{ vm_public_key['content'] | b64decode | replace('\n', '')}}"
  42. - debug:
  43. var: ca_request | to_json
  44. verbosity: 2
  45. - name: start sign request
  46. include: ca-dialog.yaml
  47. vars:
  48. ansible_connection: ssh
  49. - debug:
  50. var: request_result
  51. verbosity: 2
  52. - set_fact:
  53. request_output: "{{ request_result.stdout | from_json }}"
  54. - debug:
  55. var: request_output
  56. verbosity: 2
  57. - name: generate get request
  58. set_fact:
  59. ca_request:
  60. type: 'get_certificate'
  61. requestID: '{{ request_output.requestID }}'
  62. - debug:
  63. var: ca_request
  64. verbosity: 2
  65. - debug:
  66. msg: "Please manualy confirm sign request with id {{ request_output.requestID }}"
  67. - name: wait for cert
  68. include: ca-dialog.yaml
  69. vars:
  70. ansible_connection: ssh
  71. - debug:
  72. var: request_result
  73. verbosity: 2
  74. - set_fact:
  75. cert_key: "{{ request_result.stdout | string | from_json }}"
  76. - name: Write certificate to container
  77. copy:
  78. content: "{{ cert_key.result }}"
  79. dest: "/etc/ssh/ssh_host_ed25519_key-cert.pub"
  80. register: set_pub_key
  81. notify: restart ssh
  82. when: "vm_has_valid_ssh_certificate.rc != 0"
  83. - name: add certificate to sshd config
  84. lineinfile:
  85. line: 'HostCertificate /etc/ssh/ssh_host_ed25519_key-cert.pub'
  86. dest: '/etc/ssh/sshd_config'
  87. regexp: '^HostCertificate *'
  88. notify: restart ssh
  89. - name: trust user ca key
  90. lineinfile:
  91. line: 'TrustedUserCAKeys /etc/ssh/user_ca.pub'
  92. dest: '/etc/ssh/sshd_config'
  93. regexp: '^TrustedUserCAKeys *'
  94. notify: restart ssh
  95. - name: permit root login only with certificate
  96. lineinfile:
  97. line: 'PermitRootLogin without-password'
  98. dest: '/etc/ssh/sshd_config'
  99. regexp: '^PermitRootLogin *'
  100. notify: restart ssh
  101. - meta: flush_handlers
  102. - name: "waiting for ssh on {{ ansible_docker_extra_args | default(inventory_hostname) }} to start"
  103. wait_for:
  104. host: "{{ hostvars | ip_from_inventory(inventory_hostname) }}"
  105. port: 22
  106. timeout: 30
  107. delegate_to: "{{ inventory_hostname }}"
  108. delegate_facts: True