ansible-collections / community.windows

Windows community collection for Ansible
https://galaxy.ansible.com/community/windows
GNU General Public License v3.0
205 stars 159 forks source link

win_iis_website: Setting physical path to '%SystemDrive%\inetpub\wwwroot' fails #589

Open BradKnowles opened 2 weeks ago

BradKnowles commented 2 weeks ago
SUMMARY
- name: Ensure Default Web Site Configuration
  community.windows.win_iis_website:
    name: Default Web Site
    application_pool: DefaultAppPool
    port: 80
    physical_path: '%SystemDrive%\inetpub\wwwroot'
    state: started

This task fails even though %SystemDrive%\inetpub\wwwroot does exist once the %SystemDrive% has been expanded. I have this task to ensure the Default Web Site settings haven't been tampered with. The tasks resets them to default values.

ISSUE TYPE
COMPONENT NAME
ANSIBLE VERSION
ansible [core 2.16.11]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/ubuntu/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True
COLLECTION VERSION
# /usr/lib/python3/dist-packages/ansible_collections
Collection        Version
----------------- -------
community.windows 2.3.0
CONFIGURATION
CONFIG_FILE() = /home/ubuntu/edc/ansible.cfg
DEFAULT_GATHERING(/home/ubuntu/edc/ansible.cfg) = smart
DEFAULT_HOST_LIST(/home/ubuntu/edc/ansible.cfg) = ['/home/ubuntu/edc/inventories']
DEFAULT_LOG_PATH(/home/ubuntu/edc/ansible.cfg) = /home/ubuntu/edc/log.txt
DEFAULT_VAULT_PASSWORD_FILE(/home/ubuntu/edc/ansible.cfg) = /home/ubuntu/edc/vault-password
OS / ENVIRONMENT
STEPS TO REPRODUCE
- name: Ensure Default Web Site Configuration
  community.windows.win_iis_website:
    name: Default Web Site
    application_pool: DefaultAppPool
    port: 80
    physical_path: '%SystemDrive%\inetpub\wwwroot'
    state: started

Use this task in a playbook targeting a Windows 2019 server with IIS installed.

EXPECTED RESULTS

The value of the Default Web Site physical path to be set to %SystemDrive%\inetpub\wwwroot

ACTUAL RESULTS
TASK [iis-config : Ensure Default Web Site Configuration] **************************************************************************************************
task path: /home/ubuntu/edc/roles/iis-config/tasks/main.yml:42
Using module file /usr/lib/python3/dist-packages/ansible_collections/community/windows/plugins/modules/win_iis_website.ps1
Pipelining is enabled.
<10.0.1.12> ESTABLISH WINRM CONNECTION FOR USER: ansible on PORT 5985 TO 10.0.1.12
EXEC (via pipeline wrapper)
fatal: [APP]: FAILED! => {
    "changed": false,
    "msg": "specified folder must already exist: physical_path",
    "site": {}
}
BradKnowles commented 2 weeks ago

A potential fix would be to call [System.Environment]::ExpandEnvironmentVariables($physical_path) in the line validating the path

https://github.com/ansible-collections/community.windows/blob/e618a2fd429f999fd311029a886829ab23ad7ed6/plugins/modules/win_iis_website.ps1#L53

ElseIf (-not (Test-Path -LiteralPath [System.Environment]::ExpandEnvironmentVariables($physical_path))) {

That would expand the path, test it for existence, and still put the original specified path, with the %SystemDrive% intact.