PowerShell is a powerful tool that can be used to manage many different tasks. One of the tasks that PowerShell can be used to manage is installing and configuring software. This article will show you how to install and configure PowerShell 7 using Ansible. Requirements You will need the following requirements in order to install PowerShell 7: Ansible 2.4 or later installed on your system. A working installation of Windows 10 or 8.1. You can find a list of installation instructions here. Ansible-2.4-x86_64 (x86_64) or -x86_64 (x86_32) installed on your system, if you are using an x86_64 platform. If you are using a x86_32 platform, you will need to install Ansible-2.4-x32 (x32). The latest stable release of Ansible is available here: ansible-2.4-stable . You can also find the latest development version of Ansible here: ansible-2.4 . An SSH keypair for your system, if you want to use SSH for managing your systems from outside of your workstation or if you want to use SSH for managing your systems from inside your workstation (for example, if you are a remote administrator). You can find an overview of how to create and use SSH keys here: ssh keys . An account with at least 500 hours’ experience in working with Windows Powershell, which includes learning about the cmdlets that are included in PowerShell 7 and how they work. You can find more information about this account here:powershell accounts . The first step is to install Ansible on your system: sudo aptitude install ansible
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.