> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/continuous-delivery/use-continuous-delivery/cd-building-blocks/cd-steps/run-a-script-on-multiple-target-instances.md).

# Run a step on multiple target instances

Run the same step on every target host in an SSH, WinRM, or deployment template stage.

When you deploy to multiple hosts, such as in a Secure Shell (SSH), Windows Remote Management (WinRM), or deployment template stage, you can run the same step on all of the target hosts.

To run the step on all hosts, use the **Repeat** looping strategy and set the target to every host in the stage. Go to [looping strategies](https://developer.harness.io/docs/platform/pipelines/looping-strategies/looping-strategies-matrix-repeat-and-parallelism) to configure Repeat.

```yaml
repeat:
  items: <+stage.output.hosts>
```

The following example uses a Shell Script step:

![Shell Script step on the Advanced tab, with Repeat selected and items set to stage output hosts](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-26ff87edceb7ea859018c9104aacf9f510f93e7a%2Frun-a-script-on-multiple-target-instances-00.png?alt=media)

***

## What you will learn from this topic

* How to choose a [supported deployment type](#supported-deployment-types) for multiple hosts
* How to [reference each host](#reference-hosts-in-a-step) with instance expressions
* How to [add the step](#add-the-step) and [set Repeat](#set-the-looping-strategy) with `<+stage.output.hosts>`
* How to [run the pipeline](#run-the-pipeline) and confirm the step runs on each target host

***

## Before you begin

Confirm the following before you repeat a step across hosts:

* **Deployment stage**: Use an SSH, WinRM, or deployment template stage. Go to [Supported deployment types](#supported-deployment-types) to choose the stage and where Repeat can run.
* **Fetch Instances (deployment templates only)**: Any step that uses Repeat with `<+stage.output.hosts>` must come after the **Fetch Instances** step.

***

## Supported deployment types

You can use Repeat with `<+stage.output.hosts>` on the following deployment types:

* [SSH (Traditional)](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/traditional/ssh-ng.md): Deploy artifacts to hosts over SSH. You can use Repeat on a step anywhere in execution, because the hosts are fetched as part of the environment.
* [WinRM](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/traditional/win-rm-tutorial.md): Deploy artifacts to Windows hosts. You can use Repeat on a step anywhere in execution, because the hosts are fetched as part of the environment.
* [Custom deployments using deployment templates](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/custom/custom-deployment-tutorial.md): Place any step that uses Repeat with `<+stage.output.hosts>` after the **Fetch Instances** step.

All three types can deploy your artifacts to hosts in Microsoft Azure, Amazon Web Services (AWS), or any platform-agnostic Physical Data Center (PDC).

***

## Reference hosts in a step

You can use the instance expressions to reference your hosts. These expressions can be used only with a looping strategy. Go to [deployment instance expressions](https://developer.harness.io/docs/platform/variables-and-expressions/harness-expressions-reference#deployment-instance-expressions) to review the full list.

{% hint style="warning" %}
**Where instance expressions work**

The `<+instance...>` expression is supported only in SSH, WinRM, and custom deployments that use deployment templates.
{% endhint %}

For Microsoft Azure, AWS, or any platform-agnostic PDC:

* `<+instance.hostName>`
* `<+instance.host.instanceName>`
* `<+instance.name>`

For Microsoft Azure or AWS:

* `<+instance.host.privateIp>`
* `<+instance.host.publicIp>`

`instance.name` has the same value as `instance.hostName`. Both are available for backward compatibility.

***

## Add the step

Perform the following steps to add the step that runs on every host:

1. In the stage, go to **Execution** -> **Add Step**.
2. Select the step you want to run on all hosts.
3. Configure the step.

Depending on the type of step you add, you might want to reference the host instances. For example, a Shell Script step can run on all hosts.

***

## Set the looping strategy

The Repeat looping strategy repeats the step for all target hosts. The strategy iterates through the list of hosts. The list of hosts is identified with the expression `<+stage.output.hosts>`.

Perform the following steps to repeat the step for every target host:

1. In the step, select **Advanced**.
2. Select **Looping Strategy**.
3. Select **Repeat**.
4. Enter the following:

```yaml
repeat:
  items: <+stage.output.hosts>
```

The following example uses a Shell Script step:

![Shell Script step on the Advanced tab, with Repeat selected and items set to stage output hosts](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-26ff87edceb7ea859018c9104aacf9f510f93e7a%2Frun-a-script-on-multiple-target-instances-00.png?alt=media)

5. Select **Apply Changes**.

***

## Run the pipeline

After the pipeline runs, the step is applied to multiple hosts.

For example, here is a custom deployment stage that uses a deployment template.

![Custom deployment execution with Fetch Instances followed by a loop of Shell Script steps sh-1\_0 and sh-1\_1](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-0238309addb4bd6f62f6f0f4cf7d4606e479b5f2%2Frun-a-script-on-multiple-target-instances-02.png?alt=media)

The **Fetch Instances** step returned two instances, and the Shell Script step ran on both using the Repeat looping strategy.

Here is an SSH deployment example with a Command step that uses `<+stage.output.hosts>`:

![Command step on the Advanced tab in an SSH stage, with Repeat selected and items set to stage output hosts](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-d861acb2ee80c66db4e0fd7fd1374b869510d4ce%2Frun-a-script-on-multiple-target-instances-03.png?alt=media)

After the pipeline runs, each Deploy step runs on each of the two target hosts:

![SSH execution with a loop of Deploy steps Deploy\_0 and Deploy\_1, and logs for two target hosts](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-d984141f282c22d5a5cd9e4ff6e797370a606496%2Frun-a-script-on-multiple-target-instances-04.png?alt=media)

You can also add a Shell Script step to echo `<+stage.output.hosts>` to see the target hosts. The expression resolves to a list like this SSH example that deploys to AWS Elastic Compute Cloud (EC2) instances:

```
[ec2-54-201-142-249.us-west-2.compute.amazonaws.com, ec2-54-190-26-183.us-west-2.compute.amazonaws.com]
```

***

## Next steps

* [Looping strategies](https://developer.harness.io/docs/platform/pipelines/looping-strategies/looping-strategies-matrix-repeat-and-parallelism): Compare Repeat with matrix and parallelism.
* [Deployment instance expressions](https://developer.harness.io/docs/platform/variables-and-expressions/harness-expressions-reference#deployment-instance-expressions): Review the instance expressions you can use inside the repeated step.
* [SSH (Traditional)](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/traditional/ssh-ng.md): Deploy artifacts to hosts over SSH.
* [WinRM](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/traditional/win-rm-tutorial.md): Deploy artifacts to Windows hosts.
* [Custom deployments using deployment templates](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/custom/custom-deployment-tutorial.md): Deploy with a deployment template and a Fetch Instances step.

{% @harness-feedback/feedback %}
