# How do you use a variable defined in \[\[\[environment\]\]\] in the \[\[\[directives\]\]\] section?

**URL:** <https://cylc.discourse.group/t/how-do-you-use-a-variable-defined-in-environment-in-the-directives-section/760>\
**Category:** Cylc Support\
**Created:** [September 14, 2023, 4:11am UTC](https://cylc.discourse.group/t/how-do-you-use-a-variable-defined-in-environment-in-the-directives-section/760 "2023-09-14T04:11:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonnyhtw](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/jonnyhtw/32/232_2.png) [@jonnyhtw](https://cylc.discourse.group/u/jonnyhtw)\
**Post date:** [September 14, 2023, 4:11am UTC](https://cylc.discourse.group/t/how-do-you-use-a-variable-defined-in-environment-in-the-directives-section/760/1 "2023-09-14T04:11:22Z")

</div>

hey,

i want to use a number from a variable in the `[[[directives]]]` section of a task family which is defined in `[[[environment]]]` section.

this doesn’t work…

![image](https://global.discourse-cdn.com/free1/uploads/cylc1/original/1X/cee946dc77f78d7eb5a9fbf68f56682af1b2094a.png)

and gives this error…

```auto
Jinja2Error: 'TOTAL_MPI_TASKS' is undefined
File /scale_wlg_persistent/filesets/home/williamsjh/roses/u-cy821/flow.cylc
              execution time limit = PT6M
          inherit = UMRUN_ENV
          [[[directives]]]
              --ntasks={{TOTAL_MPI_TASKS}} <-- UndefinedError

```

i tried removing the `{ ... }` but this then just gives this in the `job` script!..

`#SBATCH --ntasks=TOTAL_MPI_TASKS`

any tips?

thanks

---

<div class="post-metadata">

**Author:** ![hilary.j.oliver](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/hilary.j.oliver/32/4_2.png) [@hilary.j.oliver](https://cylc.discourse.group/u/hilary.j.oliver)\
**Post date:** [September 14, 2023, 4:17am UTC](https://cylc.discourse.group/t/how-do-you-use-a-variable-defined-in-environment-in-the-directives-section/760/2 "2023-09-14T04:17:22Z")

</div>

The problem is, your `TOTAL_MPI_TASKS` is not defined as a Jinaj2 variable, it’s just a plain Cylc config item. So Jinja2 does not know about it when you try to print it out for the `--ntasks` bit.

Just as assign it to a Jinja2 variable, and and have Jinja2 print it to the file in both places,e.g.:

```auto
{% set NTASKS = 36 }}
...
       --ntasks = {{NTASKS}}
...
       TOTAL_MPI_TASKS = {{NTASKS}}

```

(Remember Jinja2 is a template processor. All the Jinja2 code in your `flow.cylc` has to “process out” first thing, to generate the actual valid Cylc workflow configuration. )

---

<div class="post-metadata">

**Author:** ![jonnyhtw](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/jonnyhtw/32/232_2.png) [@jonnyhtw](https://cylc.discourse.group/u/jonnyhtw)\
**Post date:** [September 14, 2023, 4:23am UTC](https://cylc.discourse.group/t/how-do-you-use-a-variable-defined-in-environment-in-the-directives-section/760/3 "2023-09-14T04:23:57Z")

</div>

> [@hilary.j.oliver](#):
>
> ```auto
> {% set NTASKS = 36 }}
> 
> ```

aha got it, thanks! could i also do something like the `CORES_PER_NUMA_REGION` definition in this? this is from a different task family…

 ![image](https://global.discourse-cdn.com/free1/uploads/cylc1/original/1X/7c78c3131fcc9f65f4aa52c71165cf4601540f84.png)

---

<div class="post-metadata">

**Author:** ![hilary.j.oliver](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/hilary.j.oliver/32/4_2.png) [@hilary.j.oliver](https://cylc.discourse.group/u/hilary.j.oliver)\
**Post date:** [September 14, 2023, 4:26am UTC](https://cylc.discourse.group/t/how-do-you-use-a-variable-defined-in-environment-in-the-directives-section/760/4 "2023-09-14T04:26:19Z")

</div>

If I understand your question, yes.

You can use Jinja2 to programmatically generate **any** text and print it **anywhere** in the file - so long as the resulting file (with Jinja2 processed out) is valid Cylc syntax.

---

<div class="post-metadata">

**Author:** ![hilary.j.oliver](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/hilary.j.oliver/32/4_2.png) [@hilary.j.oliver](https://cylc.discourse.group/u/hilary.j.oliver)\
**Post date:** [September 14, 2023, 4:31am UTC](https://cylc.discourse.group/t/how-do-you-use-a-variable-defined-in-environment-in-the-directives-section/760/5 "2023-09-14T04:31:18Z")

</div>

Oh, hang on, I just saw that the value of CORES\_PER\_NUMA\_REGION is a shell expression.

This is very different to Jinja2 code. Jinja2 code gets processed at start-up, when the flow.cylc is parsed.

Shell expressions are not processed at all by Cylc. They are printed verbatim to the job file, to be evaluated in the job environment at job run time!

If the final values you need are available at start-up (when flow.cylc is parsed) it’s probably better to use Jinja2, then you don’t have to wait till that particular job runs to find out if you got it wrong because the job environment is not what you thought.

---

<div class="post-metadata">

**Author:** ![jonnyhtw](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/jonnyhtw/32/232_2.png) [@jonnyhtw](https://cylc.discourse.group/u/jonnyhtw)\
**Post date:** [September 14, 2023, 4:35am UTC](https://cylc.discourse.group/t/how-do-you-use-a-variable-defined-in-environment-in-the-directives-section/760/6 "2023-09-14T04:35:08Z")

</div>

ok cool, i think i get it! as this workflow progresses (it’s a beta climate LFRic one) there’ll be more stuff in the `rose-suite.conf` file to prevent having to do this anyway!

all the best,

j
