# including a pre\_tasks

**URL:** <https://forum.ansible.com/t/including-a-pre-tasks/22443>\
**Category:** Ansible Project\
**Created:** [September 1, 2016, 9:00am UTC](https://forum.ansible.com/t/including-a-pre-tasks/22443 "2016-09-01T09:00:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [September 1, 2016, 9:00am UTC](https://forum.ansible.com/t/including-a-pre-tasks/22443/1 "2016-09-01T09:00:08Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 1, 2016, 11:07am UTC](https://forum.ansible.com/t/including-a-pre-tasks/22443/2 "2016-09-01T11:07:22Z")

</div>

> 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](https://docs.ansible.com/ansible/playbooks_roles.html#task-include-files-and-encouraging-reuse)

> &nbsp;&nbsp;pre\_tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: variables Instance 1 DV/QA  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;KARAFHTTPPORT: "210{{ Slice }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;KARAFSSHPORT: "211{{ Slice }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;KARAFRMIREGISTRYPORT: "212{{ Slice }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;KARAFRMISERVERPORT: "213{{ Slice }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:  
> &nbsp;&nbsp;pre\_tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars/setting.yml

&nbsp;&nbsp;&nbsp;pre\_tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- include: vars/settings.yml

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [September 5, 2016, 8:18am UTC](https://forum.ansible.com/t/including-a-pre-tasks/22443/3 "2016-09-05T08:18:28Z")

</div>

Thanks a lot,  
Regards
