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.

93 lines
3.0 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 [ 'wheezy', 'jessie', 'stretch', 'sid', ]
  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. - name: deploy container config
  32. template:
  33. src: config.j2
  34. dest: "/var/lib/lxc/{{ vm_name }}/config"
  35. - name: start container
  36. lxc_container:
  37. name: "{{ vm_name }}"
  38. state: started
  39. when: auto_start|bool
  40. when: not (container_exists.exists and lxc_existance.stat.isdir)
  41. - name: update container config
  42. template:
  43. src: config.j2
  44. dest: "/var/lib/lxc/{{ vm_name }}/config"
  45. register: container_config
  46. notify: restart container
  47. - name: set container running state
  48. lxc_container:
  49. name: "{{ vm_name }}"
  50. state: "{{ container_state }}"
  51. register: container_running_state
  52. - name: Read container DNS configuration
  53. container_file_read:
  54. name: "{{ vm_name }}"
  55. path: /etc/resolv.conf
  56. register: vm_resolv_conf
  57. - debug:
  58. var: vm_resolv_conf
  59. verbosity: 2
  60. - name: update container DNS configuration
  61. 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"
  62. register: container_dns_configuration
  63. changed_when: "container_dns_configuration.stdout != 'nameserver {{ hostvars[ext_gateway].ansible_host }}'"
  64. - name: update container network configuration
  65. 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"
  66. register: container_network
  67. changed_when: "container_network.stdout != 'iface eth0 inet manual'"
  68. notify: restart container
  69. - name: install packages
  70. shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "apt-get update && apt-get install python ssh -y"
  71. register: install_packages
  72. changed_when: "install_packages.stdout.find('0 newly installed') == -1"
  73. notify: restart container
  74. # Restart container when one in
  75. # - container_dns_configuration
  76. # - network conf has changed
  77. # - install_packages
  78. # - container_network
  79. # is changed by executing handlers now
  80. - meta: flush_handlers