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.

171 lines
6.0 KiB

  1. - name: check for lxc container dir
  2. stat:
  3. path: '/var/lib/lxc/{{ vm_name }}'
  4. register: lxc_existance
  5. - name: check for lxc container existance
  6. container_exists:
  7. name: "{{ vm_name }}"
  8. register: container_exists
  9. - block:
  10. - name: create the lxc container
  11. lxc_container:
  12. name: "{{ vm_name }}"
  13. backing_store: lvm
  14. vg_name: "{{ inventory_hostname }}vg"
  15. lv_name: "vm_{{ vm_name }}"
  16. fs_type: xfs
  17. container_log: true
  18. template: debian
  19. template_options: --release {{ distro }} --packages=ssh,python
  20. # container_command: |
  21. # echo "ssh-rsa {{ user_ca_key }}" > /etc/ssh/user_ca.pub
  22. # echo "TrustedUserCAKeys /etc/ssh/user_ca.pub" >> /etc/ssh/sshd_config
  23. # sed -i 's/iface eth0 inet dhcp/iface eth0 inet manual/' /etc/network/interfaces
  24. state: stopped
  25. - name: deploy container config
  26. template: src=config.j2 dest="/var/lib/lxc/{{ vm_name }}/config"
  27. - name: start container
  28. lxc_container:
  29. name: "{{ vm_name }}"
  30. state: started
  31. when: auto_start|bool
  32. when: not (container_exists.exists and lxc_existance.stat.isdir)
  33. - name: update container config
  34. template:
  35. src: config.j2
  36. dest: "/var/lib/lxc/{{ vm_name }}/config"
  37. register: container_config
  38. - name: set container running state
  39. lxc_container:
  40. name: "{{ vm_name }}"
  41. state: "{{ container_state }}"
  42. register: container_running_state
  43. - name: update container DNS configuration
  44. shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "grep '^nameserver {{ hostvars[ext_gateway].ansible_host }}$' /etc/resolv.conf || echo 'nameserver {{ hostvars[ext_gateway].ansible_host }}' > /etc/resolv.conf"
  45. register: container_dns_configuration
  46. changed_when: "container_dns_configuration.stdout != 'nameserver {{ hostvars[ext_gateway].ansible_host }}'"
  47. - name: Check if host certificate exists
  48. container_file_exists:
  49. name: "{{ vm_name }}"
  50. path: "/etc/ssh/ssh_host_ed25519_key-cert.pub"
  51. register: host_certificate_exists
  52. - name: check if cert key exist
  53. shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "ls /etc/ssh/ssh_host_ed25519_key-cert.pub"
  54. register: cert_key_existance
  55. ignore_errors: true
  56. changed_when: "cert_key_existance.rc != 0"
  57. - block:
  58. - name: get pub key
  59. shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "cat /etc/ssh/ssh_host_ed25519_key.pub"
  60. register: pub_key
  61. - debug: var=pub_key verbosity=2
  62. - name: generate host request
  63. set_fact:
  64. cert_request:
  65. type: 'sign_request'
  66. request:
  67. keyType: 'ssh_host'
  68. hostName: '{{ vm_name }}'
  69. keyData: '{{ pub_key.stdout }}'
  70. - debug: var=cert_request verbosity=2
  71. - name: start sign request
  72. raw: "{{ cert_request | to_json }}"
  73. delegate_to: "{{ item }}"
  74. delegate_facts: True
  75. with_items: "{{ groups['cas'] }}"
  76. register: request_result
  77. - debug: var=request_result verbosity=2
  78. - set_fact:
  79. request_output: "{{ request_result.results[0].stdout|string|from_json }}"
  80. - debug: var=request_output verbosity=2
  81. - name: generate get request
  82. set_fact:
  83. get_request:
  84. type: 'get_certificate'
  85. requestID: '{{ request_output.requestID }}'
  86. - debug:
  87. var: get_request
  88. verbosity: 2
  89. - debug:
  90. msg: "Please manualy confirm sign request with id {{ request_output.requestID }}"
  91. - name: wait for cert
  92. raw: "{{ get_request | to_json }}"
  93. delegate_to: "{{ item }}"
  94. delegate_facts: True
  95. with_items: "{{ groups['cas'] }}"
  96. register: cert_result
  97. - debug: var=cert_result verbosity=2
  98. - set_fact:
  99. cert_key: "{{ cert_result.results[0].stdout|string|from_json }}"
  100. - debug:
  101. var: request_output
  102. verbosity: 2
  103. - name: set cert key
  104. shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "echo '{{ cert_key.result }}' > /etc/ssh/ssh_host_ed25519_key-cert.pub"
  105. register: set_pub_key
  106. when: "cert_key_existance.rc != 0"
  107. - name: update container network configuration
  108. shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "grep -F 'iface eth0 inet manual' /etc/network/interfaces || sed -i 's/iface eth0 inet dhcp/iface eth0 inet manual/' /etc/network/interfaces"
  109. register: container_network
  110. changed_when: "container_network.stdout != 'iface eth0 inet manual'"
  111. - name: install packages
  112. shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "apt-get install python ssh -y"
  113. register: install_packages
  114. changed_when: "install_packages.stdout.find('0 newly installed') == -1"
  115. - name: lookup user ca key
  116. set_fact:
  117. user_ca_key: "{{ lookup('file', 'test_ssh_ca.pub') }}"
  118. - name: update container user ca key
  119. shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "grep -F 'ssh-rsa {{ user_ca_key }}' /etc/ssh/user_ca.pub || echo 'ssh-rsa {{ user_ca_key }}' > /etc/ssh/user_ca.pub"
  120. register: update_user_ca_key
  121. changed_when: "update_user_ca_key.stdout != 'ssh-rsa {{ user_ca_key }}'"
  122. - name: trust user ca key
  123. shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "grep -F 'TrustedUserCAKeys /etc/ssh/user_ca.pub' /etc/ssh/sshd_config || echo 'TrustedUserCAKeys /etc/ssh/user_ca.pub' >> /etc/ssh/sshd_config"
  124. register: trust_ca_key
  125. changed_when: "trust_ca_key.stdout != 'TrustedUserCAKeys /etc/ssh/user_ca.pub'"
  126. - name: restart-container
  127. lxc_container:
  128. name: "{{ vm_name }}"
  129. state: restarted
  130. register: container_restart
  131. when: set_pub_key.changed or install_packages.changed or update_user_ca_key.changed or trust_ca_key.changed or container_network.changed or container_config.changed or container_dns_configuration.changed
  132. - name: "waiting for ssh on {{ vm_name }} vm to start"
  133. wait_for:
  134. host: "{{ hostvars[vm_name]['ansible_host'] }}"
  135. port: 22
  136. timeout: 30
  137. delegate_to: "{{ inventory_hostname }}"
  138. delegate_facts: True
  139. - pause: seconds=20
  140. when: container_restart.changed or container_running_state.changed