ansible-roles/lxc/tasks/create-container.yml

75 lines
2.3 KiB
YAML
Raw Normal View History

2017-08-22 17:32:32 +02:00
---
- name: "Check if container {{ name }} exists"
2020-04-17 15:56:54 +02:00
command: "lxc-ls {{ name }}"
changed_when: false
check_mode: no
2017-08-22 17:32:32 +02:00
register: container_exists
- name: "Create container {{ name }}"
lxc_container:
2020-04-17 15:56:54 +02:00
name: "{{ name }}"
container_log: true
template: debian
state: stopped
2020-04-17 15:56:54 +02:00
template_options: "--arch amd64 --release {{ release }}"
when: container_exists.stdout_lines | length == 0
- name: "Disable network configuration inside container {{ name }}"
2017-08-22 17:32:32 +02:00
replace:
2020-04-17 15:56:54 +02:00
name: "/var/lib/lxc/{{ name }}/rootfs/etc/default/networking"
2017-08-22 17:32:32 +02:00
regexp: "^#CONFIGURE_INTERFACES=yes"
replace: CONFIGURE_INTERFACES=no
when: lxc_network_type == "none"
- name: "Disable interface shut down on halt inside container {{ name }} (Jessie container)"
2017-08-22 17:32:32 +02:00
lineinfile:
2020-04-17 15:56:54 +02:00
name: "/var/lib/lxc/{{ name }}/rootfs/etc/default/halt"
2017-08-22 17:32:32 +02:00
line: "NETDOWN=no"
when: lxc_network_type == "none" and release == "jessie"
2017-08-22 17:32:32 +02:00
- name: "Make the container {{ name }} poweroff on SIGPWR sent by lxc-stop (Jessie container)"
2017-08-22 17:32:32 +02:00
file:
src: /lib/systemd/system/poweroff.target
2020-04-17 15:56:54 +02:00
dest: "/var/lib/lxc/{{ name }}/rootfs/etc/systemd/system/sigpwr.target"
2017-08-22 17:32:32 +02:00
state: link
when: release == 'jessie'
- name: "Configure the DNS resolvers in the container {{ name }}"
copy:
remote_src: yes
src: /etc/resolv.conf
2020-04-17 15:56:54 +02:00
dest: "/var/lib/lxc/{{ name }}/rootfs/etc/"
2017-08-22 17:32:32 +02:00
- name: "Add hostname in /etc/hosts for container {{ name }}"
2017-08-22 17:32:32 +02:00
lineinfile:
2020-04-17 15:56:54 +02:00
name: "/var/lib/lxc/{{ name }}/rootfs/etc/hosts"
line: "127.0.0.1 {{ name }}"
2017-08-22 17:32:32 +02:00
- name: "Fix permission on /dev for container {{ name }}"
2017-08-22 17:32:32 +02:00
lineinfile:
2020-04-17 15:56:54 +02:00
name: "/var/lib/lxc/{{ name }}/rootfs/etc/rc.local"
2017-08-22 17:32:32 +02:00
line: "chmod 755 /dev"
insertbefore: "^exit 0$"
when: release == 'jessie'
2017-08-22 17:32:32 +02:00
2021-08-05 10:55:41 +02:00
# For some reason import_role/include_role doesn't work here.
# Apparently ansible see some condition that end up being false.
# So we use import_tasks and a symlink.
- name: "Put /etc of container {{ name }} into git"
import_tasks: repository.yml
vars:
repository_path: "/var/lib/lxc/{{ name }}/rootfs/etc"
gitignore_items:
- "aliases.db"
- "*.swp"
- "postfix/sa-blacklist.access"
- "postfix/*.db"
- "postfix/spamd.cidr"
- "evobackup/.keep-*"
- "letsencrypt/.certbot.lock"
2020-04-17 15:56:54 +02:00
- name: "Ensure that {{ name }} container is running"
lxc_container:
2020-04-17 15:56:54 +02:00
name: "{{ name }}"
state: started