I have a playbook test_loop.yml, which includes another playbook and uses loop, which then includes one more playbook as shown below. I need to run the tasks under 3.yml only once per test_loop.yml execution. I tried like below but 3.yml contents are getting executed for each iteration of the loop. Any way I can limit the execution of 3.yml to only once?
test_loop.yml
- hosts: all
tasks:
- include: one.yml
loop:
- 1
- 2
one.yml
- debug:
msg: "I'm one.yml item {{ item }}"
- include: 3.yml
3.yml
- debug:
msg: "I'm a task"
run_once: true
You misinterpreted the meaning of run_once. Its purpose is to run the task a single time for the batch of hosts in the play (e.g. iterate over hosts to install a clustered application but run a registration task only once at the end....). It will not stop your loop. Actually, each iteration in the loop is as if your write several tasks with different parameters each time.
In your example, this ends up having your task in 3.yml being run only for the first host in the play for each iteration ([1, 2]), which is precisely what you see.
If you want this task to run only once for your loop, don't loop it. If you want it to run only once for your loop and only for the first host in your play batch, don't loop it and add run_once as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With