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.

129 lines
5.4 KiB

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