Multiple !include - is it possible?

How does !include actually work? Does it literally replace itself with the specified file?

Is there a syntax that would make something like this valid?

  - service: script.say
    data_template:
      message: >
        !include ../good_morning.yaml
        !include ../good_bye.yaml

where good_morning.yaml and good_bye.yaml are both macros e.g.

      {%- macro bye() -%}
        {{ "Bye bye! " ,
           "See you later! ",
           "cheerio! ",
           "have a good day! ",
           "thank you for listening, I'll be back tomorrow! ",
           "Have a nice day! "] | random }}
      {%- endmacro -%}

      {{- bye() -}}

It replaces the line with the contents of the include (albeit the replacement may be multiple lines), BUT you can only replace that line and only use one include per line.

So, in your example you’re actually saying

  - service: script.say
    data_template:
      message: !include ../good_morning.yaml !include ../good_bye.yaml

Which is two includes in one line, the only valid way would be something like:

  - service: script.say
    data_template:
      message: !include ../good_morning.yaml
  - service: script.say
    data_template:
      message: !include ../good_bye.yaml

Hope that makes sense.

4 Likes

Thanks, yes it makes perfect sense and if I’m honest I was expecting it as I am sure I’d have seen others doing this if it were possible.

1 Like