# Ansible to run the tasks in sequence 

**URL:** <https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385>\
**Category:** Get Help\
**Tags:** ansible, linux\
**Created:** [August 28, 2025, 5:42pm UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385 "2025-08-28T17:42:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![sai\_amulya](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sai_amulya/32/9340_2.png) [@sai\_amulya](https://forum.ansible.com/u/sai_amulya)\
**Post date:** [August 28, 2025, 5:42pm UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385/1 "2025-08-28T17:42:57Z")

</div>

Hi All,

I am writing to build a code with Ansible to run specify tasks on one hosts and another specify tasks on another host in a sequence manner. Here is the use-case:

Role1-- \> needs to be executed on first host which I request  
Role 2–\> rest of other hosts

Here is the condition:  
My first hosts and it’s role must be executed everytime before role2 and other hosts starts its execution

Example:  
Role1(host1) → role2(host2)  
Role1(host1) → role2( host3)

Except for host1 all other hosts must be run in sequence not in parallel

Note: cannot use commands like delegate\_to or pre-tasks or hosts: localhost or serial:1

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [August 28, 2025, 6:53pm UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385/2 "2025-08-28T18:53:31Z")

</div>

Could you elaborate on _why_ you can’t use these options, so we can better understand the limitations you’re working with?

> [@sai\_amulya](#):
>
> Note: cannot use commands like delegate\_to or pre-tasks or hosts: localhost or serial:1

---

<div class="post-metadata">

**Author:** ![sai\_amulya](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sai_amulya/32/9340_2.png) [@sai\_amulya](https://forum.ansible.com/u/sai_amulya)\
**Post date:** [August 28, 2025, 9:34pm UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385/3 "2025-08-28T21:34:39Z")

</div>

Hi lyle,  
The reason we cannot use these commands is that these are been restricted for usage in our organisation so if we try to use them the play building jenkins gets failed for us.

---

<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:** [August 28, 2025, 10:13pm UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385/4 "2025-08-28T22:13:18Z")

</div>

It’s still a bit unclear to me. Does a Jenkins job vet your playbooks? Or does it assemble plays for you with these restrictions? Or something else? I’m trying to understand how Jenkins fits into your workflow.

In any case, disallowing `serial: 1` and `delegate_to` seems like rather harsh restrictions.

Are you allowed to create your own Jenkins jobs? If so, you could move the host selection and serialization up into Jenkins which could fire off `ansible-playbook` invocations in a loop to satisfy your requirements. But I suspect if you can’t use `serial:` then you probably aren’t allowed to customize Jenkins jobs either.

How up-to-date is your résumé? (I’m not sure if that’s a joke. Sorry.)

---

<div class="post-metadata">

**Author:** ![sai\_amulya](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sai_amulya/32/9340_2.png) [@sai\_amulya](https://forum.ansible.com/u/sai_amulya)\
**Post date:** [August 29, 2025, 4:03am UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385/5 "2025-08-29T04:03:06Z")

</div>

Hi,  
Yes jenkins vet the playbooks,  
Though for serial I won’t get a direct restriction but will get a warning to change. I can leave that as it is too. But if use serial : 1 then hosts are picked in serial manner like host1, host2 and host 3 and so on but not as host1, host2 then again host 1 then host3 to do the job

I know that it’s not strange that we cannot use the mentioned restricted commands but cannot help in my case. It’s strictly not allowed atleast for delegate to, hosts: localhost and pre-tasks - it fails my jenkins build  
Serial – I get a warning in build

And to answer the creating own jenkins jobs? – it’s a no, we are not allowed these are already configured jenkins which we use from many years

---

<div class="post-metadata">

**Author:** ![sai\_amulya](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sai_amulya/32/9340_2.png) [@sai\_amulya](https://forum.ansible.com/u/sai_amulya)\
**Post date:** [August 29, 2025, 8:07am UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385/6 "2025-08-29T08:07:59Z")

</div>

Thanks everyone for trying to understand my use case.  
But I cracked the logic using it

Here is the code:

- name: Test the list of tasks  
hosts: all  
gather\_facts: false  
vars:  
hostlist: “{{hostlist.split(‘,’) }}”  
tasks:  
- name: testing the use case  
include\_role:  
name: test-usecase  
loop: “{{ range(1, hostlist | length | int) list}}”  
loop\_control:  
loop\_var: index  
when: inventory\_hostname in [hostlist[0], hostlist[index]]

## Role: role/test-usecase/tasks/main.yml

- name: test the case for host1  
block:  
- name: testing on host1  
shell: hostname  
when: inventory\_ hostname == hostlist[0]

- name: test the case for host2  
block:  
- name: testing on host1  
shell: hostname  
when: inventory\_ hostname != hostlist[0]

---

<div class="post-metadata">

**Author:** ![anon81884396](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anon81884396](https://forum.ansible.com/u/anon81884396)\
**Post date:** [August 29, 2025, 12:25pm UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385/7 "2025-08-29T12:25:16Z")

</div>

Your org is ran by insane people. Good luck

---

<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:** [August 29, 2025, 3:10pm UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385/8 "2025-08-29T15:10:09Z")

</div>

May I suggest a few tweaks? First off is this variable initialization:

```yml
vars:
  hostlist: "{{ hostlist.split(',') }}"

```

If you’re passing in `hostlist` as an `--extra-vars`, this isn’t going to work.  
It looks like you’re redefining the variable `hostlist` in terms of itself, but it doesn’t do that. Instead, it defines a new _Play var_ (precedence 12) which has the same name as an _Extra var_ (precedence 22). That doesn’t make the _Extra var_ go away. Since _Extra vars_ have the highest [precedence](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#understanding-variable-precedence), the identically named _Play var_’s value will never be seen. (It may “work” for you in older Ansibles, but it behaves as I’ve described above in my Ansible core 2.18.6.)

I recommend a different _Extra var_ name, say `hostlist_in`, so your variable initialization becomes  
`hostlist: "{{ hostlist_in.split(',') }}"`

This next one is personal preference, but I like to avoid creating index variables when filters can manipulate the data directly into the form I need. In this case, you want to create pairs of host names where the _first_ or _left_ host in each pair is always `hostlist[0]` and the _second_ or _right_ host is in turn each of the other `hostlist` hosts. The `product` filter will do exactly that.  
`loop: "{{ [hostlist[0]] | product(hostlist[1:]) }}"`

Lastly, when posting code (or editing a prior post containing code - hint, hint) to the forum, precede and follow the code with lines containing only three back-ticks (“```”). You can include a syntax highlighting hint on the first line, like this:

````
```yaml
- name: Code never looked so good
  ansible.builtin.admire: |
    Hey, I can read this easily!
```

````

With the above changes, here’s my re-working of your test/example. Note that both `hosts:` and the _Play var_ `hostlist` are derived from the same _Extra var_ `hostlist_in`. It would be neat if you could use the `ansible_play_hosts` special variable which will contain the same hosts as your `limit:` and `hostlist_in`; then you wouldn’t have to pass in the hosts twice. Alas, `ansible_play_hosts` is a sorted list, and we must distinguish the first host from the others. (Which we could do, but that adds more code which kind of defeats the purpose.)

```yaml
# test-playbook.yml
- name: Test the list of tasks
  hosts: "{{ hostlist_in | split(',') }}"
  gather_facts: false
  vars:
    hostlist: "{{ hostlist_in.split(',') }}"
  tasks:
    - name: Testing the use case
      ansible.builtin.include_role:
        name: test-usecase
      loop: "{{ [hostlist[0]] | product(hostlist[1:]) }}"
      loop_control:
        loop_var: hostpair

```

```yml
---
# role/test-usecase/tasks/main.yml
- name: Document this role inclusion
  ansible.builtin.debug:
    msg: "test-usecase -- primary: {{ hostpair[0] }}, secondary: {{ hostpair[1] }}"
  run_once: true

- name: Test block for primary host {{ hostpair[0] }}
  block:
    - name: Primary is {{ hostpair[0] }}
      ansible.builtin.shell: hostname
  when: inventory_hostname == hostpair[0]

- name: Test block for secondary host {{ hostpair[1] }}
  block:
    - name: Secondary is {{ hostpair[1] }}
      ansible.builtin.shell: hostname
  when: inventory_hostname == hostpair[1]

```

---

<div class="post-metadata">

**Author:** ![sai\_amulya](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sai_amulya/32/9340_2.png) [@sai\_amulya](https://forum.ansible.com/u/sai_amulya)\
**Post date:** [August 29, 2025, 3:25pm UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385/9 "2025-08-29T15:25:46Z")

</div>

Hi,

I will definitely give a try and let you know on result. Thank you, for giving an alternative for my conditions specified.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 29, 2025, 3:50pm UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385/10 "2025-08-29T15:50:15Z")

</div>

FWIW, a simple script does the job

```sh
#!/usr/bin/bash

IFS=',' read -r -a hosts <<< "$1"

ansible-playbook pb1.yml -l "${hosts[0]}"

for host in "${hosts[@]:1}"; do
    ansible-playbook pb2.yml -l "$host"
done

```

For example,

```yaml
shell> ./project.bash "test_03,test_01,test_02"

PLAY [all] **********************************************************************************************************

TASK [role1 : debug] ************************************************************************************************
ok: [test_03] => 
    msg: 'role1: test_03'

PLAY RECAP **********************************************************************************************************
test_03 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   

PLAY [all] **********************************************************************************************************

TASK [role2 : debug] ************************************************************************************************
ok: [test_01] => 
    msg: 'role2: test_01'

PLAY RECAP **********************************************************************************************************
test_01 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   

PLAY [all] **********************************************************************************************************

TASK [role2 : debug] ************************************************************************************************
ok: [test_02] => 
    msg: 'role2: test_02'

PLAY RECAP **********************************************************************************************************
test_02 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

```

---

<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:** [August 31, 2025, 7:32pm UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385/11 "2025-08-31T19:32:36Z")

</div>

@sai_amulya 's requirements are slightly different, as he needs to run tasks on the first host before each run on the other hosts. So giving a list of four host, it should run three times on the first host and once on each of the other hosts. The following modification to your script would do that (except for the `echo ansible-playbook` instead of `ansible-playbook` of course).

```sh
#!/usr/bin/bash

IFS=',' read -r -a hosts <<< "$1"

primary="${hosts[0]}"
for secondary in "${hosts[@]:1}" ; do
  # These ansible-playbook commands are given
  # as "echo"s for demo purposes.
  echo ansible-playbook pb1.yml -l "$primary"
  echo ansible-playbook pb2.yml -l "$secondary"
  echo
done

```

which produces

```txt
$ ./project2.bash "center,top,right,bottom,left"
ansible-playbook pb1.yml -l center
ansible-playbook pb2.yml -l top

ansible-playbook pb1.yml -l center
ansible-playbook pb2.yml -l right

ansible-playbook pb1.yml -l center
ansible-playbook pb2.yml -l bottom

ansible-playbook pb1.yml -l center
ansible-playbook pb2.yml -l left

```

The other complications come from, em, “crazy management policy”. He can’t invoke `ansible-playbook` directly, but only through Jenkins jobs that are provided to him, and those jobs reject unauthorized Ansible constructs. It’s a bit like learning to juggle with your hands in your pockets.

But otherwise, scripting this seems like a reasonable approach to dealing with these strange host (re-)ordering requirements.

---

<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:** [September 30, 2025, 7:33pm UTC](https://forum.ansible.com/t/ansible-to-run-the-tasks-in-sequence/44385/12 "2025-09-30T19:33:31Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
