Run ansible tasks on hosts one-by-one

117 views
Skip to first unread message

Sameer Modak

unread,
Nov 3, 2023, 11:03:19 AM11/3/23
to Ansible Project


Here is my question exactlyt the same.


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.

Brian Coca

unread,
Nov 3, 2023, 2:06:48 PM11/3/23
to ansible...@googlegroups.com
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).

--
----------
Brian Coca

Sameer Modak

unread,
Nov 3, 2023, 3:57:41 PM11/3/23
to Ansible Project

How to acheive this then ??

Brian Coca

unread,
Nov 3, 2023, 6:31:33 PM11/3/23
to ansible...@googlegroups.com
Put the tasks in a play, you can have multiple plays per playbook


--
----------
Brian Coca

Todd Lewis

unread,
Nov 3, 2023, 9:04:58 PM11/3/23
to ansible...@googlegroups.com, uto...@gmail.com
- name: One host at a time
  hosts: ducks_in_a_row
  serial: 1
  max_fail_percentage: 0
  tasks:
    - task1
    - task2
    - task3
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a628eae5-14f1-4e17-9425-2643282af93dn%40googlegroups.com.

-- 
Todd
Message has been deleted

Sameer Modak

unread,
Nov 6, 2023, 7:49:01 AM11/6/23
to Ansible Project
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


Brian Coca

unread,
Nov 6, 2023, 10:13:58 AM11/6/23
to ansible...@googlegroups.com
roles can be in plays, plays cannot be in roles


--
----------
Brian Coca

Sameer Modak

unread,
Nov 10, 2023, 5:04:32 AM11/10/23
to Ansible Project
how should place these playbooks and what should be the order. Below is my role . 

Brian Coca

unread,
Nov 10, 2023, 11:01:55 AM11/10/23
to ansible...@googlegroups.com
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.

--
----------
Brian Coca

Sameer Modak

unread,
Nov 11, 2023, 9:25:32 AM11/11/23
to Ansible Project
ok i heard you. Let met try 
Reply all
Reply to author
Forward
0 new messages