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.

100 lines
3.3 KiB

  1. ---
  2. - name: check for lxc container dir
  3. stat:
  4. path: '/var/lib/lxc/{{ vm_name }}'
  5. register: lxc_existance
  6. - name: check for lxc container existance
  7. container_exists:
  8. name: "{{ vm_name }}"
  9. register: container_exists
  10. - name: Check debian release
  11. assert:
  12. that: distro in [ 'stretch', 'sid', 'buster' ]
  13. msg: "release {{ distro }} not supported by debian template"
  14. - block:
  15. - name: create the lxc container
  16. lxc_container:
  17. name: "{{ vm_name }}"
  18. backing_store: lvm
  19. fs_size: "{{ vm_size }}"
  20. vg_name: "{{ inventory_hostname }}vg"
  21. lv_name: "vm_{{ vm_name }}"
  22. fs_type: xfs
  23. container_log: true
  24. template: debian
  25. template_options: --release {{ distro }} --packages=ssh,python
  26. state: stopped
  27. # suppress messages related to file descriptors
  28. # leaking when lvm is invoked
  29. environment:
  30. LVM_SUPPRESS_FD_WARNINGS: 1
  31. MIRROR: http://mi.mirror.garr.it/mirrors/debian/
  32. - name: deploy container config
  33. template:
  34. src: config.j2
  35. dest: "/var/lib/lxc/{{ vm_name }}/config"
  36. - name: start container
  37. lxc_container:
  38. name: "{{ vm_name }}"
  39. state: started
  40. when: auto_start|bool
  41. when: not (container_exists.exists and lxc_existance.stat.isdir)
  42. - name: update container config
  43. template:
  44. src: config.j2
  45. dest: "/var/lib/lxc/{{ vm_name }}/config"
  46. register: container_config
  47. notify: restart container
  48. - name: set container running state
  49. lxc_container:
  50. name: "{{ vm_name }}"
  51. state: "{{ container_state }}"
  52. register: container_running_state
  53. - name: Read container DNS configuration
  54. container_file_read:
  55. name: "{{ vm_name }}"
  56. path: /etc/resolv.conf
  57. register: vm_resolv_conf
  58. - debug:
  59. var: vm_resolv_conf
  60. verbosity: 2
  61. - name: update container DNS configuration
  62. shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "grep -Pz1 'domain lilik.it\nnameserver {{ hostvars | ip_from_inventory('vm_gateway') }}' /etc/resolv.conf || echo -e 'domain lilik.it\nnameserver {{ hostvars | ip_from_inventory('vm_gateway') }}' > /etc/resolv.conf"
  63. register: container_dns_configuration
  64. changed_when: container_dns_configuration.stdout != "domain lilik.it\nnameserver {{ hostvars | ip_from_inventory('vm_gateway') }}\n\u0000"
  65. - name: update container network configuration
  66. 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"
  67. register: container_network
  68. changed_when: "container_network.stdout != 'iface eth0 inet manual'"
  69. notify: restart container
  70. - name: install packages
  71. shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "apt-get update && apt-get install python ssh -y"
  72. register: install_packages
  73. changed_when: "install_packages.stdout.find('0 newly installed') == -1"
  74. notify: restart container
  75. # Restart container when one in
  76. # - container_dns_configuration
  77. # - network conf has changed
  78. # - install_packages
  79. # - container_network
  80. # is changed by executing handlers now
  81. - meta: flush_handlers
  82. - name: add monitoring facts
  83. set_fact:
  84. monitoring_host: "{{monitoring_host| default([]) }} + [ '{{ vm_name }}' ]"
  85. delegate_facts: True
  86. delegate_to: status