# execute a task or playbook once per play?

**URL:** <https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616>\
**Category:** Ansible Project\
**Created:** [April 2, 2013, 12:08pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616 "2013-04-02T12:08:27Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![PJ1](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@PJ1](https://forum.ansible.com/u/PJ1)\
**Post date:** [April 2, 2013, 12:08pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/1 "2013-04-02T12:08:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [April 2, 2013, 12:32pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/2 "2013-04-02T12:32:44Z")

</div>

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

Brian Coca

---

<div class="post-metadata">

**Author:** ![PJ1](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@PJ1](https://forum.ansible.com/u/PJ1)\
**Post date:** [April 2, 2013, 12:39pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/3 "2013-04-02T12:39:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PJ1](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@PJ1](https://forum.ansible.com/u/PJ1)\
**Post date:** [April 2, 2013, 12:43pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/4 "2013-04-02T12:43:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [April 2, 2013, 1:19pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/5 "2013-04-02T13:19:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 2, 2013, 1:46pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/6 "2013-04-02T13:46:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PJ1](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@PJ1](https://forum.ansible.com/u/PJ1)\
**Post date:** [April 2, 2013, 3:13pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/7 "2013-04-02T15:13:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 2, 2013, 5:17pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/8 "2013-04-02T17:17:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PJ1](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@PJ1](https://forum.ansible.com/u/PJ1)\
**Post date:** [April 2, 2013, 6:38pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/9 "2013-04-02T18:38:23Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [April 2, 2013, 7:04pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/10 "2013-04-02T19:04:14Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![PJ1](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@PJ1](https://forum.ansible.com/u/PJ1)\
**Post date:** [April 3, 2013, 7:46am UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/11 "2013-04-03T07:46:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PJ1](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@PJ1](https://forum.ansible.com/u/PJ1)\
**Post date:** [April 3, 2013, 10:22am UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/12 "2013-04-03T10:22:43Z")

</div>

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):

"

```auto
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 ... 

```

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 3, 2013, 12:32pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/13 "2013-04-03T12:32:19Z")

</div>

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

assume /path/to/inventory\_directory contains:  
&nbsp;&nbsp;&nbsp;a host file defining your localhost (this can be named anything)  
&nbsp;&nbsp;&nbsp;your cobbler inventory script (this also can be named anything)

pass -i /path/to/inventory\_directory to use both simultaneously!

---

<div class="post-metadata">

**Author:** ![PJ1](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@PJ1](https://forum.ansible.com/u/PJ1)\
**Post date:** [April 3, 2013, 12:55pm UTC](https://forum.ansible.com/t/execute-a-task-or-playbook-once-per-play/12616/14 "2013-04-03T12:55:06Z")

</div>

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