Modifying scripts not to use jinja conditionals in when's

This commit is contained in:
László Károlyi 2017-04-22 14:27:37 +02:00
parent c5ffa65ca4
commit 4c8c9a2aee
No known key found for this signature in database
GPG key ID: 372AA22D38BD5B13
5 changed files with 32 additions and 20 deletions

View file

@ -11,7 +11,7 @@
- name: Set other variables for the create
set_fact:
jail_{{ jail_name }}_is_new_created: true
jail_THIS_is_new_created: true
jail_{{ jail_name }}_new_dataset_name:
'{{ dynamic_jails_dataset_name }}/{{ vars["jail_" + jail_name + "_newest_id"] }}'
jail_{{ jail_name }}_new_path:

View file

@ -9,9 +9,17 @@
- name: Set default facts for the task
set_fact:
jail_{{ jail_name }}_is_new_created: false
jail_THIS_is_new_created: false
jail_{{ jail_name }}_existing_dirs: '{{ dirs_found.files | map(attribute="path") | map("basename") | sort(reverse=True) }}'
- name: Setting other defaults for this task depending on formerly set variables
set_fact:
jail_THIS_run_create: >-
{{
vars["jail_" + jail_name + "_existing_dirs"] | count == 0 or
vars["jail_" + jail_name + "_force_recreate"]
}}
- name: Ensure config directory exists for the generated jails
file:
path: '{{ dynamic_jails_path }}/configs'
@ -39,33 +47,29 @@
vars:
include_type: '{{ "jail_" + jail_name + "_include_createnew_poststart" }}'
when: >-
{{
vars["jail_" + jail_name + "_existing_dirs"] | count == 0 or
vars["jail_" + jail_name + "_force_recreate"]
}}
when: jail_THIS_run_create
# This task here is to set a boolean to this 'level', since the jail_{{ jail_name }}_is_new_created is used elsewhere too.
# This task here is to set a boolean to this 'level', since the jail_THIS_is_new_created is used elsewhere too.
# When we change that variable elsewhere downwards, the tasks could get skipped because the 'when' statements
# are dynamically evaluated.
- name: Set if the {{ jail_name }} jail has to be updated or a new has been created.
set_fact:
jail_{{ jail_name }}_run_update: '{{ not vars["jail_" + jail_name + "_is_new_created"] }}'
jail_THIS_run_update: '{{ not jail_THIS_is_new_created }}'
# ---
# Run UPDATE when not created
# ---
- name: Checking the latest {{ jail_name }} jail, updating/replacing when necessary
include: '{{ ansible_roles_path }}/karolyi.ansible-freebsd-jailhost-tools/tasks/jail/update.yml'
when: '{{ vars["jail_" + jail_name + "_run_update"] }}'
when: jail_THIS_run_update
# Remove the latest directory for tidyup
- name: Updating jail_{{ jail_name }}_existing_dirs when necessary
set_fact:
jail_{{ jail_name }}_existing_dirs: '{{ vars["jail_" + jail_name + "_existing_dirs"][1:] }}'
when: '{{ not vars["jail_" + jail_name + "_is_new_created"] }}'
when: not jail_THIS_is_new_created
- name: Tidying up remaining unused jails
include: '{{ ansible_roles_path }}/karolyi.ansible-freebsd-jailhost-tools/tasks/jail/teardown.yml jail_id={{ item }}'

View file

@ -1,7 +1,11 @@
- 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_id|default(False) == False }}'
when: jail_is_no_jail_id
- name: Check if the jail {{ jail_id }} runs
set_fact:
@ -29,11 +33,15 @@
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 != dynamic_jails_path + "/" + jail_id }}'
when: mountpoint != _iter_mount_point
with_items: '{{ mounts_under_jail.stdout_lines|sort(reverse=True) }}'
loop_control:
loop_var: mountpoint
@ -57,7 +65,7 @@
command:
/sbin/zfs destroy -f
{{ dynamic_jails_dataset_name }}/{{ jail_id }}
when: zfs_destroy | failed
when: 'zfs_destroy | failed and ": Device busy" in zfs_destroy.msg'
- name: Removing jail directory
file:

View file

@ -1,6 +1,6 @@
- name: Looking up if the {{ jail_name }} jail needs to be replaced, setting up variables
set_fact:
jail_{{ jail_name }}_must_replace: >-
jail_THIS_must_replace: >-
{{
lookup(
'pipe', '/usr/sbin/chroot ' +
@ -39,7 +39,7 @@
upgrade -y
when: '{{ vars["jail_" + jail_name + "_pkgs"] | changed }}'
when: '{{ not vars["jail_" + jail_name + "_must_replace"] }}'
when: jail_THIS_must_replace
@ -66,7 +66,7 @@
vars:
include_type: '{{ "jail_" + jail_name + "_include_update_poststart" }}'
when: '{{ vars["jail_" + jail_name + "_must_replace"] }}'
when: jail_THIS_must_replace

View file

@ -4,7 +4,7 @@
command: >-
/bin/sh -c "/usr/bin/grep -lE
'^[0-9]+.[0-9]+.[0-9]+.[0-9]+ jail_{{ jail_name }}'
/usr/jails/*/etc/hosts {{ dynamic_jails_path }}/*/etc/hosts"
/etc/hosts /usr/jails/*/etc/hosts {{ dynamic_jails_path }}/*/etc/hosts"
ignore_errors: yes
failed_when: false
register: matching_hosts_files
@ -19,7 +19,7 @@
loop_control:
loop_var: filename
- name: Sending HUP to any dnsmasq process to reload their /etc/hosts
- name: Sending HUP to any any crucial processes to reload their /etc/hosts
command:
/usr/bin/killall -HUP dnsmasq
/usr/bin/killall -HUP dnsmasq syslogd
failed_when: false