# Run ansible tasks on hosts one-by-one

**URL:** <https://forum.ansible.com/t/run-ansible-tasks-on-hosts-one-by-one/38567>\
**Category:** Ansible Project\
**Created:** [November 3, 2023, 3:03pm UTC](https://forum.ansible.com/t/run-ansible-tasks-on-hosts-one-by-one/38567 "2023-11-03T15:03:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [November 3, 2023, 3:03pm UTC](https://forum.ansible.com/t/run-ansible-tasks-on-hosts-one-by-one/38567/1 "2023-11-03T15:03:18Z")

</div>

Here is my question exactlyt the same.

[https://stackoverflow.com/questions/64048208/run-ansible-tasks-on-hosts-one-by-one](https://stackoverflow.com/questions/64048208/run-ansible-tasks-on-hosts-one-by-one)

Run 3 tasks one after another on single host at at a time.  
if any of that tasks fails dont move ahead.

Now when i use throttle with block it runs tasks on both the hosts.

Serial does not work with block at at all.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 3, 2023, 6:06pm UTC](https://forum.ansible.com/t/run-ansible-tasks-on-hosts-one-by-one/38567/2 "2023-11-03T18:06:23Z")

</div>

serial is a PLAY level keyword and what you want to use in this case,  
throttle will only control the forks/parallel jobs, not serialize a  
batch (this is what serial does).

---

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [November 3, 2023, 7:57pm UTC](https://forum.ansible.com/t/run-ansible-tasks-on-hosts-one-by-one/38567/3 "2023-11-03T19:57:40Z")

</div>

How to acheive this then ??

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 3, 2023, 10:31pm UTC](https://forum.ansible.com/t/run-ansible-tasks-on-hosts-one-by-one/38567/4 "2023-11-03T22:31:04Z")

</div>

Put the tasks in a play, you can have multiple plays per playbook

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 4, 2023, 1:04am UTC](https://forum.ansible.com/t/run-ansible-tasks-on-hosts-one-by-one/38567/5 "2023-11-04T01:04:44Z")

</div>

```auto
- name: One host at a time
  hosts: ducks_in_a_row
  serial: 1
  max_fail_percentage: 0
  tasks:
    - task1
    - task2
    - task3

```

---

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [November 6, 2023, 12:49pm UTC](https://forum.ansible.com/t/run-ansible-tasks-on-hosts-one-by-one/38567/6 "2023-11-06T12:49:01Z")

</div>

so what i tried is , I created another playbook and inside that mentioned serial which works fine with add\_host . I am getting expected results but when i tried to add this code in my actual role. (I first test code in temporary playbook , so that i dont mess the role)

But when copy paste the code in my role here obviously structure is different,  
main.yml–\>zookeeper.yml(task yaml )–\> imported playbook(new playbook as serial only works for plays).

code ,

this is zookeeper.yml which is a task list yaml, and not a playbook

- name: Check who is existing zokeeper leader

shell: ‘echo stat | nc localhost “{{hostvars[inventory\_hostname].zk\_port}}” | grep Mode’

register: zkmode

- name: Set service name as a fact

set\_fact:

service\_name: “{{ zookeeper\_service\_name }}”

- name: Restart playbook

import\_playbook: Restart.yml

now when i run the role i get

ERROR! this task ‘import\_playbook’ has extra params, which is only allowed in the following modules: command, include, ansible.legacy.meta, ansible.legacy.set\_fact, import\_tasks, ansible.legacy.import\_role, win\_shell, group\_by, win\_command, ansible.builtin.set\_fact, include\_vars, ansible.builtin.shell, ansible.builtin.meta, ansible.legacy.group\_by, ansible.legacy.win\_command, set\_fact, import\_role, ansible.legacy.command, ansible.legacy.include\_tasks, ansible.builtin.import\_role, ansible.legacy.raw, meta, include\_role, ansible.legacy.win\_shell, include\_tasks, ansible.builtin.script, add\_host, ansible.legacy.add\_host, ansible.builtin.include\_tasks, script, ansible.builtin.include\_vars, ansible.windows.win\_command, ansible.builtin.add\_host, ansible.legacy.include, ansible.legacy.include\_role, ansible.legacy.shell, ansible.builtin.include\_role, shell, ansible.legacy.script, ansible.builtin.win\_shell, ansible.builtin.win\_command, ansible.builtin.command, ansible.builtin.raw, raw, ansible.legacy.import\_tasks, ansible.legacy.include\_vars, ansible.builtin.group\_by, ansible.windows.win\_shell, ansible.builtin.include, ansible.builtin.import\_tasks

The error appears to be in ‘/Users/sameer\_modak/ansibledemo/prometheus/tasks/zookeeper\_prometheus.yml’: line 118, column 3, but may

be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Imporing Restartandcheck yaml

^ here

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 6, 2023, 3:13pm UTC](https://forum.ansible.com/t/run-ansible-tasks-on-hosts-one-by-one/38567/7 "2023-11-06T15:13:33Z")

</div>

roles can be in plays, plays cannot be in roles

---

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [November 10, 2023, 10:04am UTC](https://forum.ansible.com/t/run-ansible-tasks-on-hosts-one-by-one/38567/8 "2023-11-10T10:04:32Z")

</div>

how should place these playbooks and what should be the order. Below is my role .

[https://github.com/sameergithub5/prometheusrole](https://github.com/sameergithub5/prometheusrole)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 10, 2023, 4:01pm UTC](https://forum.ansible.com/t/run-ansible-tasks-on-hosts-one-by-one/38567/9 "2023-11-10T16:01:23Z")

</div>

Pllaybooks can contain plays, plays can contain tasks, blocks and  
roles, but you cannot reverse the direction of 'containers'.

You CANNOT have a play in a role, so your question is a non starter.  
You have to write a playbook with multiple plays.

---

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [November 11, 2023, 2:25pm UTC](https://forum.ansible.com/t/run-ansible-tasks-on-hosts-one-by-one/38567/10 "2023-11-11T14:25:32Z")

</div>

ok i heard you. Let met try
