How to execute a playbook for a group of hosts with some tasks that have to be executed only once

530 views
Skip to first unread message

Tom Bartsch

unread,
Feb 6, 2017, 10:41:11 AM2/6/17
to Ansible Project
Hi there,

in my hosts file I have a group "product".
This group has 4 hosts: "product-et", "product-at", "product-qs" and "product-pr".

"product" has special properties which are valid for all 4 hosts.
Each of the 4 hosts has special properties which are valid only for this special host.

Now I want to write a playbook which generates jenkins-jobs.
I want to call this playbook with "hosts=product".

The playbook itself has to generate some jobs for the whole product. So each of this jobs should exist only once in Jenkins.
Futher this playbook has to generate some jobs for each of the 4 hosts. So each of this jobs should exist 4 times with only differences which depend on the host properties.

For each of these jobs the properties of the group and the properties of each of the hosts are necessary.

My problem now is that if I call this playbook with "hosts=product" it will generate each of the group jobs 4 times. So it's 3 times to much.

So now my question is how to realize this that I am able to have a mix of one time execution tasks (for the group) and tasks that have to be executed for each host?

Regards,
Tom

Johannes Kastl

unread,
Feb 6, 2017, 11:06:10 AM2/6/17
to ansible...@googlegroups.com
On 06.02.17 16:38 'Tom Bartsch' via Ansible Project wrote:

> My problem now is that if I call this playbook with "hosts=product"
> it will generate each of the group jobs 4 times. So it's 3 times to
> much.

Two ideas:

You can have more than one play in a playbook.

###############
- hosts: just_one_host
tasks:
- name: Add group job
...

- hosts: product
tasks:
- name: Add jobs per host
...
###############

Calling this via "ansible-playbook foobar.yml" executes first the task
on only one machine, creating the group job, and then runs the tasks
on all machines.

The other idea is to make your task see, if the group job has been
defined before. I do not know (and you did not tell) how you create
your jobs, but maybe you can check for existence of the jobs and only
create them, if necessary.

Johannes

signature.asc

Kai Stian Olstad

unread,
Feb 6, 2017, 3:39:18 PM2/6/17
to ansible...@googlegroups.com
I might have misunderstood your, but I believe you are looking for run_once.

https://docs.ansible.com/ansible/playbooks_delegation.html#run-once

With run_once on a task, the task is only executed on one of the hosts
in the play.

--
Kai Stian Olstad

Tom Bartsch

unread,
Feb 7, 2017, 3:27:32 AM2/7/17
to Ansible Project
Hi,

thx for the hints, Mr. Kastl and Mr. Olstad.

The hints from Mr. Kastl are not suitable for my purpose because I'm calling the playbook only with the product name and only one time.
Also I want to create the jobs don't matter if they are exist or not. The reason is that it's possible that I changed something in the config.xml of an maybe existing job. So if I call the playbook I want it to change also the existing jobs to deliver my changes to jenkins.

So it seems that the hint from Mr. Olstad hits the bull's eye. I tested the behavior with run_once and it looks fine.
Thank you once more Mr. Kastl and Mr. Olstad.

Regards,
Tom

Johannes Kastl

unread,
Feb 7, 2017, 5:11:40 AM2/7/17
to ansible...@googlegroups.com
Hi,

Glad the run_once works for you.

On 07.02.17 09:27 'Tom Bartsch' via Ansible Project wrote:

> The hints from Mr. Kastl are not suitable for my purpose because
> I'm calling the playbook only with the product name and only one
> time.

But I am not sure you got my approach. You do not need to call the
playbook multiple times, even if it contains two plays.

> Also I want to create the jobs don't matter if they are exist
> or not. The reason is that it's possible that I changed something
> in the config.xml of an maybe existing job. So if I call the
> playbook I want it to change also the existing jobs to deliver my
> changes to jenkins.

But doesn't this mean that the jobs exist multiple times in jenkins,
with different parameters?

Johannes

signature.asc
Reply all
Reply to author
Forward
0 new messages