execute a task or playbook once per play?

I’m trying to figure out a way of checking out a git repository only once per play on the management host. I’ve solved this by registering an variable when I checked it out in normal playbooks, but it wont work if I make a “playbook of playbooks”.

Has anyone an idea of how to solve this?

Hosts: localhost, in the checkout play, is how I do this.

Brian Coca

But then you can’t run the separate playbooks without modifying them to include the git-checkout playbook?

I’m aiming of having a subset of playbooks that you can run independently, as well as being able to run them in a playbook-of-playbooks.

i.e.:
playbooks-all.yml:

  • include: a.yml
  • include: b.yml

a.yml:

  • include: git_checkout.yml

b.yml:

  • include: git_checkout.yml

Another option is to use creates=/gitrepo in previous task that sets a variable and then execute the git action when_set.

The git module is already smart, and won't check things out again if
it doesn't need to.

Just pass in the tag you should be checked out at.

The issue with doing it the simple way is that I would have to run everything in serial mode, otherwise git will find a .lock file which breaks the run.

Having trouble understanding.

Why are you talking to multiple copies of the same host at the same time?

Ansible playbooks don't execute more than one step on the same host
simultaneously.

Currently I use git module as a task and delegate_to, as the repository is located on the management node and I don’t want to checkout the repository on 17 nodes to only use a few of the files, so what I did was to register a checkout in a variable and check the variable every time the task runs to avoid doing it twice. However, this only seems to work in a normal playbooks and not in a master playbook.

What I was aiming for was to be able to run each playbook by itself as well as using them as building blocks to make “role” playbooks depending on what the server would do (i.e. infrastructure/customer server/monitoring/db server etc).

In this case, don’t use delegate to.

Have a short play that only targets localhost.

And then have a play following that calls local_action: rsync (or copy)

I will try that and see how it goes, thanks.

How is localhost intended to work with the cobbler hosts-script as it says “Make sure a cobbler --dns-name is set for each cobbler system”?
Sounds dangerous to have localhost in the DNS.

Issue is that i get “no hosts matched” for both localhost and 127.0.0.1.
Having localhost in DNS seems to be fine (RFC 1912):

"

The "localhost" address is a "special" address which always refers to
   the local host.  It should contain the following line:

           localhost.      IN      A       127.0.0.1"

Question is to handle it in cobbler to avoid pitfalls.

... digging ... 

You can in 1.1, use multiple inventory sources at once:

assume /path/to/inventory_directory contains:
   a host file defining your localhost (this can be named anything)
   your cobbler inventory script (this also can be named anything)

pass -i /path/to/inventory_directory to use both simultaneously!

Thats awesome… will go over to 1.1 asap then (only had rpm package for 1.0 but easy to solve).