ansible-roles/lxc/tasks/create-container.yml
David Prevot fc692cf65b
All checks were successful
gitea/ansible-roles/pipeline/head This commit looks good
Allow more --check runs
Use “when: not ansible_check_mode” or “when <file>.stat.exists or not
ansible_check_mode” in order to provide a meaningful diff if possible.

This is an improvement from the previously reverted commit
1728eaee68.
2022-12-21 18:05:41 +01:00

80 lines
2.4 KiB
YAML

---
- name: Is lxc installed?
stat:
path: /usr/bin/lxc-ls
register: _lxc_ls
- name: "Check if container {{ name }} exists"
command: "lxc-ls {{ name }}"
changed_when: false
check_mode: no
register: container_exists
when: _lxc_ls.stat.exists or not ansible_check_mode
- name: "Create container {{ name }}"
lxc_container:
name: "{{ name }}"
container_log: true
template: debian
state: stopped
template_options: "--arch amd64 --release {{ release }}"
when:
- container_exists.stdout_lines | length == 0
- _lxc_container.stat.exists or not ansible_check_mode
- name: "Is container {{ name }} created?"
stat:
path: "/var/lib/lxc/{{ name }}"
register: _lxc_container
- name: "Disable network configuration inside container {{ name }}"
replace:
name: "/var/lib/lxc/{{ name }}/rootfs/etc/default/networking"
regexp: "^#CONFIGURE_INTERFACES=yes"
replace: CONFIGURE_INTERFACES=no
when:
- lxc_network_type == "none"
- _lxc_container.stat.exists or not ansible_check_mode
- name: "Disable interface shut down on halt inside container {{ name }} (Jessie container)"
lineinfile:
name: "/var/lib/lxc/{{ name }}/rootfs/etc/default/halt"
line: "NETDOWN=no"
when:
- lxc_network_type == "none" and release == "jessie"
- _lxc_container.stat.exists or not ansible_check_mode
- name: "Make the container {{ name }} poweroff on SIGPWR sent by lxc-stop (Jessie container)"
file:
src: /lib/systemd/system/poweroff.target
dest: "/var/lib/lxc/{{ name }}/rootfs/etc/systemd/system/sigpwr.target"
state: link
when: release == 'jessie'
- name: "Configure the DNS resolvers in the container {{ name }}"
copy:
remote_src: yes
src: /etc/resolv.conf
dest: "/var/lib/lxc/{{ name }}/rootfs/etc/"
- name: "Add hostname in /etc/hosts for container {{ name }}"
lineinfile:
name: "/var/lib/lxc/{{ name }}/rootfs/etc/hosts"
line: "127.0.0.1 {{ name }}"
when: _lxc_container.stat.exists or not ansible_check_mode
- name: "Fix permission on /dev for container {{ name }}"
lineinfile:
name: "/var/lib/lxc/{{ name }}/rootfs/etc/rc.local"
line: "chmod 755 /dev"
insertbefore: "^exit 0$"
when:
- release == 'jessie'
- _lxc_container.stat.exists or not ansible_check_mode
- name: "Ensure that {{ name }} container is running"
lxc_container:
name: "{{ name }}"
state: started