PowerShell is a powerful automation and configuration tool that can be used to manage systems and automate tasks. PowerShell 7 is the latest version of PowerShell, and it offers many new features and improvements over previous versions. This article will show you how to install and configure PowerShell 7 using Ansible. To install PowerShell 7, you first need to download the installer from the Microsoft website. Once the installer has been downloaded, you can start it by double-clicking on the file. The installer will start automatically and will ask you to select a language for the installation process. Select English (United States) as your language, and click Next. The next screen will ask you to select your operating system type. Select Windows 10 as your operating system, and click Next. The next screen will ask you to choose your installation location for PowerShell 7. You can either choose an existing location on your computer or create a new location by clicking on the Create button. Once you have chosen a location, click on the OK button to continue with the installation process. The next screen will ask you if you want to install any additional software during the installation process. Click on the Yes button if you want to install any additional software, or click on No if you don’t want any additional software installed during the installation process. The next screen will show you a list of optional features that are available during the installation process for PowerShell 7. If you want any of these optional features installed, click on the Yes button next to each feature name in this list, and then continue with the installation process by clicking on Next once all of these features have been selected in this list. The final screen of this installer window will show you a summary of what has been installed during this process, including information about which languages are supported by PowerShell 7 (English (United States), French (France), German (Germany), Italian (Italy), Spanish (Spain)), as well as information about which modules have been installed ..
PowerShell 7 is a versatile shell and programming language. Now that it is cross-platform, most system admins need a way to deploy the language to multiple systems automatically. To that end, Ansible is a perfect system to create easy to use playbooks to deploy PowerShell 7 to as many systems as may be needed.
In this article, we are going to explore how to create a simple Ansible playbook to install PowerShell 7 to multiple systems.
Installing Ansible on Windows & Linux
To install Ansible on a Linux system, it is very easy. Most package systems for Linux distributions have this built-in. A few of the common ways to install Ansible are as follows:
sudo apt install ansible
sudo yum install ansible
sudo dnf install ansible
Windows is a unique case though, as Ansible is not available as a Windows package. The easiest way to install Ansible for use on Windows is to use the Windows Subsystem for Linux (WSL). This is a virtualized instance of Linux that runs in parallel with Windows. After this is installed, you can use the same installation commands within WSL to install Ansible.
Set Up Ansible
There are a few key components to ansible that we need to install PowerShell 7. Notably, we need a hosts file to define our target locations. There are many ways to set this up, but usually, a folder structure such as below works well.
inventories
playbooks
vars
Within the inventories directory, we would create a hosts file that contains all of the systems that we want to target with our playbook. A simple hosts file that creates a group of hosts under the production tag is outlined below. Comments are useful to tell you what the actual hostname of the systems is.
You are able to create multiple groups of hosts and the same host can exist in multiple groups. This makes grouping and “tagging” those hosts easier for managing them later on for more complex roles.
Creating our Playbook
Now that we have our hosts file, we can start to build our playbook. To do this let’s first create a new folder under the playbooks folder to contain our playbook. In this case, we are going to call it deploy-powershell. Under that folder, we will create the following file, main.yml. The main.yml file is our primary entry point for the playbook. It doesn’t necessarily have to be named main.yml but it is common convention.
Extending our Playbook for Other Hosts
Right now our playbook only handles Ubuntu Linux systems. To update this, we can use Ansible conditionals to make this a bit more robust. Let’s expand this to support Redhat Linux and Fedora systems.
To allow support for other package installation systems, we use the when conditional clause. By reading the ansible_distribution value, we can tell Ansible to only target certain distributions for specific commands.
Running our Playbook
Our playbook is set up, so let’s go ahead and actually install this on the systems that we want. To do this, we will run the following command line.
While running this we will get the results of each step for each host that the playbook is run against and their success rate. Each command will run on each system as the playbook runs, this means that each command needs to complete before moving on to the next command. Additionally, the conditional when will show as a skip within the results.
Conclusion
Ansible makes it easy to install PowerShell on multiple systems at once. Once PowerShell 7 is installed on those systems, you can further use Ansible to then configure the shell for logging, remote access, and other abilities to make it even easier to migrate from Windows PowerShell or PowerShell Core to the new unified PowerShell 7.
Ansible in combination with PowerShell allows you to quickly and easily distribute PowerShell to many different systems in a flexible and convenient method.