including a pre_tasks

31 views
Skip to first unread message

fanvalt

unread,
Sep 1, 2016, 5:00:08 AM9/1/16
to Ansible Project
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,

Kai Stian Olstad

unread,
Sep 1, 2016, 7:07:35 AM9/1/16
to ansible...@googlegroups.com
On 01. sep. 2016 11:00, fanvalt wrote:
> 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

--
Kai Stian Olstad

fanvalt

unread,
Sep 5, 2016, 4:18:28 AM9/5/16
to Ansible Project, ansible-pr...@olstad.com
Thanks a lot,
Regards
Reply all
Reply to author
Forward
0 new messages