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

68 lines
2 KiB
YAML
Raw Normal View History

- name: Fail when jail_id not specified
fail:
msg: jail_id must be defined for teardown.yml, bailing out
when: '{{ jail_id|default(False) == False }}'
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: Unmounting all mounts from under the jail so it could be destroyed
command:
/sbin/umount
'{{ mountpoint }}'
when: '{{ mountpoint != dynamic_jails_path + "/" + jail_id }}'
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 }}
when: zfs_destroy | failed
2017-03-09 17:17:13 +01:00
- name: Removing jail directory
file:
path: >-
{{ dynamic_jails_path }}/{{ jail_id }}
state: absent