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

65 lines
2 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
when: not ansible_check_mode
2017-08-22 17:32:32 +02:00
- 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
when: not ansible_check_mode
- 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"
when: not ansible_check_mode
2017-08-22 17:32:32 +02:00
- 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"
when: not ansible_check_mode
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 }}"
when: not ansible_check_mode
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'
when: not ansible_check_mode
2017-08-22 17:32:32 +02:00
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