diff --git a/app/network/views.py b/app/network/views.py index 3a8aa6caaadcf60199763d1ef5a715dbcadcb67c..8909f560820cdea3f8f4a35f11c7e20096acf185 100644 --- a/app/network/views.py +++ b/app/network/views.py @@ -193,6 +193,14 @@ def view_host(name): task = utils.trigger_set_network_boot_profile( host, boot_profile=boot_profile ) + if boot_profile != "localboot": + # For localboot, there is no need to update the variable + # csentry_autoinstall_boot_profile is used for DHCP options + if utils.update_ansible_vars( + host, {"csentry_autoinstall_boot_profile": boot_profile} + ): + # If a change occured, force DHCP update + utils.trigger_core_services_update() db.session.commit() current_app.logger.info( f"Set network boot profile to {boot_profile} for {name} requested: task {task.id}" diff --git a/app/utils.py b/app/utils.py index c1f2c663ea1d6763b42674d3eefad39332a57c12..3fa9bbf5ede57c808d6b51d6d4aa6f2ef810a743 100644 --- a/app/utils.py +++ b/app/utils.py @@ -501,3 +501,24 @@ def retrieve_data_for_datatables(values, model): def minutes_ago(minutes): """Return the datetime x minutes ago""" return datetime.datetime.utcnow() - datetime.timedelta(minutes=minutes) + + +def update_ansible_vars(host, vars): + """Update the host ansible_vars + + Return False if no variables were changed, True otherwise + """ + if host.ansible_vars: + local_ansible_vars = host.ansible_vars.copy() + local_ansible_vars.update(vars) + if local_ansible_vars == host.ansible_vars: + # No change + return False + else: + host.ansible_vars.update(vars) + # If we don't flag the field as modified, it's not saved to the database + # Probably because we update an existing dictionary + sa.orm.attributes.flag_modified(host, "ansible_vars") + else: + host.ansible_vars = vars + return True