diff --git a/changelogs/fragments/56567-Fix-win_iis_website-id assignment-for-first-new-site.yml b/changelogs/fragments/56567-Fix-win_iis_website-id assignment-for-first-new-site.yml new file mode 100644 index 00000000000..515c1c2d88f --- /dev/null +++ b/changelogs/fragments/56567-Fix-win_iis_website-id assignment-for-first-new-site.yml @@ -0,0 +1,2 @@ +bugfixes: + - "win_iis_website - site_id not used if no sites exist already and creating a new site (https://github.com/ansible/ansible/issues/47057)" \ No newline at end of file diff --git a/lib/ansible/modules/windows/win_iis_website.ps1 b/lib/ansible/modules/windows/win_iis_website.ps1 index c6093721898..7851192fd38 100644 --- a/lib/ansible/modules/windows/win_iis_website.ps1 +++ b/lib/ansible/modules/windows/win_iis_website.ps1 @@ -84,7 +84,13 @@ Try { # This is a bug in the New-WebSite commandlet. Apparently there must be at least one site configured in IIS otherwise New-WebSite crashes. # For more details, see http://stackoverflow.com/questions/3573889/ps-c-new-website-blah-throws-index-was-outside-the-bounds-of-the-array $sites_list = get-childitem -Path IIS:\sites - if ($null -eq $sites_list) { $site_parameters.ID = 1 } + if ($null -eq $sites_list) { + if ($site_id) { + $site_parameters.ID = $site_id + } else { + $site_parameters.ID = 1 + } + } $site = New-Website @site_parameters -Force $result.changed = $true