I currently have a powershell script (deploy.ps1) which I use to manage files on a network drive. When running my script, I am successfully able to map to the drive and access the contents. However, when attempting to do this by via an Ansible playbook, I get problems relating to my network password not being correct.
deploy.ps1:
$mapPath = "\\computer\path\to\blah"
$username = "username"
$password = "password"
$securePassword = Convert-To-SecureString -String $password -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PScredential -ArgumentList $username, $securePassword
New-PSDrive -Name "TempDrive" -PSProvider "FileSystem" -root $mapPath -Credential $cred
playbook.yaml:
name: Deploy Report
hosts: myhost
tasks:
- name: Run Deployment Script
script: "../deploy.ps1"
register: out
I am sure that this is an Ansible-specific problem because it runs flawlessly every time without Ansible. Any help or advice is greatly appreciated.
The problem is that you need to specify your domain within the credential username using the format domain\username. For example, if your domain is google and your username is dan, the username passed into the credential should be google\dan
.
The reason this worked locally was likely due to that your computer assumed your domain. However, once you ran it in Ansible, this default domain did not exist.
More info and an example of using credentials to map can be found here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive?view=powershell-7.3