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.

134 lines
4.3 KiB

  1. ---
  2. - name: 'check | container folder exists'
  3. stat:
  4. path: '/var/lib/lxc/{{ vm_name }}'
  5. register: container_dir
  6. - name: 'check | container exists'
  7. container_exists:
  8. name: '{{ vm_name }}'
  9. register: container_exists
  10. - name: 'check | distro is supported'
  11. assert:
  12. that: distro in [ 'debian', 'alpine' ]
  13. - name: 'check | Debian | release is supported'
  14. assert:
  15. that: release in [ 'bullseye', 'sid', 'buster' ]
  16. msg: 'release {{ release }} not supported by debian template'
  17. when: distro == 'debian'
  18. - block:
  19. - name: 'create | Debian | Privileged Container'
  20. lxc_container:
  21. name: '{{ vm_name }}'
  22. backing_store: 'lvm'
  23. fs_size: '{{ vm_size }}'
  24. vg_name: '{{ vg_name }}'
  25. lv_name: 'vm_{{ vm_name }}'
  26. fs_type: 'xfs'
  27. container_log: true
  28. template: 'debian'
  29. template_options: '--release {{ distro }} --packages=ssh,python3,python3-apt'
  30. state: 'stopped'
  31. # suppress messages related to file descriptors
  32. # leaking when lvm is invoked
  33. environment:
  34. LVM_SUPPRESS_FD_WARNINGS: 1
  35. when: (not unprivileged) and distro == 'debian'
  36. - name: 'pre-create | Unprivileged Container | Subxid Script'
  37. copy:
  38. src: 'find_subxid.sh'
  39. dest: 'find_subxid.sh'
  40. when: unprivileged
  41. - name: 'pre-create | Unprivileged Container | Find Subxid'
  42. command: 'bash find_subxid.sh'
  43. register: avail_subxid
  44. when: unprivileged
  45. - name: 'pre-create | Unprivileged Container | Set Subxid'
  46. set_fact:
  47. subuidmap: '{{ avail_subxid.stdout_lines[0] }}'
  48. subgidmap: '{{ avail_subxid.stdout_lines[1] }}'
  49. when: unprivileged
  50. - name: 'pre-create | Unprivileged Container | Allocate Subxid'
  51. command: >-
  52. usermod
  53. -v {{ '{}-{}'.format(subuidmap.split(' ')[0],
  54. subuidmap.split(' ')[0]|int+subuidmap.split(' ')[1]|int-1) }}
  55. -w {{ '{}-{}'.format(subgidmap.split(' ')[0],
  56. subgidmap.split(' ')[0]|int+subgidmap.split(' ')[1]|int-1) }}
  57. root
  58. - name: 'pre-create | Unprivileged Container | Create config stub'
  59. copy:
  60. content: |
  61. lxc.idmap = u 0 {{ subuidmap }}
  62. lxc.idmap = g 0 {{ subgidmap }}
  63. dest: '/tmp/lxc_unpriv_config'
  64. when: unprivileged
  65. - name: 'create | Unprivileged Container'
  66. lxc_container:
  67. name: '{{ vm_name }}'
  68. backing_store: 'lvm'
  69. fs_type: 'xfs'
  70. fs_size: '{{ vm_size }}'
  71. vg_name: '{{ vg_name }}'
  72. lv_name: 'vm_{{ vm_name }}'
  73. container_log: true
  74. template: 'download'
  75. template_options: '-d {{ distro }} -r {{ release }} -a amd64'
  76. config: '/tmp/lxc_unpriv_config'
  77. state: 'stopped'
  78. when: unprivileged
  79. - name: 'post-create | LXC Container Configuration'
  80. template:
  81. src: 'config.j2'
  82. dest: '/var/lib/lxc/{{ vm_name }}/config'
  83. - block:
  84. - name: 'post-create | Alpine | Force restart'
  85. lxc_container:
  86. name: '{{ vm_name }}'
  87. state: 'restarted'
  88. - name: 'post-create | Alpine | Guest Network Configuration'
  89. raw: |
  90. rm /etc/network/interfaces
  91. echo 'nameserver {{ hostvars | ip_from_inventory('vm_gateway') }}' > /etc/resolv.conf
  92. delegate_to: '{{ vm_name }}'
  93. connection: 'ssh_lxc'
  94. - name: 'post-create | Alpine | Force restart'
  95. lxc_container:
  96. name: '{{ vm_name }}'
  97. state: 'restarted'
  98. - name: 'post-create | Alpine | Install Python'
  99. raw: |
  100. apk update
  101. apk upgrade
  102. apk add python3
  103. delegate_to: '{{ vm_name }}'
  104. connection: 'ssh_lxc'
  105. when: distro == 'alpine'
  106. - name: 'post-create | Debian | Guest Initial Configuration'
  107. lxc_container:
  108. name: '{{ vm_name }}'
  109. container_command: |
  110. echo 'nameserver {{ hostvars | ip_from_inventory('vm_gateway') }}' > /etc/resolv.conf
  111. apt update
  112. apt install -y python3 python3-apt
  113. systemctl mask systemd-journald-audit.socket
  114. state: 'stopped'
  115. - name: 'post-create | Start container'
  116. lxc_container:
  117. name: '{{ vm_name }}'
  118. state: 'started'
  119. when: auto_start|bool
  120. when: not (container_exists.exists and container_dir.stat.isdir)