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.

130 lines
3.2 KiB

  1. # We can not use include_role here since it not share thje 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: Check if host certificate exists
  9. stat:
  10. path: "/etc/ssh/ssh_host_ed25519_key-cert.pub"
  11. register: vm_ssh_certificate_exists
  12. - debug:
  13. var: vm_ssh_certificate_exists
  14. verbosity: 2
  15. - block:
  16. - name: Read host public key
  17. slurp:
  18. src: "/etc/ssh/ssh_host_ed25519_key.pub"
  19. register: vm_public_key
  20. - debug:
  21. var: vm_public_key['content']
  22. verbosity: 2
  23. - name: generate host request
  24. set_fact:
  25. ca_request:
  26. type: 'sign_request'
  27. request:
  28. keyType: 'ssh_host'
  29. hostName: '{{ ansible_docker_extra_args | default(inventory_hostname) }}.lilik.it'
  30. keyData: "{{ vm_public_key['content'] | b64decode | replace('\n', '')}}"
  31. - debug:
  32. var: ca_request | to_json
  33. verbosity: 2
  34. - name: start sign request
  35. include: ca-dialog.yaml
  36. vars:
  37. ansible_connection: ssh
  38. - debug:
  39. var: request_result
  40. verbosity: 2
  41. - set_fact:
  42. request_output: "{{ request_result.stdout | from_json }}"
  43. - debug:
  44. var: request_output
  45. verbosity: 2
  46. - name: generate get request
  47. set_fact:
  48. ca_request:
  49. type: 'get_certificate'
  50. requestID: '{{ request_output.requestID }}'
  51. - debug:
  52. var: ca_request
  53. verbosity: 2
  54. - debug:
  55. msg: "Please manualy confirm sign request with id {{ request_output.requestID }}"
  56. - name: wait for cert
  57. include: ca-dialog.yaml
  58. vars:
  59. ansible_connection: ssh
  60. - debug:
  61. var: request_result
  62. verbosity: 2
  63. - set_fact:
  64. cert_key: "{{ request_result.stdout | string | from_json }}"
  65. - name: Write certificate to container
  66. copy:
  67. content: "{{ cert_key.result }}"
  68. dest: "/etc/ssh/ssh_host_ed25519_key-cert.pub"
  69. register: set_pub_key
  70. notify: restart ssh
  71. when: "not vm_ssh_certificate_exists.stat.exists"
  72. - name: lookup user ca key
  73. set_fact:
  74. user_ca_key: "{{ lookup('file', 'test_ssh_ca.pub') }}"
  75. - name: Update container user CA key
  76. copy:
  77. content: "ssh-rsa {{ user_ca_key }}"
  78. dest: "/etc/ssh/user_ca.pub"
  79. notify: restart ssh
  80. - name: add certificate to sshd config
  81. lineinfile:
  82. line: 'HostCertificate /etc/ssh/ssh_host_ed25519_key-cert.pub'
  83. dest: '/etc/ssh/sshd_config'
  84. regexp: '^HostCertificate *'
  85. notify: restart ssh
  86. - name: trust user ca key
  87. lineinfile:
  88. line: 'TrustedUserCAKeys /etc/ssh/user_ca.pub'
  89. dest: '/etc/ssh/sshd_config'
  90. regexp: '^TrustedUserCAKeys *'
  91. notify: restart ssh
  92. - name: permit root login only with certificate
  93. lineinfile:
  94. line: 'PermitRootLogin without-password'
  95. dest: '/etc/ssh/sshd_config'
  96. regexp: '^PermitRootLogin *'
  97. notify: restart ssh
  98. - meta: flush_handlers
  99. - name: "waiting for ssh on {{ ansible_docker_extra_args | default(inventory_hostname) }} to start"
  100. wait_for:
  101. host: "{{ hostvars | ip_from_inventory(inventory_hostname) }}"
  102. port: 22
  103. timeout: 30
  104. delegate_to: "{{ inventory_hostname }}"
  105. delegate_facts: True