I am trying to automate building an MS RDS Server 2016. The automation of the server build works great up until the last part when it tries to install RDS. Two scripts run separately to install RDS, the first one installs the RDS feature, and then Ansible should restart the server after the feature gets installed. The first PS script below is not running when called on by the Ansible playbook and it does not restart the server as well.
Both scripts get copied to a temp folder on the server correctly via Ansible. Both scripts work perfectly when I run them from the PS editor but when I try to use Ansible they do not work. The 2nd script should take 5-7 minutes to install RDS, it finishes within seconds I am starting to think it is the way the scripts are executing since it works perfectly in the PS editor but does not work when being called on by Ansible. Any suggestions on how to troubleshoot further?
I’m not a Windows peson at all, but it looks to me a bit as if you may not be waiting long enough after the reboot? To test this, just put in a fixed (say) two minute delay to make absolutely certain; f that makes a difference you can work on fine-tuning the delay and/or detecting readiness.
The other possibility is the variables in the second script - you might like to add code to the script to output the value of “$servername” to a file, then inspect the file when the script has been run. And/or experimentally hardcode a servername just to see if it then works.
Thirdly, what if anything is in the variable “RDSInstall” after the script has been run? A debug statement in your Ansible script might help there.
Do you get an error message or any output when running it through Ansible. If you do, sharing that would really help with identifying what is going on.
Some other suggestion around making your playbook a bit more Ansible-like;
The first script, TS_Script1.ps1 can be simplified by using win_feature. This gives you idempotency, should be slightly faster, and you don’t clutter up your role and playbook with unneeded script files
The waiting task is a bit weird, win_reboot runs wait_for_connection at the end which is a simple action plugin that runs the (win_)ping module until it is ready. You shouldn’t need this task at all and I don’t even think it works the way you think it may. The ping module won’t work on Windows hosts, you need win_ping but once again you shouldn’t need this at all
The do not need to copy the script using win_copy, just use script to execute the script stored on the controller. It has all the logic to copy the file and execute it
You also need to make this script idempotent and handle it being run after RDS is installed. You can look at that after you get the install working but it’s something to consider
The last win_reboot task won’t work as RDSInstall would be a dict and not a string. You would have to access the output through something like ‘RDSInstall.stdout_lines[0] == “Complete”’ or something like that. It’s hard to tell what you need to check to idempotently do the reboot.
Thank you Jordan and Karl for the replies, they are much appreciated! After making changes to my script per suggestions from both of you it still seems that the PS install script is not running. I have tried hard coding the server name with no luck. I have also used Jordan’s ansible playbook with no luck as well although using the Win feature works great! I am new to ansible so still trying to figure all of this out. Still have no idea why the install ps script will not run but yet works perfectly when I run it manually.