Ansible – Adding Powershell and PowerCLI

Apart from managing Linux and VMware machines (and many other things) out of        the box. It is also possible to utilise PowerShell. For this reason I wanted to just briefly show how to add Powershell to the Ansible server and the later on go over deploying Windows boxes and roles via Ansible. There is one little detail, Powershell 7 is not officially listed as supported with CentOS 8/RHEL 8 but it should still install/work.

Installing PowerShell

First you need to add the repository to the Ansible machine, to do this run the following command:

curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo

 

Check that you have a new file on your repo folder (etc/yum.repos.d/):

02-repo

 

You can see the file as the last file and below I’ve just done an output of the content of the file.

Next run this command to install Powershell on your Ansible server:

sudo dnf install -y powershell

 

This should give you an output like this:

03-powershell-install

 

You can query the version installed of PowerShell by running the following command:

rpm -qi powershell

 

That gives you an output like this:

04-ps-version

 

Next you can start PowerShell by typing pwsh

Note you get a new prompt:

05-running-exiting

 

You can use PowerShell in pretty much the same way as in Windows, for example, just to demonstrate the get- and double tab and you get the list of modules and then running get-timezone and we get the local time zone of the Ansible box.

06-sample-command

 

You can exit Powershell by typing ‘exit’ and then you are returned to the normal linux bash.

Installing PowerCLI

If you want to install PowerCLI also to manage VMware infrastructure, it is quite simple to add.

You need to make the PSGallery a trusted resource, to do this run the following:

 

Set-PSRepository -Name "PSGallery" -InstallationPolicy "Trusted"

 

Subsequently you can run the following to install PowerCLI:

 

Find-Module "VMware.PowerCLI" | Install-Module -Scope "CurrentUser" -AllowClobber

 

That is it. You should now be able to use PowerCLI via your ansible host.

 

Removing Powershell

If you need to, you can remove PowerShell again by running:

sudo  dnf remove powershell -y

 

So that is it for PowerShell installation, this should give us some cool opportunity in managing Windows Servers in the near future.

Reference