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

76 lines
2.2 KiB
YAML
Raw Normal View History

- 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
2017-03-09 17:17:13 +01:00
- 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 }}'
2017-03-09 17:17:13 +01:00
- 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:
2018-10-03 19:00:29 +02:00
_root_mount_point: '{{ dynamic_jails_path + "/" + jail_id }}'
2017-03-09 17:17:13 +01:00
- name: Unmounting all mounts from under the jail so it could be destroyed
command:
/sbin/umount
'{{ mountpoint }}'
2018-10-03 19:00:29 +02:00
when: mountpoint != _root_mount_point
2017-03-09 17:17:13 +01:00
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 }}
2017-03-09 17:17:13 +01:00
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 }}
2018-10-03 19:00:29 +02:00
when: 'zfs_destroy is failed and ": Device busy" in zfs_destroy.msg'
2017-03-09 17:17:13 +01:00
- name: Removing jail directory
file:
path: >-
{{ dynamic_jails_path }}/{{ jail_id }}
state: absent