post_hooks and environement variables - how to run post_hooks only in certain cases

The problem I’m having

I am trying to run a dbt workflow with post_hooks. However, I’d like the workflow to sometimes run the post_hooks and sometimes skip them entirely.

The context of why I’m trying to do this

Basically need to not drop and unload a load of tables when I want to run data integrity tests.

What I’ve already tried

I have set up an environement variable in my workflow.yaml.

        env:
          run_hooks: true

and I then have the separate workflow where I have

        env:
          run_hooks: false

then in my models I have something like this:

        post_hook="{% if var('run_hooks', false) %}{{ '' }}{% else %}
        drop table if exists etc;
        unload ('select * from {{ this }}') TO 'etc'
        FORMAT PARQUET;
        {% endif %}
        ",

however, I have tried running it with the env_variables saying true and with them saying false and I am unable to make it skip the post_hooks either way.

Can someone shed some light on what is going on on this? Is there some different syntax I should be using to have this properly work?

Some example code or error messages

Put code inside backticks
  to preserve indentation
    which is especially important 
      for Python and YAML! 

@luigi_candela
Here is a suggestion for usage.

  1. To make it cleaner, adjust the indentation of the posthook for better readability.
{{
    config(
        materialized='table',
        alias='model',
        post_hook="
        {% if var('run_hooks', false) %}
                {{ '' }}
        {% else %}
            drop table if exists etc;
            unload ('select * from {{ this }}') TO 'etc' FORMAT PARQUET;
        {% endif %}
        "
        )
}}
  1. An alternative is to change the workflow to set the variable in the CLI command, e.g.
dbt run -s model --vars '{"run_hooks": "true"}'
or
dbt run -s model --vars '{"run_hooks": "false"}'

Which will solve it.