Skip to content

router_vm_name_to_action regex fix

During my testing, I came across a bug when trying to use the share range feature.

The user_set_defaults ansible var is set when the default section is written in the user's range config on range access operations

# range-access.yml:43
    - name: Determine if the user has set defaults
      ansible.builtin.set_fact:
        user_set_defaults: "{{ 'defaults:' in range_config_contents['content'] | b64decode | regex_replace('\\n', ' ') }}"

If a user's range has a defaults redefined but not the router variable, this conflicts with the next instructions. i.e. :

ludus:
 # inventory ...
defaults:
  stale_hours: 42 # redefine some default var

In the above configuration user_set_defaults is set to true even though the router variable is unchanged.

The Common section then skips the 1st ansible instruction and goes to the user defnied defaults

# range-access.yml:47
    - name: Set the router VM name from server defaults
      ansible.builtin.set_fact:
        router_vm_name_to_action: "{{ target_range_id }}-router-debian11-x64" # Must match the default in server-config.yml
      when: not user_set_defaults

    - name: Set the router VM name from user defined defaults
      ansible.builtin.set_fact:
        router_vm_name_to_action: "{{ (range_config_contents['content'] | b64decode | from_yaml).router.vm_name | default(range_id + '-router-debian11-x64') | regex_replace('{{\\s*range_id\\s*}}', target_range_id) }}" # I wish there was an 'eval' or 'render' filter
      when: user_set_defaults
  • This results in a cascade fail
  • fail of regex_replace since {{range_id}} can't be found in the user's config and the playbook errors out.
  • user_set_defaults var now equals default(range_id + '-router-debian11-x64' which is wrong since range_id is the admin user range_id running the command NOT the target user range_id

To fix this, the default is adjusted to correctly match the regex_replace (cf. changes) but I'm not sure this is the correct/clean way you wanna implement this.

Merge request reports

Loading