ansible-freebsd-jailhost-tools/tasks/jail/teardown.yml

75 lines
2.2 KiB
YAML

- name: Setting default facts check for teardown
set_fact:
jail_is_no_jail_id: '{{ jail_id|default(False) == False }}'
- name: Fail when jail_id not specified
fail:
msg: jail_id must be defined for teardown.yml, bailing out
when: jail_is_no_jail_id
- name: Check if the jail {{ jail_id }} runs
set_fact:
jail_is_old_running: >-
{{ jail_id in running_jail_ids }}
- name: Terminating jail when running
command:
/usr/sbin/jail -r {{ jail_id }}
when: jail_is_old_running
- name: Removing config block for {{ jail_id }} from /etc/jail.conf
blockinfile:
state: absent
dest: '/etc/jail.conf'
# Keep this line at the bottom so vim can use proper syntax higlighting
insertbefore: '^# vim: syn=conf$'
marker: '# {mark} ANSIBLE MANAGED BLOCK: {{ jail_id }}'
- name: Getting mounts under this jail
shell:
/sbin/mount -p
| grep '{{ dynamic_jails_path }}/{{ jail_id }}'
| awk '{print $2}'
warn=no
register: mounts_under_jail
- name: Setting this jail's root mount point
set_fact:
_iter_mount_point: '{{ dynamic_jails_path + "/" + jail_id }}'
- name: Unmounting all mounts from under the jail so it could be destroyed
command:
/sbin/umount
'{{ mountpoint }}'
when: mountpoint != _iter_mount_point
with_items: '{{ mounts_under_jail.stdout_lines|sort(reverse=True) }}'
loop_control:
loop_var: mountpoint
- name: Delete config dir for jail
file:
path: '{{ dynamic_jails_path }}/configs/{{ jail_id }}'
state: absent
- name: Destroying ZFS dataset {{ dynamic_jails_dataset_name }}/{{ jail_id }}
zfs:
name: >-
{{ dynamic_jails_dataset_name }}/{{ jail_id }}
state: absent
register: zfs_destroy
ignore_errors: yes
# Due to a possible bug in the ZFS/FreeBSD kernel, destroying the dataset
# sometimes isn't possible after freeing it up. Hence we use '-f'
- name: Force-destroying ZFS dataset {{ dynamic_jails_dataset_name }}/{{ jail_id }} because it failed formerly
command:
/sbin/zfs destroy -f
{{ dynamic_jails_dataset_name }}/{{ jail_id }}
when: 'zfs_destroy | failed and ": Device busy" in zfs_destroy.msg'
- name: Removing jail directory
file:
path: >-
{{ dynamic_jails_path }}/{{ jail_id }}
state: absent