including a pre_tasks

Hello,

I want to set variables with set_fact in a pre_tasks section.
This is working well, but it is 50 lines long and I want to have these lines to be set in all my playbooks.

Is there a way to include a pre_tasks ? Here is what I would expect:

pre_tasks:

  • name: variables Instance 1 DV/QA
    set_fact:
    KARAFHTTPPORT: “210{{ Slice }}”
    KARAFSSHPORT: “211{{ Slice }}”
    KARAFRMIREGISTRYPORT: “212{{ Slice }}”
    KARAFRMISERVERPORT: “213{{ Slice }}”
    when: numint == ‘1’ and ( EnvType == ‘DV’ or EnvType == ‘QA’)

I would like to copy these lines in a setting.yml file, I don’t know where it would be conventional, lets say in vars directory.
And then call it this way:
pre_tasks:
vars/setting.yml

Would this work and what is the conventional way to do this in Ansible ?

Regards,

Hello,

I want to set variables with set_fact in a pre_tasks section.
This is working well, but it is 50 lines long and I want to have these
lines to be set in all my playbooks.

Is there a way to include a pre_tasks ? Here is what I would expect:

- include: <your.file>

https://docs.ansible.com/ansible/playbooks_roles.html#task-include-files-and-encouraging-reuse

  pre_tasks:
    - name: variables Instance 1 DV/QA
      set_fact:
        KARAFHTTPPORT: "210{{ Slice }}"
        KARAFSSHPORT: "211{{ Slice }}"
        KARAFRMIREGISTRYPORT: "212{{ Slice }}"
        KARAFRMISERVERPORT: "213{{ Slice }}"
      when: numint == '1' and ( EnvType == 'DV' or EnvType == 'QA')
...

I would like to copy these lines in a setting.yml file, I don't know where
it would be conventional, lets say in vars directory.

vars, settings, tasks... as long as you don't use any of the reserved directories you are good.

And then call it this way:
  pre_tasks:
       vars/setting.yml

   pre_tasks:
     - include: vars/settings.yml

Thanks a lot,
Regards